思路:用map映射,二分查找;
描述
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_胡仁东
</pre><img src="https://img-blog.csdn.net/20160501142744308?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center" alt="" /></p><p>代码:</p><p><pre name="code" class="cpp">/*See LCS again
map+二分查找;500ms过
#include <iostream>
#include <cstring>
#include <iomanip>
#include <string>
#include <algorithm>
#include <iomanip>
#include <map>
#include <stdio.h>
using namespace std;
int dp[100005];
int dis[100005];
int len;
map<string,int>p;
int serch(int x)
{
int l=0,r=len;
while(l<=r)
{
int mid=(l+r)/2;
if(dp[mid]==x) return mid;
else if(x<dp[mid]) r=mid-1;
else l=mid+1;
}
return l;
}
int main()
{
int i,j,k,m,n;
string st;
while(scanf("%d%d",&n,&m)!=EOF)
{
for(i=1;i<=n;i++)
{
cin>>st;
p[st]=i;//字符串映射的值为输入的是第几个;
}
dp[0]=0;//初值,因为映射的最小值为1;
len=0;//开始时相同的子序列为0;
for(i=1;i<=m;i++)
{
cin>>st;
if(!p[st])continue;//如果没映射值,表示第一个串中没有出现过当前的数字;
k=serch(p[st]);//查找以st结尾的公共的子序列;
dp[k]=p[st];//更新;
if(k>len)len=k; //如果长度比len大,对len更新;
}
cout<<len<<endl;
p.clear();//清空;
}
return 0;
}*/