UVA10635 Prince and Princess

题意:已知两个序列里面没有重复的元素(l<50000),问两个序列的最大公共子序列

题解:lcs的话o(n×n)复杂度过不了,因为没有相同的元素,新数组记录序列里面的元素在第一个序列的rank再利用最大上升子序列求出就是最大公共子序列

#include <bits/stdc++.h>
#define ll long long
#define maxn 100100
using namespace std;
int a[maxn], b[maxn], dp[maxn];
map<int ,int >mp;
int main(){
    int n,q,p,temp,ans=0, T, num=1;
    scanf("%d", &T);
    while(T--){
        ans = 0;mp.clear();
        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", &b[i]), b[i] = mp[b[i]];
        memset(dp, 63, sizeof(dp));
        for(int i=1;i<=q+1;i++){
            temp = lower_bound(dp+1, dp+q+2, b[i])-dp;
            ans = max(temp, ans);
            dp[temp] = b[i];
        }
        printf("Case %d: %d\n", num++, ans);
    }
    return 0;
}

 

posted on 2017-08-27 22:07  2855669158  阅读(85)  评论(0编辑  收藏  举报

导航