P3402 最长公共子序列
经典问题
LCS-->LIS

没有重复的值才可以这么做
把第一数列转化成1~n,然后将第二个数列映射成1~n中的一些数,然后求第二个数列的LIS即可,然后用Bit求LIS,O(nlogN)

//数据太大,考虑map

#include<iostream>
#include<cstdio>
#include<queue>
#include<algorithm>
#include<cmath>
#include<ctime>
#include<map>
#include<cstring>
#define inf 2147483647
#define For(i,a,b) for(register int i=a;i<=b;i++)
#define p(a) putchar(a)
#define g() getchar()
//by war
//2017.10.17
using namespace std;
int n,m;
int t[];
int ans;
int f[];
int a[];
int b[];
int x;
map<int,int>c;
void in(int &x)
{
int y=;
char c=g();x=;
while(c<''||c>'')
{
if(c=='-')
y=-;
c=g();
}
while(c<=''&&c>='')x=x*+c-'',c=g();
x*=y;
}
void o(int x)
{
if(x<)
{
p('-');
x=-x;
}
if(x>)o(x/);
p(x%+'');
} int getmax(int k)
{
int Max=;
for(;k>;k-=(-k)&k)
Max=max(Max,t[k]);
return Max;
} void modify(int k,int Max)
{
for(;k<=n;k+=(-k)&k)
t[k]=max(t[k],Max);
} int main()
{
in(n),in(m);
For(i,,n)
in(b[i]),c[b[i]]=i;
For(i,,m)
in(a[i]);
For(i,,m)
a[i]=c[a[i]];
For(i,,m)
{
f[i]=getmax(a[i])+;
ans=max(ans,f[i]);
if(a[i]>)
modify(a[i],f[i]);
}
o(ans);
return ;
}

我今天才算搞懂最长公共子序列的nlogn的做法,下面的是任何情况都适用的

5 4 1 1 2
1 2 4,3 4,3 5

1 1 4
4 3 4 3 5

在4 3 4 3 5里面求严格递增的LIS就是二者的LCS

#include <bits/stdc++.h>
#define inf 2147483647
#define N 1000010
#define p(a) putchar(a)
#define For(i,a,b) for(long long i=a;i<=b;++i)
//by war
//2020.4.24
using namespace std;
long long n,m,cnt;
long long t[N],a[N],b[N],temp[N];
long long ans0,ans1;
long long x;
map<long long,long long>c,d;
vector<long long>v[N],u;
stack<int>st;
void in(long long &x){
long long y=;char c=getchar();x=;
while(c<''||c>''){if(c=='-')y=-;c=getchar();}
while(c<=''&&c>=''){ x=(x<<)+(x<<)+c-'';c=getchar();}
x*=y;
}
void o(long long x){
if(x<){p('-');x=-x;}
if(x>)o(x/);
p(x%+'');
} long long getmax(long long k){
long long Max=;
for(;k>;k-=(-k)&k)
Max=max(Max,t[k]);
return Max;
} void modify(long long k,long long Max){
for(;k<=cnt;k+=(-k)&k)
t[k]=max(t[k],Max);
} signed main(){
in(n);in(m);
For(i,,n){
in(b[i]);
if(c[b[i]]==){
c[b[i]]=i;
v[i].push_back(i);
}
else v[c[b[i]]].push_back(i);
temp[i]=b[i];
}
sort(temp+,temp+n+);
For(i,,n) d[temp[i]]=i;
For(i,,n) b[i]=d[b[i]],u.push_back(b[i]);
cnt=u.size();
for(auto i:u){
long long cc=getmax(i-)+;
ans0=max(ans0,cc);
modify(i,cc);
}
u.clear();
For(i,,m) in(a[i]);
For(i,,m){
if(c[a[i]]!=){
for(auto j:v[c[a[i]]])
st.push(j);
while(!st.empty()) u.push_back(st.top()),st.pop();
}
}
memset(t,,sizeof(t));
for(auto i:u){
long long cc=getmax(i-)+;
ans1=max(ans1,cc);
modify(i,cc);
}
o(ans0);p(' ');o(ans1);
return ;
}

P3402 最长公共子序列的更多相关文章

  1. P3402 最长公共子序列(nlogn)

    P3402 最长公共子序列 题目背景 DJL为了避免成为一只咸鱼,来找Johann学习怎么求最长公共子序列. 题目描述 经过长时间的摸索和练习,DJL终于学会了怎么求LCS.Johann感觉DJL孺子 ...

  2. luogu P3402 最长公共子序列

    题目背景 DJL为了避免成为一只咸鱼,来找Johann学习怎么求最长公共子序列. 题目描述 经过长时间的摸索和练习,DJL终于学会了怎么求LCS.Johann感觉DJL孺子可教,就给他布置了一个课后作 ...

  3. 洛谷P3402 最长公共子序列

    题目背景 DJL为了避免成为一只咸鱼,来找Johann学习怎么求最长公共子序列. 题目描述 经过长时间的摸索和练习,DJL终于学会了怎么求LCS.Johann感觉DJL孺子可教,就给他布置了一个课后作 ...

  4. 【Luogu】P3402最长公共子序列(LCS->nlognLIS)

    题目链接 SovietPower 的题解讲的很清楚.Map或Hash映射后用nlogn求出LIS.这里只给出代码. #include<cstdio> #include<cctype& ...

  5. 用python实现最长公共子序列算法(找到所有最长公共子串)

    软件安全的一个小实验,正好复习一下LCS的写法. 实现LCS的算法和算法导论上的方式基本一致,都是先建好两个表,一个存储在(i,j)处当前最长公共子序列长度,另一个存储在(i,j)处的回溯方向. 相对 ...

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

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

  7. [Data Structure] LCSs——最长公共子序列和最长公共子串

    1. 什么是 LCSs? 什么是 LCSs? 好多博友看到这几个字母可能比较困惑,因为这是我自己对两个常见问题的统称,它们分别为最长公共子序列问题(Longest-Common-Subsequence ...

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

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

  9. LintCode 77: 最长公共子序列

    public class Solution { /** * @param A, B: Two string. * @return: the length of the longest common s ...

随机推荐

  1. PyQt4 安装

    安装PyQt4很简单,从官网下载相应的安装包即可. 需要注意的是:应该根据你电脑上已经装好的python版本选择相应的PyQt4安装包. PyQt4的安装目录一定要选python的安装目录,比如我的P ...

  2. 团体程序设计天梯赛-L3-021 神坛 的一些错误做法 和 一些想法

    https://pintia.cn/problem-sets/994805046380707840/problems/994805046577840128 错误做法: 极角排序 + 最小三角形的两边是 ...

  3. IE盒模型和W3C盒子模型的区别

    其实这个问题到现在真的是没有意义了,因为早在IE6的兼容模式开始就已经弃用了IE盒子模型了,但是现在的各种面试题还是会时常出现这样的上世纪的题目,我觉得其实时纯粹的刁难. 好了,吐槽不多说了,直接上图 ...

  4. Hadoop生态圈-Kafka配置文件详解

    Hadoop生态圈-Kafka配置文件详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.默认kafka配置文件内容([yinzhengjie@s101 ~]$ more /s ...

  5. Nginx的基本配置案例

    Nginx的基本配置案例 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.Nginx配置虚拟主机   .操作系统环境 [root@yinzhengjie ~]# cat /etc ...

  6. JVM总结(一):概述--JVM对象探秘

    这一节我们来讨论一下JVM对象建立过程. JVM对象探秘 对象的建立 对象的内存布局 对象的访问定位 JVM对象探秘 对象的建立 对象的建立过程   图一:对象建立过程 1.类加载检查. 当JVM检测 ...

  7. python删除列表元素remove,pop,del

    python删除列表元素 觉得有用的话,欢迎一起讨论相互学习~Follow Me remove 删除单个元素,删除首个符合条件的元素,按值删除,返回值为空 List_remove = [1, 2, 2 ...

  8. Automate Tdxw

    Automate trade module in Tdxw Code # coding: utf-8 """ Created on Thu Dec 07 10:57:45 ...

  9. [转] Android 性能分析案例

    Android 系统的一个工程师(Romain Guy)针对Falcon Pro  应用,撰写了一个Android性能分析的文章.该文章介绍了如何分析一个应用哪里出现了性能瓶颈,导致该应用使用起来不流 ...

  10. Strange Queries(莫队)

    题目 You are given an array with n integers a1, a2, ..., an, and q queries to answer. Each query consi ...