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 ...
随机推荐
- luogu1084 [NOIp2012]疫情控制 (二分答案+倍增+dfs序)
先二分出一个时间,把每个军队倍增往上跳到不能再跳 然后如果它能到1号点,就记下来它跳到1号点后剩余的时间:如果不能,就让它就地扎根,记一记它覆盖了哪些叶节点(我在这里用了dfs序+差分,其实直接dfs ...
- (转)丢掉鼠标吧,使用最好用的eclipse快捷键
背景:eclipse作为自己经常使用的一款开发工具,熟练运用,能够达到事半功倍的效果.下面这篇文章总结了一些平时经常要使用的快捷键,十分的方便. 介绍Eclipse快捷键的文章很多,但大多都不详细,且 ...
- Eclipse 报 "The builder launch configuration could not be found" 错误
Eclipse 报 "The builder launch configuration could not be found" 错误的解决办法 标签: eclipseEclipse ...
- 图像处理之规则裁剪(Resize)
1 图像裁剪 在实际工作中,经常需要根据研究工作要求对图像进行裁剪(Subset Image),按照实际图像分幅裁剪的过程,可以将图像分幅裁剪分为两种类型:规则分幅裁剪(Rectangle Subse ...
- Andrew Ng机器学习课程,第一周作业,python版本
Liner Regression 1.梯度下降算法 Cost Function 对其求导: theta更新函数: 代码如下: from numpy import * import numpy as n ...
- list对象指针与指针类型list
#include <string> #include <cctype> #include <algorithm> #include <iostream> ...
- python Popen卡死问题
程序经常卡死,定位了半天才定位到原因,原来是Popen导致的卡死: 程序如下: s = subprocess.Popen([*,*,*], stdout=subprocess.PIPE) ret = ...
- node.js原生后台进阶(二)
上一章讲到怎么样用原生node.js来获取GET.POST(urlencoded,formData)的参数,这一次我们更进一步,讲一下以下的点: 1.压缩(zlib) 2.流(stream) 3.路由 ...
- 函数和常用模块【day04】:内置函数分类总结(十一)
重点掌握 字符串格式化format() 字符串格式化百分号 判断 转换 数据类型 帮助信息 map和filter()函数 局部变量全局变量 计算内置函数 常用内置函数(其他) 后续会讲 不常用
- 那些年的 网络通信之 TCP/IP 传输控制协议 ip 加 端口 ---
/* 一个文本小写转换为大写的小程序,当客户端从键盘录入一串字符串发送到服务端服务端转换为大写返回给客户端 */ import java.io.*; import java.net.*; class ...