You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

22 lines
551 B

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int N = 1010;
int n, m;
char a[N], b[N];
int f[N][N];
int main() {
// 递推式出现f[i-1][j-1]如果i,j从0开始会出现负值所以下标从1开始
cin >> n >> m >> a + 1 >> b + 1;
for (int i = 1; i <= n; i++)
1 year ago
for (int j = 1; j <= m; j++) {
1 year ago
if (a[i] == b[j])
f[i][j] = f[i - 1][j - 1] + 1;
else
f[i][j] = max(f[i - 1][j], f[i][j - 1]);
1 year ago
}
2 years ago
printf("%d", f[n][m]);
return 0;
}