UVA10635 Prince and Princess - LCS转LIS

传送门

题目大意:

给两个序列,数均在\([1, n*n]\),求最长公共子序列。

题目分析:

若用传统的\(LCS\)显然会炸,因为数字均在\([1, n * n]\),若序列a的数的位置\(1~n\),数列b变为b[i]在a中出现的位置,求出LIS即可。\(o(n log n)\)

code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<string>
//#include<vector>
using namespace std;

const int N = 1000, OO = 0x3f3f3f3f;
int T, a[N * N], b[N * N], has[N * N], f[N * N], n, p, q;

int main(){
    scanf("%d", &T);
    int k = 0;
    while(T--){
        scanf("%d%d%d", &n, &p, &q);
        memset(has, 0, sizeof has);
        for(int i = 1; i <= p + 1; i++) scanf("%d", &a[i]), has[a[i]] = i;
        for(int i = 1; i <= q + 1; i++) scanf("%d", &b[i]), b[i] = has[b[i]];
        // for(int i = 1; i <= q + 1; i++) cout << b[i] << " "; cout<<endl;
        memset(f, OO, sizeof f);
        for(int i = 1; i <= q + 1; i++){
            if(!b[i]) continue;
            *lower_bound(f + 1, f + q + 1 + 1, b[i]) = b[i];
        }
        // for(int i = 1; i <= q + 1; i++) cout << f[i] << " "; cout<<endl;
        printf("Case %d: %d\n", ++k, lower_bound(f + 1, f + q + 1 + 1, OO) - (f + 1));
    }
}

转载于:https://www.cnblogs.com/CzYoL/p/7658921.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值