#include<iostream> #include<cstdio> #include<cstring> #include<algorithm> using namespace std; const int maxn = 10001; char s1[maxn], s2[maxn]; int dp[maxn][maxn]; int count(char* s1, char* s2) { int len1 = strlen(s1); int len2 = strlen(s2); for (int i = 0; i < len1 + 2; i++) { for (int j = 0; j < len2 + 2; j++) { dp[i][j] = 0; } } for (int i = 1; i <= len1; i++) { for (int j = 1; j <= len2; j++) { if (s1[i - 1] == s2[j - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); } } } return dp[len1][len2]; } /* int main() { char s1[maxn]; char s2[maxn]; cin >> s1; int len = strlen(s1); for (int i = 0; i < len;i++) { s2[i] = s1[len-1-i]; } s2[len] = s1[len]; cout << count(s1, s2) << endl; } */
原文:http://www.cnblogs.com/jinweiseu/p/5372970.html