AcWing 897. 最长公共子序列 线性dp
#include <iostream> #include <algorithm> using namespace std; const int N = 1010; int n, m; char a[N], b[N]; int f[N][N]; int main() { scanf("%d%d", &n, &m); scanf("%s%s", 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]);//看一下第i和第j个字母有没有出现 //i-1,j-1 是包含在上面两种情况中的 if (a[i] == b[j]) f[i][j] = max(f[i][j], f[i - 1][j - 1] + 1); } printf("%d\n", f[n][m]); return 0; }