题意:有两个长度分别为p+1和q+1的序列,每个序列中的各个元素互不相同,且都是1~n2的整数。两个序列的第一个元素均为1。求出A和B的最长公共子序列长度。

分析:

A = {1,7,5,4,8,3,9},B = {1,4,3,5,6,2,8,9}。

1、A中元素各不相同,因此将A中序列重新编号为1~p+1,即A中每个元素在A中是第几个出现的。

2、按照A中制定的编号原则给B重新编号,则B为{1,4,6,3,0,0,5,7},0表示这些元素在A中没有出现过。

3、新的A和B的LCS实际上就是新的B的LIS。LIS可在O(nlogn)内解决。

#include<cstdio>
#include<cstring>
#include<cstdlib>
#include<cctype>
#include<cmath>
#include<iostream>
#include<sstream>
#include<iterator>
#include<algorithm>
#include<string>
#include<vector>
#include<set>
#include<map>
#include<stack>
#include<deque>
#include<queue>
#include<list>
#define lowbit(x) (x & (-x))
const double eps = 1e-8;
inline int dcmp(double a, double b){
if(fabs(a - b) < eps) return 0;
return a > b ? 1 : -1;
}
typedef long long LL;
typedef unsigned long long ULL;
const int INT_INF = 0x3f3f3f3f;
const int INT_M_INF = 0x7f7f7f7f;
const LL LL_INF = 0x3f3f3f3f3f3f3f3f;
const LL LL_M_INF = 0x7f7f7f7f7f7f7f7f;
const int dr[] = {0, 0, -1, 1, -1, -1, 1, 1};
const int dc[] = {-1, 1, 0, 0, -1, 1, -1, 1};
const int MOD = 1e9 + 7;
const double pi = acos(-1.0);
const int MAXN = 62500 + 10;
const int MAXT = 100000 + 10;
using namespace std;
int a[MAXN], b[MAXN], dp[MAXN];
map<int, int> mp;
int main(){
int T;
scanf("%d", &T);
int kase = 0;
while(T--){
mp.clear();
int n, p, q;
scanf("%d%d%d", &n, &p, &q);
for(int i = 1; i <= p + 1; ++i){
scanf("%d", &a[i]);
mp[a[i]] = i;
}
for(int i = 1; i <= q + 1; ++i){
scanf("%d", &b[i]);
if(!mp.count(b[i])){
b[i] = 0;
}
else{
b[i] = mp[b[i]];
}
}
memset(dp, INT_INF, sizeof dp);
for(int i = 1; i <= q + 1; ++i){
*lower_bound(dp, dp + q + 1, b[i]) = b[i];
}
printf("Case %d: %d\n", ++kase, lower_bound(dp, dp + q + 1, INT_INF) - dp);
}
return 0;
}

  

最新文章

  1. JSON简介
  2. 开发中model,entity和pojo的区别
  3. Ubuntu系统下运行Eclipse出现找不到jre的问题的解决方法
  4. UVa 1473 - Dome of Circus 三分
  5. JSP JSP工作原理 JSP语法 JSP声明 JSP注释 JSP指令 jsp九大隐式/内置对象
  6. java学习笔记 (5) —— Struts2 监听器配置
  7. 转: sublime text常用插件和快捷键
  8. Visual Studio warning MSB3270:There was a mismatch between the processor architecture of the project being built &quot;MSIL&quot;
  9. spring AOP 是如何一步一步被简化的
  10. HBuilder js 自定义代码块
  11. Java 9 揭秘(18. Streams API 更新)
  12. REST设计规则
  13. Jenkins代码管理
  14. js中==和===区别
  15. C# reportview 按时间改变行颜色
  16. SSD-Tensorflow: 3 步运行 TensorFlow 单图片多盒目标检测器
  17. (八十九)用AutoLayout实现动画和Label根据内容自动调整
  18. java 基础之 反射技术
  19. 【mongo】查询超时处理
  20. python摸爬滚打之day16----类的成员

热门文章

  1. SystemProperities
  2. Redis的C++与JavaScript访问操作
  3. 如何使用ffmpeg的c语言sdk实现对文件夹的操作
  4. core版本使用ef连接数据库(一)
  5. 十 用栈解决LeetCode20题括号的匹配
  6. Linux服务器运行一段时间,出现CPU占用率达到100%卡死
  7. 等级保护2.0-mysql
  8. 回顾PHP:第一章:PHP基础语法
  9. java学习-循环结构-递归练习1-汉诺塔问题
  10. 30 整数中1出现的次数(从1到n整数中1出现的次数)这题很难要多看*