UVA 10635 Prince and Princess【LCS 问题转换为 LIS】

题目链接:

http://acm.hust.edu.cn/vjudge/problem/visitOriginUrl.action?id=19051

题意:

有两个长度分别为 p+1 q+1 的由 1n2 之前的整数组成的序列,每个序列的元素各不相等,两个序列第一个元素均为1。求两个序列的最长公共子序列。

分析:

LCS 的复杂度为 O(pq) ,这题 p,q 最大为250 * 250,必T无疑。
注意题目说的每个序列的元素各不相等,那么就能保证我们可以把序列A的元素用 1p+1 重新进行赋值,把B中元素根据A的赋值找到对应的标号,用0表示没有出现过,那么问题就可以转化为 LIS 啦,时间复杂度 O(nlogn) ,可以过了。
有关 LIS 的内容这里有一个http://blog.csdn.net/yukizzz/article/details/50620631

代码:

/*************************************************************************
    > File Name: 10635.cpp
    > Author: jiangyuzhu
    > Mail: 834138558@qq.com 
    > Created Time: Sat 18 Jun 2016 08:57:43 PM CST
 ************************************************************************/

#include<iostream>
#include<cstdio>
#include<cstring>
#include<cmath>
#include<queue>
#include<stack>
using namespace std;
#define sa(n) scanf("%d", &(n));
typedef pair<int, int>p;
const int maxn = 250 + 5, mod = 1e9 + 7, oo = 0x3f3f3f3f;
int a[maxn * maxn], b[maxn * maxn], nb[maxn * maxn];
int pos[maxn * maxn];
int dp[maxn * maxn];
int main (void)
{
    int t;sa(t);    
    for(int kas = 1; kas <= t; kas++){
        int n, p, q;sa(n);sa(p);sa(q);
        memset(pos, 0, sizeof(pos));
        for(int i = 0; i <= p; i++){
            sa(a[i]);
            pos[a[i]] = i;
        }
        memset(nb, 0, sizeof(nb));
        for(int i = 0; i <= q; i++){
            sa(b[i]);
            nb[i] = pos[b[i]];
        }
        memset(dp, 0x3f, sizeof(dp));
        for(int i = 0; i <= q; i++){
            *lower_bound(dp, dp + q + 1, nb[i]) = nb[i];
        }
        int ans = lower_bound(dp, dp + q + 1, oo) - dp;
        printf("Case %d: %d\n", kas, ans);
    }
    return 0;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值