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