#include <iostream>
using namespace std;
char str1[1001],str2[1001];
int len[1001][1001];
int max(int a,int b)
{
return a > b ? a : b;
}
int dp(int l1,int l2)
{
int i ,j;
memset(len,0,sizeof(len));
for(i=0;i<=l1;i++)
len[i][0]=0;
for(i=0;i<=l2;i++)
len[0][i]=0;
for(i = 1; i <= l1; i++)
{
for (j = 1; j <= l2;j++)
{
if (str1[i-1] == str2[j-1]) len[i][j] = len[i-1][j-1]+1;
else if (len[i-1][j] >= len[i][j-1]) len[i][j] = len[i-1][j];
else len[i][j] = len[i][j-1];
}
}
return (max(l1, l2)-len[l1][l2]);
}
int main ()
{
int l1, l2;
while (scanf("%d%s%d%s",&l1,str1,&l2,str2)!=EOF)
{
cout<<dp(l1,l2)<<endl;
}
return 0;
}
这里有个问题,为什么LCS用滚动数组不好使呢?希望看到的牛人能指点一二!!!
转载于:https://www.cnblogs.com/Chinese-Coder-Clarence/articles/2039244.html