NYIST 760 See LCS again
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的更多相关文章
- nyoj 760 See LCS again
See LCS again 时间限制:1000 ms | 内存限制:65535 KB 难度:3 描述 There are A, B two sequences, the number of ele ...
- NYOJ 36 LCS(最长公共子序列)
题目链接: http://acm.nyist.edu.cn/JudgeOnline/problem.php?pid=36 最长公共子序列 时间限制:3000 ms | 内存限制:65535 KB ...
- nyoj36-最长公共子序列 (LCS)
http://acm.nyist.net/JudgeOnline/problem.php?pid=36 最长公共子序列 时间限制:3000 ms | 内存限制:65535 KB 难度:3 描述 ...
- 我的第一篇博客----LCS学习笔记
LCS引论 在这篇博文中,博主要给大家讲一个算法----最长公共子序列(LCS)算法.我最初接触这个算法是在高中学信息学竞赛的时候.那时候花了好长时间理解这个算法.老师经常说,这种算法是母算法,即从这 ...
- 动态规划之最长公共子序列(LCS)
转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- 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 ...
- UVA 11404 Palindromic Subsequence[DP LCS 打印]
UVA - 11404 Palindromic Subsequence 题意:一个字符串,删去0个或多个字符,输出字典序最小且最长的回文字符串 不要求路径区间DP都可以做 然而要字典序最小 倒过来求L ...
- 删除部分字符使其变成回文串问题——最长公共子序列(LCS)问题
先要搞明白:最长公共子串和最长公共子序列的区别. 最长公共子串(Longest Common Substirng):连续 最长公共子序列(Longest Common Subsequence,L ...
随机推荐
- bzoj2142 礼物——扩展卢卡斯定理
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=2142 前几天学了扩展卢卡斯定理,今天来磕模板! 这道题式子挺好推的(连我都自己推出来了) , ...
- HTTP协议下可拖动时间轴播放FLV的实现(伪流媒体)
HTTP协议下实现FLV的播放其实并不复杂,当初实现的原理是使用了flowPlayer插件实现的,效果还不错.但仍有两大问题影响着客户的访问情绪: 1.预加载时页面卡死,似乎没有边下边播. 2.偶尔边 ...
- win10系统下,开启数据库远程连接方式
右键左下角的windows标志,选择控制面板 2.查看方式修改为大图标 3.选择高级设置 4.新建入站规则 5.选择端口然后下一步 6.选择tcp协议,端口输入80,3306 7.选择允许连接 8.规 ...
- Vue Router的params和query传参的使用和区别
vue页面跳转有两种方式分别是:name和path this.$router.push({name: 'HelloWorld2}) this.$router.push({path: '/hello-w ...
- Vue中nextTick()解析
最近,在开发的时候遇到一个问题,让我对vue中nextTick()的用法加深了了解- 下面是在组件中引用的一个拖拽的组件: <vue-draggable-resizable class=&quo ...
- 题解报告:hdu 1272 小希的迷宫
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1272 Problem Description 上次Gardon的迷宫城堡小希玩了很久(见Problem ...
- day02_12/12/2016_bean的实例化之构造器方式
- android中TextView内容竖向显示
项目中遇到需要textview内容竖着排的需求,如图所示: 网上那些“教程”并不能达到需要的效果,发现有一个属性可以支持这种效果,android:ems=“*”,这是属性表示一行只显示*个字符. 具体 ...
- js 获取 checkbox[] 值
$("#btnAdd1").click(function () { console.log($("form").serialize()); var checkb ...
- day10-函数基础知识
函数 什么是函数 把工具事先准备好,然后下次使用的时候,直接使用就行了.我们的函数就是工具 为何用函数 1.遇到重复的功能只能重复编写实现代码,代码冗余 2.功能需要扩展时,需要找出所有实现该功能的地 ...