P3402 最长公共子序列
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 最长公共子序列的更多相关文章
- P3402 最长公共子序列(nlogn)
P3402 最长公共子序列 题目背景 DJL为了避免成为一只咸鱼,来找Johann学习怎么求最长公共子序列. 题目描述 经过长时间的摸索和练习,DJL终于学会了怎么求LCS.Johann感觉DJL孺子 ...
- luogu P3402 最长公共子序列
题目背景 DJL为了避免成为一只咸鱼,来找Johann学习怎么求最长公共子序列. 题目描述 经过长时间的摸索和练习,DJL终于学会了怎么求LCS.Johann感觉DJL孺子可教,就给他布置了一个课后作 ...
- 洛谷P3402 最长公共子序列
题目背景 DJL为了避免成为一只咸鱼,来找Johann学习怎么求最长公共子序列. 题目描述 经过长时间的摸索和练习,DJL终于学会了怎么求LCS.Johann感觉DJL孺子可教,就给他布置了一个课后作 ...
- 【Luogu】P3402最长公共子序列(LCS->nlognLIS)
题目链接 SovietPower 的题解讲的很清楚.Map或Hash映射后用nlogn求出LIS.这里只给出代码. #include<cstdio> #include<cctype& ...
- 用python实现最长公共子序列算法(找到所有最长公共子串)
软件安全的一个小实验,正好复习一下LCS的写法. 实现LCS的算法和算法导论上的方式基本一致,都是先建好两个表,一个存储在(i,j)处当前最长公共子序列长度,另一个存储在(i,j)处的回溯方向. 相对 ...
- 动态规划之最长公共子序列(LCS)
转自:http://segmentfault.com/blog/exploring/ LCS 问题描述 定义: 一个数列 S,如果分别是两个或多个已知数列的子序列,且是所有符合此条件序列中最长的,则 ...
- [Data Structure] LCSs——最长公共子序列和最长公共子串
1. 什么是 LCSs? 什么是 LCSs? 好多博友看到这几个字母可能比较困惑,因为这是我自己对两个常见问题的统称,它们分别为最长公共子序列问题(Longest-Common-Subsequence ...
- 动态规划求最长公共子序列(Longest Common Subsequence, LCS)
1. 问题描述 子串应该比较好理解,至于什么是子序列,这里给出一个例子:有两个母串 cnblogs belong 比如序列bo, bg, lg在母串cnblogs与belong中都出现过并且出现顺序与 ...
- LintCode 77: 最长公共子序列
public class Solution { /** * @param A, B: Two string. * @return: the length of the longest common s ...
随机推荐
- 【uoj7】 NOI2014—购票
http://uoj.ac/problem/7 (题目链接) 题意 给出一棵有根树,每次从一个节点出发可以买票到达它的一定范围内的祖先.问对于每一个点,到达根的最小花费是多少. Solution 右转 ...
- Linux上shell脚本date的用法
在shell脚本里date命令的用法: %% 一个文字的 % %a 当前locale 的星期名缩写(例如: 日,代表星期日) %A 当前locale 的星期名全称 (如:星期日) %b 当前local ...
- 虚拟机中liunx安装教程
目标:在Linux服务器上部署Java开发的网站 工具 VirtualBox-4.3.8:下载后安装. linux系统镜像: Centos国内镜像文件下载地址: http://centos.ustc ...
- redis 中用正则找key
获取 redis 中所有的 key 可用使用 *. redis 127.0.0.1:6379> KEYS * 1) "w3c3" 2) "w3c1" 3) ...
- HDFS之append数据到已存在文件中
遇到一个问题,想往已存在的hdfs文件中直接添加数据,默认的话应该是被拒绝的.查看了一些资料,可以这样操作: 在pdfs-site.xml中添加append支持: <property> & ...
- Linux命令(二)关机重启
- 流媒体技术学习笔记之(三)Nginx-Rtmp-Module统计某频道在线观看流的客户数
获得订阅者人数,可以方便地显示观看流的客户数. 查看已经安装好的模块 /usr/local/nginx/sbin/nginx -V 安装从源编译Nginx和Nginx-RTMP所需的工具 sudo a ...
- Does Deep Learning Come from the Devil?
Does Deep Learning Come from the Devil? Deep learning has revolutionized computer vision and natural ...
- html5 canvas贝塞尔曲线篇(上)
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...
- 关于Spring mvc注解中的定时任务的配置
关于spring mvc注解定时任务配置 简单的记载:避免自己忘记,不是很确定我理解的是否正确.有错误地方望请大家指出. 1,定时方法执行配置: (1)在applicationContext.xml中 ...