最长公共子列,不要求输出路径就变得很简单。
#include <stdio.h>
#include <string.h>
int main()
{
char str1[505],str2[505];
int len1,len2,c[505][505];
while(scanf("%s",str1)!=EOF)
{
scanf("%s",str2);
len1 = strlen(str1);
len2 = strlen(str2);
memset(c,0,sizeof(c));
for(int i = 0 ; i < len1 ; i ++)
{
for(int j = 0 ; j < len2 ; j++)
{
if(str1[i] == str2[j])
{
c[i+1][j+1]=c[i][j]+1;
}
else
{
if(c[i][j+1]>=c[i+1][j])
{
c[i+1][j+1] = c[i][j+1];
}
else
{
c[i+1][j+1] = c[i+1][j];
}
}
}
}
printf("%d\n",c[len1][len2]);
}
return 0;
}