最长公共子序列求解问题。
1 #include<stdio.h> 2 #include<string.h> 3 const int maxn=1000; 4 char s1[maxn],s2[maxn]; 5 int c[maxn][maxn]; 6 int main() 7 { 8 while(scanf("%s%s",s1,s2)!=EOF){ 9 int len1=strlen(s1),len2=strlen(s2); 10 memset(c,0,sizeof(c)); 11 for(int i=len1;i>=1;i--) 12 s1[i]=s1[i-1]; 13 for(int i=len2;i>=1;i--) 14 s2[i]=s2[i-1]; 15 for(int i=1;i<=len1;i++) 16 for(int j=1;j<=len2;j++){ 17 if(s1[i]==s2[j]) 18 c[i][j]=c[i-1][j-1]+1; 19 else if(c[i][j-1]>c[i-1][j]) 20 c[i][j]=c[i][j-1]; 21 else 22 c[i][j]=c[i-1][j]; 23 } 24 printf("%d\n",c[len1][len2]); 25 } 26 return 0; 27 }
POJ - 1458 Common Subsequence,布布扣,bubuko.com
原文地址:http://www.cnblogs.com/BMESwimming/p/3854462.html