题目链接:http://poj.org/problem?id=1458
分析:最大公共子序列模板
1 #include<iostream> 2 #include<sstream> 3 #include<cstdio> 4 #include<cstdlib> 5 #include<string> 6 #include<cstring> 7 #include<algorithm> 8 #include<functional> 9 #include<iomanip> 10 #include<numeric> 11 #include<cmath> 12 #include<queue> 13 #include<vector> 14 #include<set> 15 #include<cctype> 16 const double PI = acos(-1.0); 17 const int INF = 0x3f3f3f3f; 18 const int NINF = -INF - 1; 19 const int maxn = 1e3 + 5; 20 typedef long long ll; 21 #define MOD 1000000007 22 using namespace std; 23 char a[maxn], b[maxn]; 24 int dp[maxn][maxn]; 25 int main() 26 { 27 while (scanf("%s", &a) != EOF) 28 { 29 scanf("%s", &b); 30 memset(dp, 0, sizeof(dp)); 31 int len1 = strlen(a); 32 int len2 = strlen(b); 33 for (int i = 1; i <= len1; ++i) 34 { 35 for (int j = 1; j <= len2; ++j) 36 { 37 if (a[i - 1] == b[j - 1]) dp[i][j] = dp[i - 1][j - 1] + 1; 38 else dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); 39 } 40 } 41 printf("%d ", dp[len1][len2]); 42 } 43 return 0; 44 }