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 ...
随机推荐
- iOS 设置UIDatePiicer为24小时制
直接上代码: NSDateFormatter* dateFormatter = [[NSDateFormatter alloc] init]; [dateFormatter setDateFormat ...
- CKEditor + CKFinder 实现编辑上传图片配置
下载最新版 ckfinder 本人下载的php版本 https://cksource.com/ckfinder/download 下载最新版ckeditor http://ckeditor.com/ ...
- hadoop搭建杂记:Linux下ssh免密码登陆
关于ssh免密码登陆的问题 关于ssh免密码登陆的问题 linux下可以用ssh-keygen来生成公钥/私钥对 ①生成id_rsa和id_rsa.pub公钥/私钥对,自动在~/.ssh下生成文件(亦 ...
- 单例模式java实现
package Counter; public class Counter { private int counter; private static Counter instance ...
- 在CentOS 7 / Gnome 3 双屏时设置主屏
在Windows中设置扩展显示器为主屏的方式非常清楚,但在Linux中就不是那么明显了,下面介绍如何完成这个设置 ------------------------------------------- ...
- java_httpservice
http://blog.csdn.net/maosijunzi/article/details/41045181
- break在switch中的使用例子
/* Name:break在switch中的使用例子 Copyright: By.不懂网络 Author: Yangbin Date:2014年2月21日 03:16:52 Description:以 ...
- 《Pointers On C》读书笔记(第一章 快速上手)
1.C语言是一种自由格式的程序设计语言,没有规则要求我们必须如何书写语句.然而,如果我们在编写程序时能够遵守一些约定还是非常值得的,它可以使代码更加容易阅读和修改.另外,预处理命令有较为严格的规则. ...
- Wide character in print at hcp.pl line 21.
jrhmpt01:/root# cat -n hcp.pl 1 use LWP::UserAgent; 2 use Encode; 3 $ua = LWP::UserAgent->new; 4 ...
- 51nod 1244 莫比乌斯函数之和(杜教筛)
[题目链接] http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1244 [题目大意] 计算莫比乌斯函数的区段和 [题解] 利 ...