Prince and Princess UVA - 10635 (n*logn 的最长公共子序列)

思路:求两个串的最长公共子序列,将其转化成求最长上升子序列。

转化方法:将第一个串按顺序编号,将第二个串HASH,然后求hash后的串的最长上升子序列。

代码:

#include <bits/stdc++.h>
using namespace std;
const int maxn=1e5+7;
const int inf=0x3f3f3f3f;
typedef long long ll;
int n,p,q;
int a[maxn],b[maxn],Hash[maxn],L[maxn];
struct LIS
{
    int n,s[maxn];
    void init(){n=0;};
    int find(int x){return lower_bound(s,s+n,x)-s;}
    void modify(int pos,int val){s[pos]=val;n=max(n,pos+1);}
}in;
int main()
{
    #ifndef ONLINE_JUDGE
        freopen("in.txt","r",stdin);
        freopen("out.txt","w",stdout);
    #endif
    int T;
    cin>>T;
    int Case=0;
    while(T--)
    {
        scanf("%d%d%d",&n,&p,&q);
        for(int i=1;i<=p+1;i++)
        {
            int x;
            scanf("%d",&x);
            Hash[x]=i;
            a[i]=x;
        }
        for(int i=1;i<=q+1;i++)
        {
            int x;
            scanf("%d",&x);
            if(Hash[x]==0)
            {
                L[i]=0;
            }
            else
            {
                L[i]=Hash[x];
            }
        }
        in.init();
        int ans=0;
        for(int i=1;i<=q+1;i++)
        {
            int tmp=in.find(L[i]);
            in.modify(tmp,L[i]);
            ans=max(ans,tmp+1);
        }
        printf("Case %d: %d\n",++Case,ans);
    }
    return 0;
}

 

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值