CF10D LCIS 最长公共上升子序列
题目描述
This problem differs from one which was on the online contest.
The sequence a1,a2,...,an a_{1},a_{2},...,a_{n} a1,a2,...,an is called increasing, if ai<ai+1 a_{i}<a_{i+1} ai<ai+1 for i<n i<n i<n .
The sequence s1,s2,...,sk s_{1},s_{2},...,s_{k} s1,s2,...,sk is called the subsequence of the sequence a1,a2,...,an a_{1},a_{2},...,a_{n} a1,a2,...,an , if there exist such a set of indexes 1<=i1<i2<...<ik<=n 1<=i_{1}<i_{2}<...<i_{k}<=n 1<=i1<i2<...<ik<=n that aij=sj a_{ij}=s_{j} aij=sj . In other words, the sequence s s s can be derived from the sequence a a a by crossing out some elements.
You are given two sequences of integer numbers. You are to find their longest common increasing subsequence, i.e. an increasing sequence of maximum length that is the subsequence of both sequences.
输入格式
The first line contains an integer n n n ( 1<=n<=500 1<=n<=500 1<=n<=500 ) — the length of the first sequence. The second line contains n n n space-separated integers from the range [0,109] [0,10^{9}] [0,109] — elements of the first sequence. The third line contains an integer m m m ( 1<=m<=500 1<=m<=500 1<=m<=500 ) — the length of the second sequence. The fourth line contains m m m space-separated integers from the range [0,109] [0,10^{9}] [0,109] — elements of the second sequence.
输出格式
In the first line output k k k — the length of the longest common increasing subsequence. In the second line output the subsequence itself. Separate the elements with a space. If there are several solutions, output any.
题意翻译
然后考虑转移方程。
当A[i]!=B[j]时,dp[i][j]=dp[i-1][j]。这里很好理解,A[i]与B[j]都不相等了,自然可以往前推一个。要注意的是这里dp数组第二维的含义,还是以B[j]结尾,所以i->i-1而j不变。
当A[i]==B[j]时,说明AB两串至少有A[i]或者说B[j]这个公共元素了,再次考虑dp数组的定义,以B[j]为结尾的LCIS的长度”,自然想到找到一个末尾小于B[j]的串把B[j]给接上去。因此转移方程可以写成dp[i][j]=max(dp[i-1][k]+1|k:1 to j-1)。当然从决策集合考虑还可以优化成O(n^2),但看这个题的数据范围目前这么写足以过掉。
下面来看存储答案。这里借鉴了洛谷题解的巧妙思想,再创建一个二维数组out,其含义为:out[p][q]代表以B[p]结尾的LCIS的前q个元素。这样一来,在第二重for循环中,当遇到A[i]!=B[j]时,自然不用更新;当A[i]==B[j]时,首先令out[j][1]=b[j],这代表目前LCIS起码有一个元素B[j]。然后第三重循环遍历时遇到可以接上B[j]的,如果比当前dp[i][j]大,在更新dp[i][j]的
同时也更新out[j][1~dp[i-1][k]]。
#include <bits/stdc++.h>
using namespace std;
int n1,n2;
int a[];
int b[];
int dp[][]={};
int out[][];
int main()
{
int i,j,k,m;
int ans=;
cin>>n1;
memset(dp,,sizeof(dp));
for(i=;i<=n1;i++)
{
scanf("%d",&a[i]);
}
cin>>n2;
for(j=;j<=n2;j++)
{
scanf("%d",&b[j]);
}
dp[][]=;
int pos=;
for(i=;i<=n1;i++)
{
for(j=;j<=n2;j++)
{ if(a[i]!=b[j])
{
dp[i][j]=dp[i-][j];//和之前一样的话没必要更新输出串
}
else
{
//
dp[i][j]=;//相等的话最小也为1
out[j][]=b[j];//相等的话最少有一个b[j]
//
for(k=;k<j;k++)//找到最长的进行拼接
{
if(b[k]<b[j])
{
if(dp[i-][k]+>dp[i][j])//大于当前答案的话
{
dp[i][j]=dp[i-][k]+;//更新dp数组
for(m=;m<=dp[i-][k];m++)//更新out数组对应位置的元素
{
out[j][m]=out[k][m];
}
out[j][dp[i][j]]=b[j];//把b[j]加上去
}
}
}
}
ans=max(ans,dp[i][j]);//更新LCIS的长度
if(ans==dp[i][j])pos=j; //找到对应的位置
}
}
cout<<ans<<endl;
for(i=;i<=ans;i++)//输出LCIS
{
cout<<out[pos][i]<<' ';
} return ;
}
CF10D LCIS 最长公共上升子序列的更多相关文章
- [CodeForces10D]LCIS(最长公共上升子序列) - DP
Description 给定两个数列,求最长公共上升子序列,并输出其中一种方案. Input&Output Input 第一行一个整数n(0<n<=500),数列a的长度. 第二行 ...
- LCIS最长公共上升子序列
最长公共上升子序列LCIS,如字面意思,就是在对于两个数列A和B的最长的单调递增的公共子序列. 这道题目是LCS和LIS的综合. 在LIS中,我们通过两重循环枚举当序列以当前位置为结尾时,A序列中当前 ...
- LCIS 最长公共上升子序列问题DP算法及优化
一. 知识简介 学习 LCIS 的预备知识: 动态规划基本思想, LCS, LIS 经典问题:给出有 n 个元素的数组 a[] , m 个元素的数组 b[] ,求出它们的最长上升公共子序列的长度. 例 ...
- LCIS(最长公共上升子序列)Vijos1264神秘的咒语
描述 身为拜月教的高级间谍,你的任务总是逼迫你出生入死.比如这一次,拜月教主就派你跟踪赵灵儿一行,潜入试炼窟底. 据说试炼窟底藏着五行法术的最高法术:风神,雷神,雪妖,火神,山神的咒语.为了习得这些法 ...
- LCIS 最长公共上升子序列
这个博客好久没写了,这几天为了准备清华交叉研究院的夏令营,在复习大一大二ACM训练时的一些基础算法,正好碰到LICS,发现没有写在博客里,那就顺便记录一下好了. 参考链接:http://blog.cs ...
- LCIS(最长公共上升子序列)模板
求出LCIS并输出其路径. 1 #include <iostream> 2 #include <cstdio> 3 #include <string> 4 #inc ...
- CodeForces 10D. LCIS 最长公共上升子序列模板题 + 打印路径
推荐一篇炒鸡赞的blog. 以下代码中有打印路径. #include <algorithm> #include <iostream> #include <cstring& ...
- 【简单dp】poj 2127 Greatest Common Increasing Subsequence【最长公共上升子序列】【模板】
Sample Input 5 1 4 2 5 -12 4 -12 1 2 4 Sample Output 2 1 4 题目:给你两个数字序列,求出这两个序列的最长公共上升子序列.输出最长的长度,并打表 ...
- 最长公共上升子序列(LCIS)
最长公共上升子序列慕名而知是两个字符串a,b的最长公共递增序列,不一定非得是连续的.刚开始看到的时候想的是先用求最长公共子序列,然后再从其中找到最长递增子序列,可是仔细想一想觉得这样有点不妥,然后从网 ...
随机推荐
- redis 解决秒杀
# import redis pool = redis.ConnectionPool(host = '127.0.0.1', port=6379, db=0)#创建连接池 r = redis.Redi ...
- CentOS 7 使用笔记
一.下载.解压或安装等命令: 目前自己用过的三个下载及安装命令:curl.wget.yum. yum用法: $ sudo yum install libpng16-1.6.29-alt1.i586.r ...
- 【转载】SpringMVC框架介绍
转自:http://com-xpp.iteye.com/blog/1604183 SpringMVC框架图 SpringMVC接口解释 DispatcherServlet接口: Spring提 ...
- 一个实例 ---灵活使用jquery选择器实现input一个key,多个value 。 用ajax传递对象到后台控制器
标题可能不是很清晰,我们看实例: 简单来说就是需要实现sku的功能...一件商品可以有多个属性, 一个属性可以有多个值 . 最后以json格式存到数据库 难点一: 如何实现input输入框的弹性使用 ...
- js加密(五)产品目录
1. url:http://www.300600900.cn/ 2. target: 3. 简单分析 偶然发现,这个网站的加密,和landChina加密一模一样,js函数的名字都一样...所以,只贴 ...
- SQL通过Datatable更新数据库表内容
SQL通过Datatable更新数据库表内容 //要注意的一点是在Select语句中要选择的列中必须包含主键的列,此外不支持多表连接查询 DataTable dt = new DataTable( ...
- 密码学笔记——playfair密码
Playfair密码(Playfair cipher 或 Playfair square)一种替换密码,1854年由查尔斯·惠斯通(Charles Wheatstone)的英国人发明. 例题: 某种 ...
- bugku 前女友
首先打开链接然后会发现 照常情况下进行分析 查看源码然后发现 在这一串文字后还有一个链接然后 发现链接被隐藏了然后我们将link 删除就会显示出来点开新的连接 然后会发现这个 (仔细一看好像是php中 ...
- Codeforces Round #598 (Div. 3) D. Binary String Minimizing
You are given a binary string of length nn (i. e. a string consisting of nn characters '0' and '1'). ...
- linux中卸载mysql以及安装yum
卸载mysql:https://blog.csdn.net/qq_41829904/article/details/92966943 链接2:https://www.cnblogs.com/nickn ...