题目描述
https://www.acwing.com/problem/content/899/
代码
#include <cstdio>
#include <iostream>
using namespace std;
const int N = 1010;
char a[N], b[N];
int f[N][N]; //f[i][j]表示a中前i个子序列和b中前j个子序列的最长公共子序列
int main()
{
int n, m;
cin >> n >> m >> a + 1 >> b + 1;
for(int i = 1; i <= n; i ++)
{
for(int j = 1; j <= m; j ++)
{
f[i][j] = max(f[i - 1][j], f[i][j - 1]);
if(a[i] == b[j]) f[i][j] = max(f[i][j], f[i - 1][j - 1] + 1);
//printf("f[%d][%d] = %d\n", i, j, f[i][j]);
}
// puts("");
}
printf("%d\n", f[n][m]);
return 0;
}