题意:给两个序列(长度达10^5,序列内的数都不同且都小于10^5),求这两个序列的最长公共序列的长度
2015-06-02 14:06
381 查看
#include <iostream> #include <cstdio> #include <algorithm> #include <cmath> #include <cstring> #include <string> #include <vector> #include <queue> #include <map> #define LL long long #define N 100100 using namespace std; int a , b , dp ; //nyoj See LCS again /* 题意:给两个序列(长度达10^5,序列内的数都不同且都小于10^5),求这两个序列的最长公共序列的长度 解法:预处理,把相同的项记录其数组位置,将题目转化为求这个数组的最长单调递增序列的长度(用二分法优化) */ int binarysearch(int key, int len) { int l=0, r=len-1, mid; while(l<=r) { mid=(l+r)>>1; if(dp[mid]>key) { r=mid-1; } else if(dp[mid]<key) { l=mid+1; } else return mid; } return l; } int main() { int n, m, t, j, k, l; while(scanf("%d%d", &n, &m)!=EOF) { memset(a, 0, sizeof(a)); for(t=1; t<=n; ++t) { scanf("%d", &k); a[k]=t; } for(t=j=0; t<m; ++t) { scanf("%d", &k); if(a[k]) { b[j++]=a[k]; } } dp[0]=b[0]; for(t=l=1; t<j; ++t) { k=binarysearch(b[t], l); dp[k]=b[t]; if(k+1>l) l=k+1; } printf("%d\n", l); } return 0; }
相关文章推荐
- SAT阅读考试的特点及应对策略
- 基本树形dp及例题
- 读写xml笔记
- Eclipse和PyDev搭建完美Python开发环境(Windows篇)
- c#深拷贝的一个方法
- SQL Server利用RowNumber()内置函数与Over关键字实现通用分页存储过程(支持单表或多表结查集分页)
- 【Hibernate学习笔记-5.1】使用@Transient修饰不想持久保存的属性
- mysql权限审核
- 编程十年,工作八年之个人感悟
- [leetcode] Plus One
- ios 流媒体
- 时间同步
- 在hpyer-v中导出和导入虚拟机------克隆虚拟机
- VIM大小写的变换
- Oracle字符编码与汉字存储长度的处理
- 智力题(第三集)
- PHP时间
- Unity3d 使用UGUI实现button其他事件处理
- MySQL slave状态之Seconds_Behind_Master
- nexus安装实例