nyoj See LCS again 760 (LCS&&LIS) 好题

来源:互联网 发布:刘昱旻 知乎 编辑:程序博客网 时间:2024/04/28 20:23

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 41 2 6 5 41 3 5 4
样例输出
3
//函数lower_bound()在first和last中的前闭后开区间进行二分查找,//返回大于或等于val的第一个元素位置。如果所有元素都小于val,//则返回last的位置,且last的位置是越界的!返回查找元素的第一个//可安插位置,也就是“元素值>=查找值”的第一个元素的位置
#include<stdio.h>#include<string.h>#include<algorithm>#define N 100010using 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;}


0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 期望薪资谈低了怎么办 请年假公司不批怎么办 期望工资填低了怎么办 面试工资要高了怎么办 找工作期望薪资写低了怎么办 期望工资写少了怎么办 不给工人发工资怎么办 天亮了怎么办我好想你 亲爱的我想你我怎么办 人在澳大利亚悉尼找不到了怎么办 红米手机忘记手势密码怎么办 捡到苹果手机怎么办才能自己用 日语会读不会写怎么办 手术后nbp过低怎么办 我的手破了怎么办英文 平板手机屏坏了怎么办 他很优秀我该怎么办 洗澡的花洒漏水怎么办 高三了文科成绩很差怎么办 骑缝章最后一页没盖全怎么办 机票取早了没有登机口怎么办 机票早订比晚订贵怎么办? 孩子考差了父母怎么办 保险公司不给业务员办退司怎么办 我不习惯没有你我怎么办 锁坏了打不开了怎么办 要上班老人生病无人照顾怎么办 苹果手机一直说英文怎么办 公司很抠门怎么办英文怎么说 过了截港时间怎么办 截关日期是假日怎么办 恒温阀冷水进水堵塞怎么办 缺氧液泵管道堵塞怎么办 货物包装大集装箱装不下怎么办 微信收藏的视频格式错误怎么办 乙方被刑拘房租未付清怎么办 房贷银行卡号弄错怎么办 社保卡号弄错了怎么办 社保名字写错了怎么办 档案和身份证年龄姓名不一样怎么办 档案年龄与身份证年龄不一样怎么办