## See LCS again

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
//函数lower_bound()在first和last中的前闭后开区间进行二分查找，
//返回大于或等于val的第一个元素位置。如果所有元素都小于val，
//则返回last的位置，且last的位置是越界的！返回查找元素的第一个
//可安插位置，也就是“元素值>=查找值”的第一个元素的位置


#include<stdio.h>
#include<string.h>
#include<algorithm>
#define N 100010
using namespace std;
int a[N];
int b[N];
int dp[N];
int main()
{
int n,m,i,j;
int x;
while(scanf("%d%d",&n,&m)!=EOF)
{
memset(dp,0,sizeof(dp));
for(i=1;i<=n;i++)
{
scanf("%d",&x);
dp[x]=i;
}
int r=0;
for(i=1;i<=m;i++)
{
scanf("%d",&x);
if(dp[x])
b[r++]=dp[x];
}
int p=0;
dp[p++]=b[0];
for(i=1;i<r;i++)
{
if(dp[p-1]<b[i])
dp[p++]=b[i];
else
{
x=lower_bound(dp,dp+p,b[i])-dp;
dp[x]=b[i];
}
}
printf("%d\n",p);
}
return 0;
}

#### NYOJ 760 See LCS again

2014-03-20 10:37:29

#### NYOJ 760 See LCS again

2014-03-21 08:30:41

#### Nyoj 760 See LCS again [Lcs]

2014-08-05 09:52:25

#### nyoj 760 See LCS again 最长公共子序列

2016-08-01 10:15:59

#### NYOJ 题目760 See LCS again（LIS求LCS）

2015-02-01 00:44:12

#### NYOJ 760 - See LCS again(LCS转LIS)

2014-07-08 22:03:25

#### 【CF 578D】LCS again

2016-07-20 21:04:33

#### NYOJ 760 See LCS again(基础dp+哈希表)(复习)

2018-03-18 16:19:27

#### nyoj 回文字符串题解

2014-05-10 21:41:10

#### NYOJ760又见LCS

2016-09-18 18:03:36

## 不良信息举报

nyoj See LCS again 760 （LCS&&LIS） 好题