CodeForces 10D. LCIS 最长公共上升子序列模板题 + 打印路径
以下代码中有打印路径。
#include <algorithm>
#include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <queue>
#include <cmath>
#include <stack>
#include <map>
#include <ctime>
#include <iomanip> #pragma comment(linker, "/STACK:1024000000");
#define EPS (1e-6)
#define LL long long
#define ULL unsigned long long
#define _LL __int64
#define INF 0x3f3f3f3f
#define Mod 1000000007 using namespace std; int A[510],B[510]; int py[510][510],px[510][510],dp[510][510] = {0}; void Output(int x,int y)
{
if(x == -1 && y == -1)
return ; Output(px[x][y],py[x][y]); if(A[x] == B[y])
printf("%d ",A[x]);
} int main()
{
int n,m,i,j,mlen,x,y; scanf("%d",&n);
for(i = 1;i <= n; ++i)
scanf("%d",&A[i]); scanf("%d",&m);
for(i = 1;i <= m; ++i)
scanf("%d",&B[i]); int Max = 0,ax,ay; for(i = 1;i <= n; ++i)
{
mlen = 0,x = -1,y = -1;
for(j = 1;j <= m; ++j)
{
dp[i][j] = dp[i-1][j];
px[i][j] = i-1,py[i][j] = j;
if(B[j] < A[i] && dp[i-1][j] > mlen)
mlen = dp[i-1][j],x = i-1,y = j; if(B[j] == A[i])
dp[i][j] = mlen + 1,px[i][j] = x,py[i][j] = y;
if(dp[i][j] > Max)
Max = dp[i][j],ax = i,ay = j;
}
} if(Max)
printf("%d\n",Max),Output(ax,ay);
else
printf("0\n");
return 0;
}
CodeForces 10D. LCIS 最长公共上升子序列模板题 + 打印路径的更多相关文章
- LCIS(最长公共上升子序列)模板
求出LCIS并输出其路径. 1 #include <iostream> 2 #include <cstdio> 3 #include <string> 4 #inc ...
- [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[] ,求出它们的最长上升公共子序列的长度. 例 ...
- CF10D LCIS 最长公共上升子序列
题目描述 This problem differs from one which was on the online contest. The sequence a1,a2,...,an a_{1}, ...
- LCIS(最长公共上升子序列)Vijos1264神秘的咒语
描述 身为拜月教的高级间谍,你的任务总是逼迫你出生入死.比如这一次,拜月教主就派你跟踪赵灵儿一行,潜入试炼窟底. 据说试炼窟底藏着五行法术的最高法术:风神,雷神,雪妖,火神,山神的咒语.为了习得这些法 ...
- LCIS 最长公共上升子序列
这个博客好久没写了,这几天为了准备清华交叉研究院的夏令营,在复习大一大二ACM训练时的一些基础算法,正好碰到LICS,发现没有写在博客里,那就顺便记录一下好了. 参考链接:http://blog.cs ...
- 【简单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的最长公共递增序列,不一定非得是连续的.刚开始看到的时候想的是先用求最长公共子序列,然后再从其中找到最长递增子序列,可是仔细想一想觉得这样有点不妥,然后从网 ...
随机推荐
- iOS开发之Crash分析,以及收集
一 先谈谈iOS的Crash收集方式: 1. APP 发生crash,用户手机手机上肯定会有crash纪录,当然删除了该app,或是删了再装 crash纪录还是没了. 2. 如果用户设置-隐私 同 ...
- java的Future使用方法
首先,Future是一个接口,该接口用来返回异步的结果. package com.itbuluoge.mythread; import java.util.ArrayList; import java ...
- Flashback Drop实例操作
1.Flashback DropFlashback Drop 是从Oracle 10g 开始出现的,用于恢复用户误删除的对象(包括表,索引等), 这个技术依赖于Tablespace Recycle B ...
- js中退出语句break,continue和return 比较(转)
原链接:http://blog.163.com/ued_er/blog/static/199703159201210283107315/ js中退出语句break,continue和return 比较 ...
- html5 拖放---(二)转
draggable是一个枚举属性,用于指定一个标签是否可以被拖拽.有以下四种取值: true 表示此元素可拖拽 false 表示此元素不可拖拽 auto 除img和带href的标签a标签表示可拖拽外, ...
- 浏览器的Storage做缓存需要注意的地方
使用浏览器的Storage来做缓存.如: window.sessionStorage.setItem("cache", $(data).stringify()) 需要注意以下几点: ...
- CSS 设计彻底研究(一)(X)HTML与CSS核心基础
第1章 (X)HTML与CSS核心基础 这一章重点介绍了4个方面的问题.先介绍了 HTML和XHTML的发展历程以及需要注意的问题,然后介绍了如何将CSS引入HTML,接着讲解了CSS的各种选择器,及 ...
- 亲试,Windows平台上使用Qt5.2.1编写Android
首先把工具都下载好: 1. Qt for Android: http://qt-project.org/downloads 2. Android NDK http://developer.androi ...
- python运维开发(二十)----models操作、中间件、缓存、信号、分页
内容目录 select Form标签数据库操作 models操作F/Q models多对多表操作 Django中间件 缓存 信号 分页 select Form标签补充 在上一节中我们可以知道Form标 ...
- ZOJ 3745 Salary Increasing
Description Edward has established a company with n staffs. He is such a kind man that he did Q time ...