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 ...
随机推荐
- Qt(QML)本地化
Internationalization and Localization with Qt Quick 程序国际化 1) Use qsTr() for all Literial UI strings ...
- 提供一段Excel获取Title的标题,类似于A、AA、AAA,我们操作Excel的时候通常根据次标题来获取一定的操作范围。
/******************************************** FormatExcelColumTitle Purpose Get excel title like &qu ...
- centos 安装mysql密码修改后还是不能连接的原因
centos 上安装mysql密码修改后还是不能连接出现错误:ERROR 1142 (42000): SELECT command denied to user ''@'localhost' for ...
- Ie浏览器TextBox文本未居中
Ie浏览器TextBox文本未居中,而其他浏览器无问题时,可能原因是未设置垂直居中 vertical-align:middle
- Unity3D Android手机开发环境配置
Unity3D Android手机开发环境配置 Date:2014-01-01 07:09 1.配置eclipse环境:首先在官网下载安装包:http://developer.android.com/ ...
- zoj 3171 The Hidden 7's
这道题,我在网上看到两种dp,不过基本原理是一样的,不过感觉还是后面的一种比较巧妙!因为我对动态不是很熟,自能加上一些自己的理解,写上注释. 1) #include <stdio.h> # ...
- SQL Server 数据库所有者
1. 数据库所有者应当永远是 sa 用户 2. 改变数据库的所有者 alter authorization on database :: databaseName to sa; -- 这一句话把数据库 ...
- SQL Server 数据类型陷阱
1. bit 类型:bit(1) 不要以为它只占一个位,事实上它要占一个字节!也就是说当n < 8 时都是这样的! 2. varchar(n) 这里的n不能大于8000,如果想要比8000大你 ...
- Google浏览器调试js
1.进入source找到js位置 一般js文件就到js目录下找,tpl中的就到tpl中找. 我这次写在tpl中的,就到list中找,它就把list中的js单独显示出来了. 2.设置断点,进行排错.刷新 ...
- POJ 3261 Milk Patterns(后缀数组+二分答案)
[题目链接] http://poj.org/problem?id=3261 [题目大意] 求最长可允许重叠的出现次数不小于k的子串. [题解] 对原串做一遍后缀数组,二分子串长度x,将前缀相同长度超过 ...