最长公共子序列的问题,不过看了其他的代码是把该问题想最长上升子序列转换的(LIS问题可以在nlogn时间内解决,不理解为何不是n^2)
代码如下:
#include <iostream>
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <math.h>
using namespace std;
#define LLEN 80000
#define INF 99999999
int a[LLEN], s[LLEN];
int g[LLEN];
int main() {
int t;
int n, p, q;
int x;
int ans;
scanf("%d", &t);
for(int cse = 1; cse <= t; cse++) {
ans = 0;
memset(a, 0, sizeof(a));
memset(s, 0, sizeof(s));
scanf("%d%d%d", &n, &p, &q);
for(int i = 1; i <= p+1; i++) {
scanf("%d", &x);
a[x] = i;
}
int k = 0;
for(int i = 0; i <= q; i++) {
scanf("%d", &x);
if(a[x]) {
s[k++] = a[x];
}
}
for(int i = 1; i <= k; i++) {
g[i] = INF;
}
for(int i = 0; i < k; i++) {
int index = lower_bound(g+1, g+k+1, s[i]) - g;
g[index] = s[i];
ans = max(index, ans);
}
printf("Case %d: %d\n", cse, ans);
}
return 0;
}