CF10D LCIS
题意翻译
求两个串的最长公共上升子序列。
题目描述
This problem differs from one which was on the online contest.
The sequence a1,a2,...,an is called increasing, if ai<ai+1 for i<n .
The sequence s1,s2,...,sk is called the subsequence of the sequence a1,a2,...,an , if there exist such a set of indexes 1<=i1<i2<...<ik<=n that 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 ) — the length of the first sequence. The second line contains n n n space-separated integers from the range [0,109] — elements of the first sequence. The third line contains an integer m m m ( 1<=m<=500) — the length of the second sequence. The fourth line contains m m m space-separated integers from the range [0,109] — elements of the second sequence.
输出格式:
In the first line output 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.
输入输出样例
7
2 3 1 6 5 4 6
4
1 3 5 6
3
3 5 6
5
1 2 0 2 1
3
1 0 1
2
0 1
Solution:
本题线性DP。
直接把两个常见的dp揉合,定义状态$f[i][j]$表示匹配到$A$串第$i$位并以$B$串第$j$位结尾的LCIS长度。
那么不难得到状态转移方程:$f[i][j]=max(f[i-1][k]+1),a[i]==b[j]\&\&a[i]>b[k]$。
显然可以滚掉第一维,然后若直接转移复杂度是$n^3$的,我们可以优化掉枚举决策$k$的循环,在状态转移完后存下最大的满足条件的$f[k]$作为下次转移的决策,显然满足最优性,这样的时间复杂度是$O(n^2)$的。
输出方案就只需要在转移时顺带记录下$b$串每个位置的$pre$就好了。
代码:
/*Code by 520 -- 10.20*/
#include<bits/stdc++.h>
#define il inline
#define ll long long
#define RE register
#define For(i,a,b) for(RE int (i)=(a);(i)<=(b);(i)++)
#define Bor(i,a,b) for(RE int (i)=(b);(i)>=(a);(i)--)
using namespace std;
const int N=;
int n,a[N],m,b[N],f[N],pre[N];
int stk[N],top,ans; int main(){
scanf("%d",&n); For(i,,n) scanf("%d",a+i);
scanf("%d",&m); For(i,,m) scanf("%d",b+i);
For(i,,n) {
int pos=;
For(j,,m){
if(a[i]==b[j]) f[j]=f[pos]+,pre[j]=pos;
if(a[i]>b[j]&&f[pos]<f[j]) pos=j;
}
}
int pos=;
For(i,,m) if(f[i]>f[pos]) pos=i;
printf("%d\n",f[pos]);
while(f[pos]--) stk[++top]=b[pos],pos=pre[pos];
while(top) printf("%d ",stk[top--]);
return ;
}
CF10D LCIS的更多相关文章
- CF10D LCIS (动态规划)
题目链接 Solution 动态规划. 令 \(f_{i,j}\) 表示 \(a\) 数组前 \(i\) 个和 \(b\) 数组前 \(j\) 所得的最长的 LCIS . 转移很好想: \(a_i!= ...
- CF10D LCIS 最长公共上升子序列
题目描述 This problem differs from one which was on the online contest. The sequence a1,a2,...,an a_{1}, ...
- CF10D LCIS(线性DP)
题意:\(LCIS\)输出方案 变迁の时刻,标记它 P.S:特判没\(LCIS\)的情况 //#include <iostream> #include <cstdio> #in ...
- CF10D/POJ2127 LCIS解题报告
题目传送门(洛谷)(CF)(POJ) 前言 期末考试前的最后一篇题解,希望期末考 rp++ 奇怪,为什么在CF上能过的代码到POJ上就 听取WA声一片 (不管了) 题目思路 LCIS模版O(n²) ...
- 【CF10D】LCIS(LCIS)
题意:求两个序列的LCIS n,m<=300,a[i]<=1e9 题意:O(n^2) O(n^3)的话设dp[i,j]为A终点为a[1..i]且B终点为b[j]的最大长度,分a[i]==b ...
- 「CF10D」LCIS
传送门 Luogu 解题思路 首先考虑怎么求方案,这样才可能会输出方案. 考虑 \(\text{DP}\). 设 \(f[i][j]\) 表示在 \(a\) 序列中选择一个 \([1...i]\) 的 ...
- 【二维树状数组】【CF10D】 LCIS
传送门 Description 给你两个串,求他们的最长公共上升子序列 Input 第一行是第一个串的长度\(n\) 第二行\(n\)个数代表第一个串 第三行是第二个串的长度\(m\) 第四行\(m\ ...
- 【CF10D】 LCIS
题目链接 最长公共上升子序列 \(f[i][j]\)表示\(A\)的前\(i\)个数,匹配\(B\)的第\(j\)个数,且\(B[j]\)必选时的最长公共上升子序列长度 转移: if(A[i]==B[ ...
- BestCoder Round #87 1003 LCIS[序列DP]
LCIS Accepts: 109 Submissions: 775 Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65 ...
随机推荐
- day44
今日内容: 1.前端概述 2.前端三剑客 3.页面基本结构 4.常用标签 5.标签分类 1.前端概述与前端三剑客 前端即⽹站前台部分,运⾏在PC端,移动端等浏览器上展现给⽤户浏览的⽹⻚.随着互联⽹技术 ...
- UDP穿越NAT原理(p2p)
转载自:http://blog.csdn.net/ldd909/article/details/5979967 论坛上经常有对P2P原理的讨论,但是讨论归讨论,很少有实质的东西产生(源代码).在这里我 ...
- 使用Novell.Directory.Ldap.NETStandard在.NET Core中验证AD域账号
Novell.Directory.Ldap.NETStandard是一个在.NET Core中,既支持Windows平台,又支持Linux平台,进行Windows AD域操作的Nuget包. 首先我们 ...
- css样式显示省略号
用css样式显示省略号,记 .xx{ display: block; width:200px;/*对宽度的定义,根据情况修改*/ overflow: hidden; white-space: n ...
- Redis Replication
Replication 官网说明:http://www.redis.io/topics/replication Redis使用异步复制; 一个Master可以有多个Slaves; Slaves可以接收 ...
- AS3.0 自定义右键菜单类
AS3.0 自定义右键菜单类: /** * 自定义右键菜单类 * 自定义菜单项不得超过15个,每个标题必须至少包含一个可见字符. * 标题字符不能超过100个,并且开头的空白字符会被忽略. * 与任何 ...
- 20155223 Exp7 网络欺诈防范
20155223 Exp7 网络欺诈防范 基础问题回答 通常在什么场景下容易受到DNS spoof攻击? 无设防或防护力特别弟弟低的公共局域网,或者是在同一个局域网下. 在日常生活工作中如何防范以上两 ...
- 20155233 《网络对抗》Exp7 网络欺诈技术防范
应用SET工具建立冒名网站 1.要让冒名网站在别的主机上也能看到,需要开启本机的Apache服务,并且要将Apache服务的默认端口改为80,先在kali中使用netstat -tupln |grep ...
- 20155305乔磊《网络对抗》逆向及Bof基础
20155305乔磊<网络对抗>逆向及Bof基础 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回显任何 ...
- 20155327 Exp9 Web安全基础
20155327 Exp9 Web安全基础 基础问题回答 (1)SQL注入攻击原理,如何防御 SQL注入攻击就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器 ...