0
1
mirror of https://git.sb/baoshuo/OI-codes.git synced 2024-09-16 20:05:26 +00:00

P1439 【模板】最长公共子序列

https://www.luogu.com.cn/record/94751747
This commit is contained in:
Baoshuo Ren 2022-11-20 11:16:23 +08:00
parent ef8275fc7a
commit 7b3f37a540
Signed by: baoshuo
GPG Key ID: 00CB9680AB29F51A

View File

@ -1,29 +1,42 @@
#include <iostream>
#include <algorithm>
using std::cin;
using std::cout;
using std::endl;
const char endl = '\n';
const int N = 10005;
const int N = 1e5 + 5;
int n, a[N], b[N], f[N][N];
int n, cnt, a[N], b[N], c[N], p[N], f[N];
int main() {
std::ios::sync_with_stdio(false);
cin.tie(nullptr);
cin >> n;
for (int i = 1; i <= n; i++) {
cin >> a[i];
p[a[i]] = 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);
}
if (p[b[i]] > c[cnt]) {
c[++cnt] = p[b[i]];
f[i] = cnt;
} else {
int k = std::lower_bound(c + 1, c + cnt + 1, p[b[i]]) - c;
c[k] = p[b[i]];
f[i] = k;
}
}
cout << f[n][n] << endl;
cout << cnt << endl;
return 0;
}