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.

31 lines
989 B

2 years ago
#include <bits/stdc++.h>
using namespace std;
const int N = 3010;
int a[N], b[N];
int f[N][N];
int res;
// O(n^2)
1 year ago
// f[i][j]—集合:考虑 a 中前 i 个数字b 中前 j 个数字 ,且当前以 b[j] 结尾的子序列的方案
2 years ago
int main() {
int n;
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++) {
1 year ago
int mx = 1; // 如果a[i]==b[j],那么LICS最小是1.如果下面的循环中没有一个if被执行则mx没使上
2 years ago
for (int j = 1; j <= n; j++) {
1 year ago
f[i][j] = f[i - 1][j]; // 先继承过来现实含义即使a[i]!=b[j],那么最长长度不会因为i的增加而变化即f[i][j]=f[i-1][j]
1 year ago
if (a[i] == b[j]) f[i][j] = mx;
if (a[i] > b[j]) mx = max(mx, f[i - 1][j] + 1);
2 years ago
}
}
for (int i = 1; i <= n; i++) res = max(res, f[n][i]);
printf("%d\n", res);
return 0;
}