题目传送门

题意:LCIS(Longest Common Increasing Subsequence) 最长公共上升子序列

分析:a[i] != b[j]: dp[i][j] = dp[i-1][j]; a[i]==b[j]:  dp[j]=max(dp[j],dp[k]); (1<=k<j&&b[k]<b[j])  打印路径时按照b[i]来输出

收获:理解不是很深入,推荐资料: 最长公共上升子序列(LCIS)的O(n^2)算法  最长公共上升子序列的另一个O(mn)的算法

 

代码:

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <map>
using namespace std; const int N = 5e2 + 10;
const int INF = 0x3f3f3f3f;
int a[N], b[N], dp[N][N], fx[N][N], fy[N][N];
int n, m;
bool fir; void print(int x, int y, int last) { //bool fir;
if (x == 0 || y == 0) return ;
print (fx[x][y], fy[x][y], y);
if (y != last) {
if (fir) printf ("%d", b[y]), fir = false;
else printf (" %d", b[y]);
}
} void LCIS(void) {
memset (dp, 0, sizeof (dp));
memset (fx, 0, sizeof (fx));
memset (fy, 0, sizeof (fy));
int sx = 0, sy = 0;
int ret = 0, k = 0;
for (int i=1; i<=n; ++i) {
k = 0;
for (int j=1; j<=m; ++j) {
dp[i][j] = dp[i-1][j]; //以a[]为主循环,每个a[i],去找每个b[j]
fx[i][j] = i - 1; fy[i][j] = j;
if (a[i] == b[j] && dp[i][j] < dp[i][k] + 1) { //满足LCS
dp[i][j] = dp[i][k] + 1; //在1~j-1找到b[k]<a[i],满足LIS,在b[k]上更新dp
fx[i][j] = i; fy[i][j] = k;
}
else if (a[i] > b[j] && dp[i][j] > dp[i][k]) k = j; //找到最优的k
if (ret < dp[i][j]) {
ret = dp[i][j]; //更新所有dp中的最大值
sx = i, sy = j;
}
}
}
printf ("%d\n", ret);
fir = true;
print (sx, sy, -1); puts ("");
} int main(void) {
while (scanf ("%d", &n) == 1) {
for (int i=1; i<=n; ++i) scanf ("%d", &a[i]);
scanf ("%d", &m);
for (int i=1; i<=m; ++i) scanf ("%d", &b[i]);
LCIS ();
} return 0;
}

  

LCIS POJ 2172 Greatest Common Increasing Subsequence的更多相关文章

  1. POJ 1423 Greatest Common Increasing Subsequence【裸LCIS】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1423 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  2. POJ 2127 Greatest Common Increasing Subsequence -- 动态规划

    题目地址:http://poj.org/problem?id=2127 Description You are given two sequences of integer numbers. Writ ...

  3. POJ 2127 Greatest Common Increasing Subsequence

    You are given two sequences of integer numbers. Write a program to determine their common increasing ...

  4. 最长公共上升子序列 (poj 2127) (Greatest Common Increasing Subsequence)

    \(Greatest Common Increasing Subsequence\) 大致题意:给出两个长度不一定相等的数列,求其中最长的公共的且单调递增的子序列(需要具体方案) \(solution ...

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

  6. HDU 1423 Greatest Common Increasing Subsequence LCIS

    题目链接: 题目 Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...

  7. HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS)

    HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS) http://acm.hdu.edu.cn/showproblem.php?pi ...

  8. HDU 1423 Greatest Common Increasing Subsequence(LCIS)

    Greatest Common Increasing Subsequenc Problem Description This is a problem from ZOJ 2432.To make it ...

  9. HDOJ 1423 Greatest Common Increasing Subsequence -- 动态规划

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1423 Problem Description This is a problem from ZOJ 2 ...

随机推荐

  1. 批量删除wordpress垃圾评论留言

    wordpress博客的存在,垃圾评论注定会找上门来.大家还可以用Akismet.Bad Behavior.Spam Karma等一些其他的插件或者直接用程序写个验证码函数对留言进行验证来过滤 垃圾评 ...

  2. 理解Java中的接口

    一.为什么要使用接口 假如有一个需求:要求实现防盗门的功能.门有"开"和"关"的功能,锁有"上锁"和"开锁"的功能. 分 ...

  3. Linux内核之数据双链表

    导读 Linux 内核中自己实现了双向链表,可以在 include/linux/list.h 找到定义.我们将会首先从双向链表数据结构开始介绍内核里的数据结构.为什么?因为它在内核里使用的很广泛,你只 ...

  4. IOS 中的CoreImage框架

    IOS 中的CoreImage框架(framework) - time4cnblogs 时间 2014-03-15 00:24:00  博客园-所有随笔区原文  http://www.cnblogs. ...

  5. ubuntu12.04 Daemon的简单实现

    使用命令 start-stop-daemon 官方文档:http://manpages.ubuntu.com/manpages/lucid/en/man8/start-stop-daemon.8.ht ...

  6. HTML5/CSS3开发工具

    1.谷歌Google Web Designer https://www.google.com/webdesigner/ 这个工具不能在xp上运行,可以在win7 win8上运行 http://www. ...

  7. 【Other】千字文 硬笔 楷书 字帖

    <千字文>是我国最优秀的一篇训蒙教材,用一千个汉字勾划出一部完整的中国文化史的基本轮廓,代表了中国传统教育启蒙阶段的最高水平.<千字文>通篇首尾连贯,音韵谐美,读起来朗朗上口, ...

  8. Count Primes

    Count the number of prime numbers less than a non-negative number, n public int countPrimes(int n) { ...

  9. 跨域请求获取Solr json检索结果并高亮显示

    Solr提供了json格式的检索结果,然而在跨域的情况下如何调用呢?我们可以利用jquery提供的jsonp的方式获取Solr检索结果. <script type="text/java ...

  10. Linux下配置Tomcat服务器

    Linux下配置Tomcat服务器和Windows下其实差不多,可以去官网下载安装包释放或者在线下载,只是当时下载的windows.zip文件,现在下载.tar.gz格式的即可,下面使用命令行的方式安 ...