题意翻译

求两个串的最长公共上升子序列。

题目描述

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.

输入输出样例

输入样例#1:

7
2 3 1 6 5 4 6
4
1 3 5 6
输出样例#1:

3
3 5 6
输入样例#2:

5
1 2 0 2 1
3
1 0 1
输出样例#2:

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的更多相关文章

  1. CF10D LCIS (动态规划)

    题目链接 Solution 动态规划. 令 \(f_{i,j}\) 表示 \(a\) 数组前 \(i\) 个和 \(b\) 数组前 \(j\) 所得的最长的 LCIS . 转移很好想: \(a_i!= ...

  2. CF10D LCIS 最长公共上升子序列

    题目描述 This problem differs from one which was on the online contest. The sequence a1,a2,...,an a_{1}, ...

  3. CF10D LCIS(线性DP)

    题意:\(LCIS\)输出方案 变迁の时刻,标记它 P.S:特判没\(LCIS\)的情况 //#include <iostream> #include <cstdio> #in ...

  4. CF10D/POJ2127 LCIS解题报告

    题目传送门(洛谷)(CF)(POJ) 前言 期末考试前的最后一篇题解,希望期末考  rp++ 奇怪,为什么在CF上能过的代码到POJ上就 听取WA声一片  (不管了) 题目思路 LCIS模版O(n²) ...

  5. 【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 ...

  6. 「CF10D」LCIS

    传送门 Luogu 解题思路 首先考虑怎么求方案,这样才可能会输出方案. 考虑 \(\text{DP}\). 设 \(f[i][j]\) 表示在 \(a\) 序列中选择一个 \([1...i]\) 的 ...

  7. 【二维树状数组】【CF10D】 LCIS

    传送门 Description 给你两个串,求他们的最长公共上升子序列 Input 第一行是第一个串的长度\(n\) 第二行\(n\)个数代表第一个串 第三行是第二个串的长度\(m\) 第四行\(m\ ...

  8. 【CF10D】 LCIS

    题目链接 最长公共上升子序列 \(f[i][j]\)表示\(A\)的前\(i\)个数,匹配\(B\)的第\(j\)个数,且\(B[j]\)必选时的最长公共上升子序列长度 转移: if(A[i]==B[ ...

  9. BestCoder Round #87 1003 LCIS[序列DP]

    LCIS  Accepts: 109  Submissions: 775  Time Limit: 4000/2000 MS (Java/Others)  Memory Limit: 65536/65 ...

随机推荐

  1. 20155238 《JAVA程序设计》实验三(敏捷开发与XP实践)实验报告

    实验内容 敏捷开发与XP实践 XP基础 XP核心实践 相关工具 实验要求 1.没有Linux基础的同学建议先学习<Linux基础入门(新版)><Vim编辑器> 课程 2.完成实 ...

  2. Exp7

    实验内容 简单应用SET工具建立冒名网站 kali IP: 192.168.1.160 (原198) win7 IP: 192.168.1.199 1.开启本机Apache服务 (1)查看80端口是否 ...

  3. Unused Method(不再使用的方法)——Dead Code(死亡代码)

        系列文章目录:     使用Fortify进行代码静态分析(系列文章) Unused Method(不再使用的方法)    示例:  private bool checkLevel(strin ...

  4. 带Alpha通道的色彩叠加问题

    css3的rgba色彩模式.png/gif图片的alpha通道.canvas的rgba色彩模式.css3的阴影.css3的opacity属性等等,这些应用在网页中,有意无意间,我们的页面多了许多半透明 ...

  5. eclipse中怎么找项目部署的路径和找编译后的class路径

    1.快捷键 ctrl+shift+R,会默认显示你的源文件.java的路径,如果没有.class的话,点击右上角的三角,选中  Show Derived Resource; 2.打开出现下图 3.按下 ...

  6. 博客目录 Blog directory

    Linux 学习笔记 Linux/Mac 挂载远程服务器目录到本地 --Mount remote server directory to local PC 远程连接服务器端Jupyter Notebo ...

  7. Ubuntu命令行运行C程序和C++程序

    首先Ctrl + T 打开一个终端,cd到你建立C/C++文件的目录下. 下面以建立 helloc.c 和 hellocpp.cpp 进行演示 vim helloc.c 按 i 进入插入操作,然后写C ...

  8. k8s网络之calico学习

    一.知识准备 1.calico主要通过ipip协议与bgp协议来实现通信.前者通过ipip隧道作为通信基础,后者则是纯三层的路由交换 2.bgp协议主要由两种方式:BGP Speaker 全互联模式( ...

  9. Kaggle入门(一)——Digit Recognizer

    目录 0 前言 1 简介 2 数据准备 2.1 导入数据 2.2 检查空值 2.3 正则化 Normalization 2.4 更改数据维度 Reshape 2.5 标签编码 2.6 分割交叉验证集 ...

  10. git初次推送

    第一次配置Git git config --global user.name "xxxx" git config --global user.email "xxxx@xx ...