See LCS again

思路:用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;
}*/


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值