链接:点击打开链接
题意:求两个串的最长公共子序列(两个串长度最大为250*250)
代码:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream>
#include <algorithm>
using namespace std;
const int INF=0x3f3f3f3f;
int a[100005],b[100005],dp[100005],pos[100005];
int main(){ //样例A={1,7,5,4,8,3,9},B={1,4,3,5,6,2,8,9}
int t,n,p,q,i,j,cas; //因为A,B中,每个数字只出现一次,因此对A,B中的
scanf("%d",&t); //数进行重新编号,编号后
for(cas=1;cas<=t;cas++){ //A={0,1,2,3,4,5,6},B={0,3,5,2,-1,-1,4,6}
scanf("%d%d%d",&n,&p,&q); //-1代表没出现过,因此就变成求数组B的LIS
memset(dp,INF,sizeof(dp)); //因此就从O(n*n)的LCS转换成O(n*logn)的LIS
memset(pos,-1,sizeof(pos));
for(i=0;i<p+1;i++){
scanf("%d",&a[i]);
pos[a[i]]=i;
}
for(i=0;i<q+1;i++){
scanf("%d",&b[i]);
b[i]=pos[b[i]];
}
for(i=0;i<q+1;i++)
*lower_bound(dp,dp+q+1,b[i])=b[i];
printf("Case %d: %d\n",cas,lower_bound(dp,dp+q+1,INF)-dp);
}
return 0;
}