推荐一篇炒鸡赞的blog。

以下代码中有打印路径。

#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 最长公共上升子序列模板题 + 打印路径的更多相关文章

  1. LCIS(最长公共上升子序列)模板

    求出LCIS并输出其路径. 1 #include <iostream> 2 #include <cstdio> 3 #include <string> 4 #inc ...

  2. [CodeForces10D]LCIS(最长公共上升子序列) - DP

    Description 给定两个数列,求最长公共上升子序列,并输出其中一种方案. Input&Output Input 第一行一个整数n(0<n<=500),数列a的长度. 第二行 ...

  3. LCIS最长公共上升子序列

    最长公共上升子序列LCIS,如字面意思,就是在对于两个数列A和B的最长的单调递增的公共子序列. 这道题目是LCS和LIS的综合. 在LIS中,我们通过两重循环枚举当序列以当前位置为结尾时,A序列中当前 ...

  4. LCIS 最长公共上升子序列问题DP算法及优化

    一. 知识简介 学习 LCIS 的预备知识: 动态规划基本思想, LCS, LIS 经典问题:给出有 n 个元素的数组 a[] , m 个元素的数组 b[] ,求出它们的最长上升公共子序列的长度. 例 ...

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

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

  6. LCIS(最长公共上升子序列)Vijos1264神秘的咒语

    描述 身为拜月教的高级间谍,你的任务总是逼迫你出生入死.比如这一次,拜月教主就派你跟踪赵灵儿一行,潜入试炼窟底. 据说试炼窟底藏着五行法术的最高法术:风神,雷神,雪妖,火神,山神的咒语.为了习得这些法 ...

  7. LCIS 最长公共上升子序列

    这个博客好久没写了,这几天为了准备清华交叉研究院的夏令营,在复习大一大二ACM训练时的一些基础算法,正好碰到LICS,发现没有写在博客里,那就顺便记录一下好了. 参考链接:http://blog.cs ...

  8. 【简单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 题目:给你两个数字序列,求出这两个序列的最长公共上升子序列.输出最长的长度,并打表 ...

  9. 最长公共上升子序列(LCIS)

    最长公共上升子序列慕名而知是两个字符串a,b的最长公共递增序列,不一定非得是连续的.刚开始看到的时候想的是先用求最长公共子序列,然后再从其中找到最长递增子序列,可是仔细想一想觉得这样有点不妥,然后从网 ...

随机推荐

  1. UVA 536 (13.08.17)

     Tree Recovery  Little Valentine liked playing with binary trees very much. Her favoritegame was con ...

  2. C#邮件发送

    public static void CreateCopyMessage() { MailAddress from = new MailAddress("yang@163.com" ...

  3. javascript 获取event对象

    //转载处 http://www.cnblogs.com/funlake/archive/2009/04/07/1431238.html 非常详细 先从一个简单的例子说起,一个简单的button控件如 ...

  4. 2016 ACM/ICPC Asia Regional Shenyang Online

    I:QSC and Master 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5900 题意: 给出n对数keyi,vali表示当前这对数的键值和权值 ...

  5. Volley报错!!!No address associated with hostname

    年轻人检查你的网络去吧,这是没有网络导致的原因

  6. Ext.net GridPanel获取选中行的数据

    1.前台页面 在button中添加ExtraParams   <DirectEvents> <Click> <ExtraParams> <ext:Pramet ...

  7. java连接sqL2008 数据库实例

    package com.lzw; import java.sql.Connection; import java.sql.DriverManager; import java.sql.ResultSe ...

  8. jar文件运行打断点

    eclipse中jar包打断点 1. 下载工具 链接:http://pan.baidu.com/s/1jHSXMSm 密码:3aww 或者: 1下载jad.exe,地址: http://www.kpd ...

  9. bzoj 2049 Cave 洞穴勘测(LCT)

    转载请注明出处: http://www.cnblogs.com/fraud/          ——by fraud 动态树入门题,不需要维护任何信息. 我用的是splay,下标实现的lct. #in ...

  10. n个数的最大公约、最小公倍数

    #include <cstdio> #include <cstring> using namespace std; #define N 1010 //两个数的最大公约数和最小公 ...