最大公共字串LCS问题(阿里巴巴)
给定两个串,均由最小字母组成。求这两个串的最大公共字串LCS(Longest Common Substring)。
使用动态规划解决。
#include <iostream>
#include <vector>
#include <cstring>
#include <algorithm> using namespace std; #define MAX 100 int LCS(string left, string right){
int imax = -1;
int m = left.size();
int n = right.size(); int i,j;
int x,y; vector<vector<int> > temp(m, vector<int>(n,0)); for(i=0; i<m; i++){
for(j=0; j<n; j++){
if(left[i] == right[j]){
if (i == 0 || j == 0){
temp[i][j] = 1;
}else{
temp[i][j] = temp[i-1][j-1] + 1;
} if(temp[i][j] > imax){
imax = temp[i][j];
x = i;
y = j;
}
}
}
} /* output the common substring */
i = x, j = y;
int k = imax;
string s(min(m,n),0);
s[k--] = '\0'; while(i>=0 && j>=0){
if(left[i] == right[j]){
s[k--] = left[i];
i--;
j--;
}else{
break;
}
} cout<<s<<endl; return imax;
} int main(){
int result = 0; string query, text;
cin>>query>>text; cout<<query<<endl;
cout<<text<<endl; result = LCS(query, text); cout<<result; return 0;
}
最大公共字串LCS问题(阿里巴巴)的更多相关文章
- (字符串)最长公共字串(Longest-Common-SubString,LCS)
题目: 给定两个字符串X,Y,求二者最长的公共子串,例如X=[aaaba],Y=[abaa].二者的最长公共子串为[aba],长度为3. 子序列是不要求连续的,字串必须是连续的. 思路与代码: 1.简 ...
- URAL 1517 Freedom of Choice(后缀数组,最长公共字串)
题目 输出最长公共字串 #define maxn 200010 int wa[maxn],wb[maxn],wv[maxn],ws[maxn]; int cmp(int *r,int a,int b, ...
- POJ - 2774~POJ - 3415 后缀数组求解公共字串问题
POJ - 2774: 题意: 求解A,B串的最长公共字串 (摘自罗穗骞的国家集训队论文): 算法分析: 字符串的任何一个子串都是这个字符串的某个后缀的前缀. 求 A 和 B 的最长 公共子串等价于求 ...
- 最长公共字串(LCS)最长连续公共字串(LCCS)
链接1:http://blog.csdn.net/x_xiaoge/article/details/7376220 链接2:http://blog.csdn.net/x_xiaoge/article/ ...
- 最长公共字串算法, 文本比较算法, longest common subsequence(LCS) algorithm
''' merge two configure files, basic file is aFile insert the added content of bFile compare to aFil ...
- 最长公共子序列与最长公共字串 (dp)转载http://blog.csdn.net/u012102306/article/details/53184446
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- POJ - 3080 Blue Jeans 【KMP+暴力】(最大公共字串)
<题目链接> 题目大意: 就是求k个长度为60的字符串的最长连续公共子串,2<=k<=10 限制条件: 1. 最长公共串长度小于3输出 no significant co ...
- poj 3080 kmp求解多个字符串的最长公共字串,(数据小,有点小暴力 16ms)
Blue Jeans Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 14113 Accepted: 6260 Descr ...
- POJ - 3294~Relevant Phrases of Annihilation SPOJ - PHRASES~Substrings POJ - 1226~POJ - 3450 ~ POJ - 3080 (后缀数组求解多个串的公共字串问题)
多个字符串的相关问题 这类问题的一个常用做法是,先将所有的字符串连接起来, 然后求后缀数组 和 height 数组,再利用 height 数组进行求解. 这中间可能需要二分答案. POJ - 3294 ...
随机推荐
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
- [No000088]并行循环vs普通循环
using System; using System.Collections.Generic; using System.Threading; using System.Threading.Tasks ...
- 记录android显示流程
mtk平台: displayclient->deque->数据放入显存->surfaceflinger->分发数据到surfacetexture(OnFrameAvailabl ...
- JS当心隐式的强制转换
JavaScript对类型错误出奇的宽容 3 + true; // 4 null + 3; // 3 运算符+(加号)的重载 运算符+既重载了数字相加,又重载了字符串连接操作.具体是数字相加还是字符串 ...
- asp.net core 部署 提示DataProtectionServices 错误
今天在部署asp.net core网站时,因为调用到阿里云的api,api的参数需要加密签名,系统报出了如下错误: warn: Microsoft.Extensions.DependencyInjec ...
- ie与火狐中常见的一些兼容问题
1. document.form.item 问题 (1)现有问题: 现有代码中存在许多 document.formName.item("itemName") 这样的语句,不能在Fi ...
- WPF实现物理效果 拉一个小球
一直以来都对物理效果有神秘感,完全不知道怎么实现的.直到看到了周银辉在老早前写的一篇博客:http://www.cnblogs.com/zhouyinhui/archive/2007/06/23/79 ...
- CSS基本知识2-CSS选择
选择就是CSS定义的第一部分,可以用面向对象的模式来理解,或者声明式的面向对象. 标准选择: #.E 进阶选择:“,”分隔多个相同项,相当于类的实例. 如:#btn1,#btn2,.btn {...} ...
- canvas弹动效果
弹动效果,用物体与目标的距离乘上系数再累加至速度上,让物体呈加速度运动,再让速度乘与摩擦力系数,让物体最终停止运动 代码如下所示 var canvas = document.getElementByI ...
- [转]ExtJS之遍历Store
原文地址:http://blog.sina.com.cn/s/blog_67cc6e7d0100ox6u.html ExtJS中,一般很少需要遍历Store,因为它的selectModel很好用,无论 ...