UVa 10635 王子和公主(LCS转LIS)

https://vjudge.net/problem/UVA-10635

题意:

有两个长度分别为p+1和q+1的序列,每个序列中的各个元素互不相同,且都是1~n^2之间的整数。两个序列的第一个元素均为1,。求出A和B的最长公共子序列长度。

思路:
因为序列中元素各不相同,所以我们可以把A重新编号为{1,2,3,4,5...},也就是相当于映射,之后B根据A映射,最后在B中找LIS即可。

 #include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<vector>
#include<stack>
#include<queue>
#include<cmath>
#include<map>
using namespace std;
typedef long long LL;
typedef pair<int,int> pll;
const int INF=0x3f3f3f3f;
const int maxn=*; int n,p,q;
int num[maxn],s[maxn];
int d[maxn],g[maxn]; int main()
{
//freopen("D:\\input.txt","r",stdin);
int kase=;
int T;
scanf("%d",&T);
while(T--)
{
memset(num,,sizeof(num));
scanf("%d%d%d",&n,&p,&q); for(int i=;i<=p+;i++)
{
int x;
scanf("%d",&x);
num[x]=i;
} int cnt=;
for(int i=;i<q+;i++)
{
int x;
scanf("%d",&x);
if(num[x]) s[cnt++]=num[x];
} int ans=;
for(int i=;i<=cnt;i++) g[i]=INF;
for(int i=;i<cnt;i++)
{
int k=lower_bound(g+,g+cnt+,s[i])-g;
d[i]=k;
g[k]=s[i];
ans=max(ans,d[i]);
}
printf("Case %d: %d\n",++kase,ans);
}
return ;
}
上一篇:POJ1038 - Bugs Integrated, Inc.(状态压缩DP)


下一篇:(原创)基于FPGA的调光流水灯(Verilog,CPLD/FPGA)