uva 10635 Prince and Princess

来源:互联网 发布:excel破解密码软件 编辑:程序博客网 时间:2024/04/25 16:04

题意:求最长公共上升子序列。

n^2的算法会超时。考虑是最长公共子子序列可以看成最长上升序列来做(只不过是以上一个字符给出的大小关系,以前我不知道最长公共子序列时的做法,不过当前我并不知道最长上升子序列的olog(n)做法,所以没有意识到这点。),所以我用map建立了大小关系,然后进行跟一个nlog(n)的最长上升子序列就可以过了。

#include <iostream>#include <cstdio>#include <cstring>#include <map>#include <algorithm>using namespace std;map<int,int> hash;int cnt,dp[70000];void fun(int);int main(){    int t,t_cnt=0;    scanf("%d",&t);    while(t--)    {        cnt=-1;        hash.clear();        int k,n,m,temp;        scanf("%d%d%d",&k,&n,&m);        for(int i=0;i<n+1;i++)        {            scanf("%d",&temp);            hash.insert(pair<int,int>(temp,i));        }        for(int i=0;i<m+1;i++)        {            scanf("%d",&temp);            fun(hash[temp]);        }        printf("Case %d: %d\n",++t_cnt,cnt+1);    }    return 0;}void fun(int x){    if(cnt==-1||dp[cnt]<x) dp[++cnt]=x;    else    {        int pos=lower_bound(dp,dp+cnt,x)-dp;        dp[pos]=x;    }}


原创粉丝点击