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 4
1 2 6 5 4
1 3 5 4

样例输出
3

上传者
TC_胡仁东

解题:一种LCS转LCS的nlogn的算法。是严格上升的LCS。

首先是LCS,我们把a序列中的每个元素在b中出现的位置保存起来,再按照降序排列,排列后再代入a的每个对应元素,那就转化为了求这个新的序列的最长上升子序列了。如:a[] = {a, b, c,} b[] = {a,b,c,b,a,d},那么a中的a,b,c在b中出现的位置分别就是{0,4},{1,3},{2}。分别按降序排列后代入a序列就是{4,0,2,3,1},之所以要按照降序排列,目的就是为了让每个元素只取到一次。

接下来的问题就是要求最长升序子序列问题了,也就是求LIS。

特殊情况下,会退化得很严重。

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <climits>
#include <vector>
#include <queue>
#include <cstdlib>
#include <string>
#include <set>
#include <stack>
#define LL long long
#define pii pair<int,int>
#define INF 0x3f3f3f3f
using namespace std;
struct info{
int num,pos;
};
int n,m,tot,sa[],sc[],q[],head,tail;
info sb[];
bool cmp(const info &x,const info &y){
return x.num < y.num;
}
int bsearch(int lt,int rt,int val){
int mid,pos = -;
while(lt <= rt){
int mid = (lt+rt)>>;
if(val <= sb[mid].num){
pos = mid;
rt = mid-;
}else lt = mid+;
}
return pos;
}
int binsearch(int lt,int rt,int val){
while(lt <= rt){
int mid = (lt+rt)>>;
if(q[mid] < val) lt = mid+;
else rt = mid-;
}
return lt;
}
int main() {
while(~scanf("%d %d",&n,&m)){
head = tail = tot = ;
for(int i = ; i <= n; i++) scanf("%d",sa+i);
for(int i = ; i <= m; i++){
scanf("%d",&sb[i].num);
sb[i].pos = i;
}
sort(sb+,sb+m+,cmp);
for(int i = ; i <= n; i++){
int tmp = bsearch(,m,sa[i]);
while(tmp > && sb[tmp].num == sa[i]) sc[tot++] = sb[tmp++].pos;
}
for(int i = ; i < tot; i++){
if(head == tail || q[head-] < sc[i]){
q[head++] = sc[i];
}else{
int tmp = binsearch(tail,head-,sc[i]);
q[tmp] = sc[i];
}
}
printf("%d\n",head-tail);
}
return ;
}

NYIST 760 See LCS again的更多相关文章

  1. nyoj 760 See LCS again

    See LCS again 时间限制:1000 ms  |  内存限制:65535 KB 难度:3 描述 There are A, B two sequences, the number of ele ...

  2. NYOJ 36 LCS(最长公共子序列)

    题目链接: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=36 最长公共子序列 时间限制:3000 ms  |  内存限制:65535 KB ...

  3. nyoj36-最长公共子序列 (LCS)

    http://acm.nyist.net/JudgeOnline/problem.php?pid=36 最长公共子序列 时间限制:3000 ms  |  内存限制:65535 KB 难度:3   描述 ...

  4. 我的第一篇博客----LCS学习笔记

    LCS引论 在这篇博文中,博主要给大家讲一个算法----最长公共子序列(LCS)算法.我最初接触这个算法是在高中学信息学竞赛的时候.那时候花了好长时间理解这个算法.老师经常说,这种算法是母算法,即从这 ...

  5. 动态规划之最长公共子序列(LCS)

    转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...

  6. 动态规划求最长公共子序列(Longest Common Subsequence, LCS)

    1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...

  7. Hackerrank11 LCS Returns 枚举+LCS

    Given two strings,  a and , b find and print the total number of ways to insert a character at any p ...

  8. UVA 11404 Palindromic Subsequence[DP LCS 打印]

    UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...

  9. 删除部分字符使其变成回文串问题——最长公共子序列(LCS)问题

    先要搞明白:最长公共子串和最长公共子序列的区别.    最长公共子串(Longest Common Substirng):连续 最长公共子序列(Longest Common Subsequence,L ...

随机推荐

  1. 洛谷 P3377 模板左偏树

    题目:https://www.luogu.org/problemnew/show/P3377 左偏树的模板题: 加深了我对空 merge 的理解: 结构体的编号就是原序列的位置. 代码如下: #inc ...

  2. php排序函数测试

    1.sort,asort,arsort函数 十万个数的数组排序,用了0.17秒 $starttime=explode(' ',microtime());;for ($i=0; $i <10000 ...

  3. 父页面调用子页面js的方法

    iframe子页面调用父页面javascript函数的方法今天遇到一个iframe子页面调用父页面js函数的需求,解决起来很简单,但是在chrome浏览器遇到一点小问题.顺便写一下iframe的父页面 ...

  4. 基于Spark Streaming预测股票走势的例子(一)

    最近学习Spark Streaming,不知道是不是我搜索的姿势不对,总找不到具体的.完整的例子,一怒之下就决定自己写一个出来.下面以预测股票走势为例,总结了用Spark Streaming开发的具体 ...

  5. Wannafly挑战赛19 A-队列Q

    题目描述 ZZT 创造了一个队列 Q.这个队列包含了 N 个元素,队列中的第 i 个元素用 Qi 表示.Q1 表示队头元素,QN 表示队尾元素.队列中的元素是 N 的一个全排列. ZZT 需要在这个队 ...

  6. 如何下载 Nginx (windows 版本)并且简单的使用

    官网地址:http://nginx.org/ 进到官网 我这里下载的是 稳定版的 windows版本. 开始我们的简单测试 步骤一:找到nginx的压缩包,(随意找个地方)解压 步骤二:进入conf文 ...

  7. sql server 无法创建数据库,错误代码:1807

    SQL Server 不能创建数据库,发生错误:1807 :未能获得数据库 'model' 上的排它锁.请稍后重试操作. declare   @sql   varchar(100)     while ...

  8. Objective-C——关联对象

    动态语言 OC是一种动态语言,它的方法,对象的类型都是到运行的时候才能够确定的.所以这就使得OC存在了关联对象这一强大的机制. 关联对象 所谓关联对象,其实就是我们在运行时对一个已存在的对象上面绑定一 ...

  9. [ BZOJ 3038 & 3211 / SPOJ GSS4 ] 上帝造题七分钟2 / 花神游历各国

    \(\\\) \(Description\) 给出一个长度为\(N\)的数列,共进行\(M\)次操作: \(1\ L\ R\):查询\([L,R]\)区间和. \(2\ L\ R\):对\([L,R] ...

  10. bootstrap 字体颜色 对齐方式

    一:字体代码:作用--颜色 1..text-muted:提示--浅灰色 2..text-primary:主要--蓝色 3..text-success:成功--浅绿色 4..text-info:     ...