题目链接: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)的更多相关文章

  1. HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】

    HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...

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

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

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

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

  4. 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 ...

  5. HDU 1423 Greatest Common Increasing Subsequence LCIS

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

  6. HDU 1423 Greatest Common Increasing Subsequence(LCIS)

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

  7. HDUOJ ---1423 Greatest Common Increasing Subsequence(LCS)

    Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536 ...

  8. HDU 1423 Greatest Common Increasing Subsequence(LICS入门,只要求出最长数)

    Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536 ...

  9. 【HDOJ】1423 Greatest Common Increasing Subsequence

    LCIS /* 1423 */ #include <cstdio> #include <cstring> #include <cstdlib> #define MA ...

随机推荐

  1. “Zhuang.Data”轻型数据库访问框架(一)开篇介绍

    目录: “Zhuang.Data”轻型数据库访问框架(一)开篇介绍 “Zhuang.Data”轻型数据库访问框架(二)框架的入口DbAccessor对象 框架介绍 该框架主要用于数据库访问,封装了包括 ...

  2. kruskal算法求最小生成树(jungle roads的kruskal解法)

    注意: 注意数组越界问题(提交出现runtimeError代表数组越界) 刚开始提交的时候,边集中边的数目和点集中点的数目用的同一个宏定义,但是宏定义是按照点的最大数定义的,所以提交的时候出现了数组越 ...

  3. vi、vim 配置上下左右方向键和删除键

    vi.vim 配置上下左右方向键和删除键 " An example for a vimrc file. " " Maintainer: Bram Moolenaar &l ...

  4. python 发送安全邮件

    用python 写了一个发送邮件的脚本,配上host 和端口,发现一直报错: smtplib.SMTPException: No suitable authentication method foun ...

  5. android studio2.0 搭建Robotium环境--有被测源代码的情况下

    1.导入robotium-solo-5.2.1.jar 包    导入junit:4.12.jar2.app- -src- -main- -libs 或者app-libs下 复制进去后,右键add a ...

  6. IOS UTI统一类型标识符:判断文件类型通过后缀

    今天在学习文档和数据共享中,首先讲的处理统一类型标识符UTI.第一次见,所以记下来以备之用,首先了解UTI和MIME的概念 1.同一类型标识符(Uniform Type Identifier,UTI) ...

  7. SublimeLinter

    SublimeLinter 关于代码检查 郑重推荐这个插件 官方插件库中仅有sublime3 有此插件 (对于ST2 有个sublimeLinter for ST2 没试过) SublimeLinte ...

  8. linux 怎么查找oracle11g的安装目录

    一般来说,/etc/oraInst.loc文件里会记录oracle的路径,如[oracle@ruby ~]$ cat /etc/oraInst.loc inventory_loc=/u01/app/o ...

  9. POJ 2774 Long Long Message(后缀数组)

    [题目链接] http://poj.org/problem?id=2774 [题目大意] 求最长公共子串 [题解] 将两个串中间嵌一个字符相连,求一遍后缀数组 如果排名相邻的两个后缀的开端是分属于两个 ...

  10. Ubuntu 麒麟版下安装:Apache+php5+mysql+phpmyadmin.

    摘要 LAMP是Linux web服务器组合套装的缩写,分别是Apache+MySQL+PHP.此文记录在Ubuntu上安装Apache2服务器,包括PHP5(mod_php)+MySQL+phpmy ...