时间复杂度n^2
debug过程
1 #include <bits/stdc++.h> 2 using namespace std; 3 const int N = 1010; 4 char a[N], b[N]; 5 int dp[N][N]; 6 int main() { 7 int n, m; 8 cin >> n >> m; 9 cin >> a + 1 >> b + 1; 10 for (int i = 1; i <= n; i++) { 11 for (int j = 1; j <= m ; j++) { 12 dp[i][j] = max(dp[i - 1][j], dp[i][j - 1]); 13 if (a[i] == b[j]) { 14 dp[i][j] = max(dp[i][j], dp[i - 1][j - 1] + 1); 15 } 16 } 17 } 18 cout << dp[n][m] << endl; 19 return 0; 20 }