http://acm.hdu.edu.cn/showproblem.php?pid=1159
最长公共子序列
1 #include <stdio.h> 2 #include <algorithm> 3 #include <string.h> 4 using namespace std; 5 const int N=1010; 6 char s1[N],s2[N]; 7 int dp[N][N]; 8 9 int main() 10 { 11 while(~scanf("%s%s",s1,s2)) 12 { 13 int len1 = strlen(s1); 14 int len2 = strlen(s2); 15 for (int i = 0; i <= len1; i++) 16 dp[i][0] = 0; 17 for (int j = 0; j <= len2; j++) 18 dp[0][j] = 0; 19 for (int i = 1; i <= len1; i++) 20 { 21 for (int j = 1; j <= len2; j++) 22 { 23 if (s1[i-1]==s2[j-1]) 24 dp[i][j] = dp[i-1][j-1]+1; 25 else 26 dp[i][j] = max(dp[i-1][j],dp[i][j-1]); 27 } 28 } 29 printf("%d ",dp[len1][len2]); 30 } 31 return 0; 32 }
有关最长公共子序列的知识点:
http://blog.csdn.net/yysdsyl/article/details/4226630