See LCS again
时间限制:
1000 ms | 内存限制:
65535 KB
难度:
3
-
描述
-
There are A, B two sequences, the number of elements in the sequence is n、m;
Each element in the sequence are different and less than 100000.
Calculate the length of the longest common subsequence of A and B.
-
输入
-
The input has multicases.Each test case consists of three lines;
The first line consist two integers n, m (1 < = n, m < = 100000);
The second line with n integers, expressed sequence A;
The third line with m integers, expressed sequence B;
输出
- For each set of test cases, output the length of the longest common subsequence of A and B, in a single line. 样例输入
-
5 4 1 2 6 5 4 1 3 5 4
样例输出
-
3
上传者
-
TC_胡仁东
#include<stdio.h> #include<string.h> int a[100010],b[100010],v[100010]; int n,m; int lis() { int l,r,mid,len,i; b[1]=a[1]; len=1; for(i=2;i<=m;i++) { l=1; r=len; if(b[len]<a[i]) { b[++len]=a[i]; } else { while(l<=r) { mid=(l+r)/2; if(b[mid]>=a[i]) r=mid-1; else l=mid+1; } b[l]=a[i]; } } return len; } int main() { //int n,m; while(scanf("%d%d",&n,&m)!=EOF) { int i,x,w=0; memset(v,-1,sizeof(v)); for(i=1;i<=n;i++) { scanf("%d",&x); v[x]=i; } for(i=1;i<=m;i++) { scanf("%d",&x); if(v[x]!=-1) w=1; a[i]=v[x]; } printf("%d\n",lis()); } }
ac代码
-
The input has multicases.Each test case consists of three lines;