您的位置:首页 > 其它

最长公共字串与最长公共子序列

2016-10-06 10:21 197 查看
最长公共子串(Longest Common Substirng)和最长公共子序列(Longest Common Subsequence,LCS)的区别为:子串是串的一个连续的部分,子序列则是从不改变序列的顺序,而从序列中去掉任意的元素而获得新的序列;也就是说,子串中字符的位置必须是连续的,子序列则可以不必连续。

如:

abcdefghijklmnop

abcsafjklmnopqrstuvw

最长公共子串为:jklmnop

最长公共子序列为:abcfjklmnop

这两个都可以用动态规划,只是状态转移方程有点区别

最长公共子串是: dp[i][j] – 表示以str1[i]和str2[j]为结尾的最长公共子串 当str1[i] == str2[j]时,dp[i][j] = dp[i-1][j-1] + 1; 否则,dp[i][j] = 0;

最长公共子序列是:

dp[i][j] – 表示子串str1[0…i]和子串str[0…j]的最长公共子序列

当str1[i] == str2[j]时,dp[i][j] = dp[i-1][j-1] + 1;

否则,dp[i][j] = max(dp[i-1][j], dp[i][j-1]);

最优解为dp[len1-1][len2-1];

//最长公共字串
#include <iostream>
#include <cstring>
using namespace std;

#define MAX_N 500

int main()
{
char pFirstStr[MAX_N],pSecondStr[MAX_N];
while(cin>>pFirstStr>>pSecondStr)
{
int len1=strlen(pFirstStr);
int len2=strlen(pSecondStr);
int maxlen=0;
int dp[MAX_N][MAX_N]={0};
int end=0;
for(int i=0;i<len1;i++)
{
for(int j=0;j<len2;j++)
{
if(pFirstStr[i]==pSecondStr[j])
{
if(i==0||j==0)
dp[i][j]=1;
else
dp[i][j]=dp[i-1][j-1]+1;
}
if(dp[i][j]>maxlen)
{
maxlen=dp[i][j];
end=i;
}
}
}
for(int i=end-maxlen+1;i<=end;i++)
cout<<pFirstStr[i];
cout<<endl;
}
return 0;
}


最长公共子序列:



//最长公共子序列
//只能打印一个最长公共子序列
#include <iostream>
using namespace std;

const int X = 100, Y = 100;        //串的最大长度
char result[X+1];                    //用于保存结果
int count=0;                        //用于保存公共最长公共子串的个数

/*功能:计算最优值
*参数:
*        x:字符串x
*        y:字符串y
*        b:标志数组
*        xlen:字符串x的长度
*        ylen:字符串y的长度
*返回值:最长公共子序列的长度
*
*/
int Lcs_Length(string x, string y, int b[][Y+1],int xlen,int ylen)
{
int i = 0;
int j = 0;

int c[X+1][Y+1];
for (i = 0; i<=xlen; i++)
{
c[i][0]=0;
}
for (i = 0; i <= ylen; i++ )
{
c[0][i]=0;
}
for (i = 1; i <= xlen; i++)
{

for (j = 1; j <= ylen; j++)
{
if (x[i - 1] == y[j - 1])
{
c[i][j] = c[i-1][j-1]+1;
b[i][j] = 1;   //1->"↖"
}
else
if (c[i-1][j] > c[i][j-1])
{
c[i][j] = c[i-1][j];
b[i][j] = 2;   //2->"↑"
}
else
if(c[i-1][j] <= c[i][j-1])
{
c[i][j] = c[i][j-1];
b[i][j] = 3; //3->"←"
}

}
}
cout << "计算最优值效果图如下所示:" << endl;
for(i = 1; i <= xlen; i++)
{
for(j = 1; j < ylen; j++)
{
cout << c[i][j] << " ";
}
cout << endl;
}
return c[xlen][ylen];
}

void Display_Lcs(int i, int j, string x, int b[][Y+1],int current_Len)
{
if (i ==0 || j==0)
{
return;
}
if(b[i][j]== 1)
{
current_Len--;
result[current_Len]=x[i- 1];
Display_Lcs(i-1, j-1, x, b, current_Len);
}
else
{
if(b[i][j] == 2)
{
Display_Lcs(i-1, j, x, b, current_Len);
}
else
{
if(b[i][j]==3)
{
Display_Lcs(i, j-1, x, b, current_Len);
}
else
{
Display_Lcs(i-1,j,x,b, current_Len);
}
}
}
}

int main(int argc, char* argv[])
{
string x = "ABCBDAB";
string y = "BDCABA";
int xlen = x.length();
int ylen = y.length();

int b[X + 1][Y + 1];

int lcs_max_len = Lcs_Length( x, y, b, xlen,ylen );
cout << lcs_max_len << endl;

Display_Lcs( xlen, ylen, x, b, lcs_max_len );

//打印结果如下所示
for(int i = 0; i < lcs_max_len; i++)
{
cout << result[i];
}
cout << endl;
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: