题目:

给定两个字符串X,Y,求二者最长的公共子串,例如X=[aaaba],Y=[abaa]。二者的最长公共子串为[aba],长度为3。

子序列是不要求连续的,字串必须是连续的。

思路与代码:

1、简单思想:

  • 遍历两个字符串X、Y,分别比较X的字串与Y的字串,求出最长的公共字串。
  • 设X长度为m,Y长度为n,最长公共字串长度为len,则时间复杂度为O(m*n*len),空间复杂度为O(1)
#include <iostream>
#include <vector> using namespace std; int getComLen(char *str1,char *str2){
int len=;
while(*str1 && *str2){
if(*(str1++)==*(str2++))
len++;
}
return len;
} int LCS1(char *str1,int len1,char *str2,int len2){
int maxlen=; // max length of LCS
int maxIndex=; // start position of LCS
int len;
for(int i=;i<len1;i++){
for(int j=;j<len2;j++){
len=getComLen(str1+i,str2+j);
if(len>maxlen){
maxlen=len;
maxIndex=i;
}
}
}
cout<<"Length of Longest Common Substring: "<<maxlen<<endl;
cout<<"LCS is: ";
for(int i=maxIndex;i<maxIndex+maxlen;i++)
cout<<str1[i];
cout<<endl;
return maxlen;
}
int main()
{
char str1[]="Chinese";
char str2[]="Chienglish";
int len1=sizeof(str1)/sizeof(str1[])-;
int len2=sizeof(str2)/sizeof(str2[])-;
cout << LCS1(str1,len1,str2,len2) << endl;
return ;
}

2、动态规划思想:

  • 与最长字符子序列一样,最长字符字串一样可以通过动态规划来求解,不一样的是,字串是连续的。
  • 假设dp[i][j]来表示以x[i]、y[j]结尾的公共子串长度(不是最长,最长的字串长度需要通过比较得到),由于字串连续,x[i]和y[i]要么与前面的前面的公共字串构成新的字串,要么不能构成公共字串。
  • 公共字串长度的状态转移方程如下:

初始状态:dp[i][j]=0 if i==0 || j==0

转移方程:dp[i][j] = dp[i-1][j-1]+1 if x[i-1]==y[j-1]

dp[i][j] = 0 if x[i-1]!=y[j-1]

  • 最长公共字串长度以及最长公共字串,需要在求公共字串长度的过程中通过比较并记录下来,具体参考代码。
  • 设X长度为m,Y长度为n,最长公共字串长度为len,则时间复杂度为O(m*n),空间复杂度为O(m*n)
#include <iostream>
#include <vector> using namespace std; // dynamic programming
int LCS2(char *str1,int len1,char *str2,int len2){
vector<vector<int> > dp(len1+,vector<int>(len2+,));
int maxlen=; // max length of LCS
int maxIndex=; // start position of LCS
for(int i=;i<=len1;i++){
for(int j=;j<=len2;j++){
if(i== || j==)
dp[i][j]=;
else{
if(str1[i-]==str2[j-])
dp[i][j]=dp[i-][j-]+;
} if(dp[i][j]>maxlen){
maxlen=dp[i][j];
maxIndex=i-maxlen+;
}
}
}
cout<<"Length of Longest Common Substring: "<<maxlen<<endl;
cout<<"LCS is: ";
for(int i=maxIndex-;i<maxIndex-+maxlen;i++)
cout<<str1[i];
cout<<endl;
return maxlen;
} int main()
{
char str1[]="Chinese";
char str2[]="Chienglish";
int len1=sizeof(str1)/sizeof(str1[])-;
int len2=sizeof(str2)/sizeof(str2[])-;
cout << LCS2(str1,len1,str2,len2) << endl;
return ;
}

(字符串)最长公共字串(Longest-Common-SubString,LCS)的更多相关文章

  1. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  2. 最长公共子序列(LCS)问题 Longest Common Subsequence 与最长公告字串 longest common substr

    问题描述:字符序列的子序列是指从给定字符序列中随意地(不一定连续)去掉若干个字符(可能一个也不去掉)后所形成的字符序列.令给定的字符序列X=“x0,x1,…,xm-1”,序列Y=“y0,y1,…,yk ...

  3. 最长公共子串算法(Longest Common Substring)

    给两个字符串,求两个字符串的最长子串 (例如:"abc""xyz"的最长子串为空字符串,"abcde"和"bcde"的最 ...

  4. 动态规划 ---- 最长公共子序列(Longest Common Subsequence, LCS)

    分析: 完整代码: // 最长公共子序列 #include <stdio.h> #include <algorithm> using namespace std; ; char ...

  5. 最长公共子序列与最长公共字串 (dp)转载http://blog.csdn.net/u012102306/article/details/53184446

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  6. 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, ...

  7. Longest Common Substring($LCS$)

    Longest Common Substring(\(LCS\)) 什么是子序列? 子序列就是某一个序列的不连续的一部分. 如图, \(abcde\)就是图中序列的一个子序列. 公共子序列 公共子序列 ...

  8. 最长公共字串算法, 文本比较算法, longest common subsequence(LCS) algorithm

    ''' merge two configure files, basic file is aFile insert the added content of bFile compare to aFil ...

  9. poj 3080 kmp求解多个字符串的最长公共字串,(数据小,有点小暴力 16ms)

    Blue Jeans Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 14113   Accepted: 6260 Descr ...

随机推荐

  1. c++ 时间相关的类型

    关于时间转换可以参考以下博客: https://www.jianshu.com/p/80de04b41c31 https://www.cnblogs.com/qicosmos/p/3642712.ht ...

  2. 【assembly】用汇编写的一个BMP图片读取器

    ;----------------------------- ;文件满足256色调的 ;----------------------------- Stack    Segment           ...

  3. hdu 4463 第37届ACM/ICPC杭州赛区K题 最小生成树

    题意:给坐标系上的一些点,其中有两个点已经连了一条边,求最小生成树的值 将已连接的两点权值置为0,这样一定能加入最小生成树里 最后的结果加上这两点的距离即为所求 #include<cstdio& ...

  4. python开发_tkinter_修改tkinter窗口的红色图标'Tk'

    学过java的swing可能知道,在创建一个窗口的时候,窗口的左上角是一个咖啡图标 如下图所示: 在python中,tkinter模块生成的窗口左上角是一个:Tk字样的图标(Tk为tkinter的缩写 ...

  5. BZOJ 4408: [Fjoi 2016]神秘数 可持久化线段树

    4408: [Fjoi 2016]神秘数 题目连接: http://www.lydsy.com/JudgeOnline/problem.php?id=4408 Description 一个可重复数字集 ...

  6. Codeforces Round #297 (Div. 2)A. Vitaliy and Pie 水题

    Codeforces Round #297 (Div. 2)A. Vitaliy and Pie Time Limit: 2 Sec  Memory Limit: 256 MBSubmit: xxx  ...

  7. 读书笔记_Effective_C++_条款三十八:通过复合塑模出has-a或者is-implemented-in-terms-of

    如果说public是一种is-a的关系的话,那么复合就是has-a的关系.直观来说,复合就是在一个类中采用其他类的对象作为自身的成员变量,可以举个例子,像下面这样: class Person { pr ...

  8. MySQL的mysql.sock文件作用(转)

    mysql.sock是可用于本地服务器的套接字文件.它只是另一种连接机制. 不包含任何数据,但仅用于从客户端到本地服务器来进行交换数据.

  9. Android 菜单键和返回键互换

    打开RE管理器找到system/usr/keylayout/ 长按qwerty.kl选择以文本编辑器查看 将里面的MENU和BACK全部替换掉 保存,退出管理器,重启手机,菜单键和返回键的位置就调换过 ...

  10. hdu2158

    最短区间版大家来找碴 Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Total ...