UVA10635 Prince and Princess - LCS转LIS
题目大意:
给两个序列,数均在\([1, n*n]\),求最长公共子序列。
题目分析:
若用传统的\(LCS\)显然会炸,因为数字均在\([1, n * n]\),若序列a的数的位置\(1~n\),数列b变为b[i]在a中出现的位置,求出LIS即可。\(o(n log n)\)
code
#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
//#include<vector>
using namespace std;
const int N = 1000, OO = 0x3f3f3f3f;
int T, a[N * N], b[N * N], has[N * N], f[N * N], n, p, q;
int main(){
scanf("%d", &T);
int k = 0;
while(T--){
scanf("%d%d%d", &n, &p, &q);
memset(has, 0, sizeof has);
for(int i = 1; i <= p + 1; i++) scanf("%d", &a[i]), has[a[i]] = i;
for(int i = 1; i <= q + 1; i++) scanf("%d", &b[i]), b[i] = has[b[i]];
// for(int i = 1; i <= q + 1; i++) cout << b[i] << " "; cout<<endl;
memset(f, OO, sizeof f);
for(int i = 1; i <= q + 1; i++){
if(!b[i]) continue;
*lower_bound(f + 1, f + q + 1 + 1, b[i]) = b[i];
}
// for(int i = 1; i <= q + 1; i++) cout << f[i] << " "; cout<<endl;
printf("Case %d: %d\n", ++k, lower_bound(f + 1, f + q + 1 + 1, OO) - (f + 1));
}
}