如下图
#include<iostream> #include<cstring> #include<algorithm> #include<cstdio> using namespace std; int dp[1010][1010]; char list[1010]; char ans[1010]; int main() { while (~scanf("%s %s", list, ans)) { int len1 = strlen(list); int len2 = strlen(ans); memset(dp, 0, sizeof(dp)); for (int i = 1; i <= len1; i++) { for (int j = 1; j <= len2; j++) { if (ans[j - 1] == list[i - 1]) { dp[i][j] = dp[i - 1][j - 1] + 1; } else { dp[i][j] = max(dp[i][j - 1], dp[i - 1][j]); } } } cout << dp[len1][len2] << endl; } return 0; }