【笔记】最长公共子序列

给出1-n的两个排列P1和P2,求它们的最长公共子序列。

(相异)

 

转化问题成最长下降子序列

//为了更好的利用之前的信息,便于更新,
//我们把原来记录的长度信息,改成如果有这个解,他匹配的最后的元素,的最前面的s1序列的位置是多少 

#include<cstdio>
#include<cstdlib>
#include<algorithm>
#include<cstring>
using namespace std;
int n,x;
const int N=100003;
int mp[N],f[N]; 


int main()
{
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%d",&x),mp[x]=i;
    //work
    int len=0; 
    memset(f,0x7f,sizeof(f));
    for(int i=1;i<=n;i++)
    {
        scanf("%d",&x);
        int pos=upper_bound(f+1,f+len+1,mp[x])-f;
        f[pos]=mp[x];
        
        if(pos>len) len++;
    }
    //output 
    printf("%d\n",len);
    return 0;
}

 

posted @ 2019-10-24 21:38  心若笺诗  阅读(114)  评论(0编辑  收藏  举报