//最长公共子序列(记住公式即可答出)
//公式: 0 ; i=0 || j=0
// C[i][j] = { C[i-1][j-1] ; str[i] == str[j]
// max(C[i-1][j],C[i][j-1]; str[i] =/= str[j]
int lengest(char* str1, char* str2)
{
int len1 = strlen(str1);
int len2 = strlen(str2);
int** Tmp = new int*[len1+1];
for(int i=0; i<=len1; ++i)
Tmp[i] = new int[len2+1];
for(int i=0; i<=len1; ++i)
Tmp[i][0] = 0;
for(int i=0; i<len2; ++i)
Tmp[0][i] = 0;
for(int i=1; i<=len1; ++i)
{
for(int j=1; j<=len2; ++j)
{
if(str1[i-1] != str2[j-1])
Tmp[i][j] = Tmp[i-1][j]>Tmp[i][j-1] ? Tmp[i-1][j]:Tmp[i][j-1];
else
Tmp[i][j] = Tmp[i-1][j-1]+1;
cout<<Tmp[i][j]<<' ';
}
cout<<endl;
}
int val = Tmp[len1][len2];
for(int i=0; i<=len1; ++i)
delete []Tmp[i];
delete []Tmp;
return val;
}
最长公共子序列
最新推荐文章于 2024-09-20 10:50:16 发布