LCIS POJ 2172 Greatest Common Increasing Subsequence
题意: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的更多相关文章
- 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 ...
- POJ 2127 Greatest Common Increasing Subsequence -- 动态规划
题目地址:http://poj.org/problem?id=2127 Description You are given two sequences of integer numbers. Writ ...
- POJ 2127 Greatest Common Increasing Subsequence
You are given two sequences of integer numbers. Write a program to determine their common increasing ...
- 最长公共上升子序列 (poj 2127) (Greatest Common Increasing Subsequence)
\(Greatest Common Increasing Subsequence\) 大致题意:给出两个长度不一定相等的数列,求其中最长的公共的且单调递增的子序列(需要具体方案) \(solution ...
- 【简单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 题目:给你两个数字序列,求出这两个序列的最长公共上升子序列.输出最长的长度,并打表 ...
- HDU 1423 Greatest Common Increasing Subsequence LCIS
题目链接: 题目 Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS)
HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS) http://acm.hdu.edu.cn/showproblem.php?pi ...
- HDU 1423 Greatest Common Increasing Subsequence(LCIS)
Greatest Common Increasing Subsequenc Problem Description This is a problem from ZOJ 2432.To make it ...
- HDOJ 1423 Greatest Common Increasing Subsequence -- 动态规划
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1423 Problem Description This is a problem from ZOJ 2 ...
随机推荐
- Android的webview加载本地html、本apk内html和远程URL
//打开本包内asset目录下的index.html文件 wView.loadUrl(" file:///android_asset/index.html "); //打开本地sd ...
- 使用 systemd timer 备份数据库
导读 主要的Linux发行版都改用systemd 来替代 System V启动方式,其中 systemd timer 能替代 crontab 计划任务的大部分功能.本文介绍了用systemd time ...
- Unity3D使用小技巧
原地址:http://unity3d.9tech.cn/news/2014/0411/40178.html 1.Crtl+f摄像机自动适配场景. 2.可以用一个立方体作为底盘. 3.人物角色可以直接引 ...
- Selenium webdriver 学习总结-元素定位
Selenium webdriver 学习总结-元素定位 webdriver提供了丰富的API,有多种定位策略:id,name,css选择器,xpath等,其中css选择器定位元素效率相比xpath要 ...
- UML 序列图一点理解
激活状态,就是长方形该画到哪里呢?我的理解是,一个长方形代表调用函数的执行过程,比如下图 这个长方形就表示viewDidAppear()函数没有执行完,在这个长方形上发出或者收到的消息都是在 view ...
- Java for LeetCode 077 Combinations
Given two integers n and k, return all possible combinations of k numbers out of 1 ... n. For exampl ...
- 使用__declspec(export)导出C++类到DLL
使用举例: // File: SimpleDLLClass.h#ifdef SIMPLEDLL_EXPORT //在DLL工程属性-c/c++-预处理器-预处理器定义中添加此宏定义#define DL ...
- [Android Pro] Android签名与认证详细分析之二(CERT.RSA剖析)
转载自: http://www.thinksaas.cn/group/topic/335449/ http://blog.csdn.net/u010571535/article/details/899 ...
- July 12th, Week 29th Tuesday, 2016
When the traveler goes alone he gets acquainted with himself. 独自旅行可以让人更好地了解自己. With other's company, ...
- C/C++函数参数读取顺序2
#include <stdio.h>int main(){ int a=1,b=3,c=3; printf("%d, %d, %d",(a++,b++,c++),b,c ...