UVA - 10635 Prince and Princess

本文探讨了如何通过将LCS转化为LIS来解决最长公共子序列问题,并利用LIS的O(nlogn)算法实现高效的解决方案。通过记录序列A的下标并在序列B中对应填写,随后计算LIS,最终确定最长公共子序列的长度。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

题意:求最长公共子序列,这题涉及到将LCS转化为LIS,不然会超时,用LIS的o(nlogn)的算法,很容易理解:记录A序列的下标,然后依次填写在B序列对应的数中,然后求LIS

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
const int MAXN = 70000;
const int INF = 1<<30;

int g[MAXN];
int m[MAXN];
int d[MAXN],G[MAXN];

int main(){
    int t;
    scanf("%d",&t);
    for(int cas = 1; cas <= t; cas++){
        int n,q,p;
        scanf("%d%d%d",&n,&p,&q);

        memset(g,0,sizeof(g));
        int a;
        for(int i = 1; i <= p+1; i++){
            scanf("%d",&a);
            g[a]=i;
        }
        int len=0;
        for(int i = 1; i <= q + 1 ;i++){
            scanf("%d",&a);
            if(g[a])    
                m[len++]=g[a];
        }
        int ans=0;
        for(int i = 0; i <= len; i++) G[i]=INF;
        for(int i = 0; i < len ; i++)
        {
            int k=lower_bound(G,G+len,m[i])-G;
            d[i]=k+1;
            G[k]=m[i];
            ans=max(d[i],ans);
        }
        printf("Case %d: %d\n",cas,ans);
    }
    return 0;
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值