1 #include <iostream> 2 #include <stdio.h> 3 #include <algorithm> 4 5 using namespace std; 6 7 int dp[105][105]; 8 9 int main() 10 { 11 string s1, s2; 12 while(cin >> s1 >> s2) 13 { 14 int L1 = s1.length(); 15 int L2 = s2.length(); // 依次求得两个字符串的长度 16 for(int i = 0; i <= L1; ++i) dp[i][0] = 0; 17 for(int j = 0; j <= L2; ++j) dp[0][j] = 0; // 设初始值为0 18 for(int i = 1; i <= L1; ++i) 19 { 20 for(int j = 1; j <= L2; ++j) // 双重循环依次求得每个dp[i][j]的值 21 { 22 if(s1[i-1] == s2[j-1]) // 因为字符串数组下标从0开始,所以第i个字符位置为s1[i-1],若两个字符相等 23 dp[i][j] = dp[i-1][j-1] + 1; 24 else // 若当前两个字符不相等 25 dp[i][j] = max(dp[i-1][j], dp[i][j-1]); 26 } 27 } 28 29 cout << dp[L1][L2] << endl; 30 } 31 32 return 0; 33 }