HDOJ 1423 Greatest Common Increasing Subsequence(dp)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1423
思路分析:[问题定义]给定两个序列A[0, 1,..., m]和B[0, 1, ..., n],要求这两个序列的最长公共上升子序列;
该问题为动态规划问题,可以采用与求最长公共子序列与最长上升子序列相同的思想来思考求出动态规划方程;
定义状态dp[i][j] 为序列A的前 i 个数字与序列B 的最长公共上升子序列,且该最长公共上升子序列以序列B的第 j 个数字为最后一个数字;
则可以推导出动态转移方程为:
if A[i] != B[j], dp[i][j] = dp[i-1][j];
if A[i] == B[j], dp[i][j] = MAX{dp[i-1][k]} + 1, 0 <= k <j && B[k] < B[j];
代码如下:
#include <iostream>
using namespace std; const int MAX_N = + ;
int dp[MAX_N][MAX_N];
int num_a[MAX_N], num_b[MAX_N]; int main()
{
int test_case, ans;
int len_a, len_b; scanf("%d", &test_case);
while (test_case--)
{
scanf("%d", &len_a);
for (int i = ; i <= len_a; ++i)
scanf("%d", &num_a[i]);
scanf("%d", &len_b);
for (int i = ; i <= len_b; ++i)
scanf("%d", &num_b[i]); ans = ;
memset(dp, , sizeof(dp));
for (int i = ; i <= len_a; ++i)
{
for (int j = ; j <= len_b; ++j)
{
if (num_a[i] != num_b[j])
dp[i][j] = dp[i - ][j];
else
{
int max = ; for (int k = ; k < j; ++k)
{
if (num_b[j] > num_b[k] && dp[i - ][k] > max)
max = dp[i - ][k];
}
dp[i][j] = max + ;
}
}
} for (int i = ; i <= len_a; ++i)
{
if (dp[len_a][i] > ans)
ans = dp[len_a][i];
}
printf("%d\n", ans);
if (test_case)
printf("\n");
} return ;
}
HDOJ 1423 Greatest Common Increasing Subsequence(dp)的更多相关文章
- HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】
HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...
- HDOJ 1423 Greatest Common Increasing Subsequence -- 动态规划
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1423 Problem Description This is a problem from ZOJ 2 ...
- HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS)
HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS) http://acm.hdu.edu.cn/showproblem.php?pi ...
- 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 ...
- 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)
Greatest Common Increasing Subsequenc Problem Description This is a problem from ZOJ 2432.To make it ...
- HDUOJ ---1423 Greatest Common Increasing Subsequence(LCS)
Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
- HDU 1423 Greatest Common Increasing Subsequence(LICS入门,只要求出最长数)
Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
- 【HDOJ】1423 Greatest Common Increasing Subsequence
LCIS /* 1423 */ #include <cstdio> #include <cstring> #include <cstdlib> #define MA ...
随机推荐
- BZOJ 3439: Kpm的MC密码( trie + DFS序 + 主席树 )
把串倒过来插进trie上, 那么一个串的kpm串就是在以这个串最后一个为根的子树, 子树k大值的经典问题用dfs序+可持久化线段树就可以O(NlogN)解决 --------------------- ...
- C++学习之使用new的注意事项
C++学习之使用new的注意事项 在构造函数中使用new来初始化对象的指针成员成员时必须特别小心,具体的说,应该如下这样做: 一.如果在构造函数中使用new来初始化指针成员,则应该在析构函 ...
- Do not go gentle into that good night
Do not go gentle into that good night By:Dylan Thomas Do not go gentle into that good night,Old ag ...
- win7系统中桌面图标显示不正常问题
http://jingyan.baidu.com/article/466506580c9327f549e5f8dc.html 最近笔者在安装软件时,突然出现了桌面图标显示不正常了,一开始还以为是新安装 ...
- MYSQL this function has none of deterministic no sql ......错误
This function has none of DETERMINISTIC, NO SQL解决办法 创建存储过程时 出错信息: ERROR 1418 (HY000): This function ...
- css多行文本垂直居中问题研究
css多行文本垂直居中问题研究 <body> <h2>垂直居中对齐</h2> <style> *{margin:0; padding:0;} div { ...
- ComboBox控件绑定数据源
最近在研究机房收费系统的组合查询的方法时,看到了ComboBox控件可以进行数据绑定,我觉得这个功能真的很不错,可以给我省去很多的麻烦. 下面是我组合查询窗体界面 一.数据转换方法 现在我们开看一下我 ...
- python总结
环境:django,numpy,matplotlib, 解释语言:开发效率高,通用性强,内置方便的数据容器,易于扩展和嵌入. 语言:lua--嵌入式/网络/APP,erlang--嵌入式,python ...
- HDU 2167 Pebbles
题目大意:有个N*N( 3<=N<=15 )方阵, 可从中若干个数, 使其总和最大.取数要求, 当某一个数被选, 其周围8个数都不能选. 题解:记s数组为合法状态,即没有相邻的数字同时被选 ...
- Git安装及基本使用
准备: Git软件,github账号. Git安装: 直接百度搜git下载,windows和mac不同平台的.官网上的下载地址很慢或者根本下不了. 默认配置安装. github: 网址:https:/ ...