0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-11-23 20:28:48 +00:00

(50pts) P1439 【模板】最长公共子序列

R67364851
This commit is contained in:
Baoshuo Ren 2022-03-04 15:22:30 +08:00
parent e8747dac5b
commit b50080a6cc
Signed by: baoshuo
GPG Key ID: 70F90A673FB1AB68

29
Luogu/P1439/P1439.cpp Normal file
View File

@ -0,0 +1,29 @@
#include <iostream>
using std::cin;
using std::cout;
using std::endl;
const int N = 10005;
int n, a[N], b[N], f[N][N];
int main() {
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
}
for (int i = 1; i <= n; i++) {
cin >> b[i];
}
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= n; j++) {
f[i][j] = std::max(f[i - 1][j], f[i][j - 1]);
if (a[i] == b[j]) {
f[i][j] = std::max(f[i][j], f[i - 1][j - 1] + 1);
}
}
}
cout << f[n][n] << endl;
return 0;
}