题目:uva 10635 - Prince and Princess
题意:求最长公共子序列(每个元素唯一)
思路:LCS做会超时,转换成LIS
给出两个序列A,B,对于A的序列给出编号,在B中将对应的转换成编号,不存在的标记为0,然后求编号的最长上升子序列,O(nlgn)
#include <cstring>
#include <algorithm>
#include <cmath>
#include <iostream>
#include <cstdio>
#include <vector>
#include <map>
using namespace std;
const int maxn = (260*260);
#define inf 1000000
int a[maxn];
vector<int>v;
int dp[maxn];
map<int,int>mp;
int LIS(vector<int>v)
{
fill(dp,dp+v.size(),inf);
for(int i=0;i<v.size();i++)
*lower_bound(dp,dp+v.size(),v[i])=v[i];
return lower_bound(dp,dp+v.size(),inf)-dp;
}
int main()
{
int t;
scanf("%d",&t);
for(int cas=1;cas<=t;cas++)
{
v.clear();
mp.clear();
int n,p,q;
scanf("%d%d%d",&n,&p,&q);
for(int i=1;i<=p+1;i++)
{
scanf("%d",&a[i]);
mp[a[i]]=i;
}
for(int i=1;i<=q+1;i++)
{
scanf("%d",&a[i]);
v.push_back(mp[a[i]]);
}
printf("Case %d: %d\n",cas,LIS(v));
}
return 0;
}