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的最长公共递增序列,不一定非得是连续的.刚开始看到的时候想的是先用求最长公共子序列,然后再从其中找到最长递增子序列,可是仔细想一想觉得这样有点不妥,然后从网 ...
随机推荐
- proxy 利用get拦截,实现一个生成各种DOM节点的通用函数dom。
const dom = new Proxy({}, { get(target, property) { return function(attrs = {}, ...children) { const ...
- 【iOS开发-66】QQ设置界面的案例:利用storyboard开发静态的tableView界面,核心是Static Cells
(1)效果 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvd2Vpc3ViYW8=/font/5a6L5L2T/fontsize/400/fill/I0JB ...
- Unity 启动画面淡入淡出
前几天在玩我叫MT 2游戏的时候发现打开他们应用的时候发现他们Logo淡入淡出的效果做的挺好的,例如第一张是运营商腾讯的logo第二张是他们公司的游戏logo.我们也来模仿一下: 第一张图片:运营商的 ...
- 本地化下按首字母分组排序的神器——UILocalizedIndexedCollation
最近在整一个很简单的通讯录相关的项目,通讯录当然就少不了按首字母或者汉字拼音首字母分组排序索引.因为按照我一贯的的做法,都是想要做成更通用的.支持本地化的,所以这就纠结了,世界各地的语言啊我去,我顶多 ...
- Linux远程登录
Linux远程登录 远程登录 关闭linux的防火墙 /etc/init.d/iptables stop 启动VNC服务器 Vncserver & 然后记住desktop is localho ...
- C# sql操作
SqlConnection con = new SqlConnection(strSqlConnection);//strSqlConnection为字符串连接 DataTable ...
- ios10 适配问题总结
看各个大神整理而成 1.检查版本问题 不可以像下面这样用 #define isiOS10 ([[[[UIDevice currentDevice] systemVersion] substringTo ...
- 获取UILabel上最后一个字符串的位置。获取文字长度和高度,自动换行
//行的高度. -(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPat ...
- UVA 11754 Code Feat (枚举,中国剩余定理)
转载请注明出处: http://www.cnblogs.com/fraud/ ——by fraud C Code Feat The government hackers at C ...
- 解决mysql中文存储问题
在mysql安装目录下先找到my.ini,给[mysql]和[mysqld]下的default-character-set赋值为utf8 即改为:default-character-set=utf8 ...