uva 10635 - Prince and Princess

题目: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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值