题目链接: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. iOS iOS9下修改回HTTP模式进行网络请求

    升级为iOS9后,默认请求类型为https,如何使用http进行请求会报错 The resource could not be loaded because the App Transport Sec ...

  2. 【转】在Spring中基于JDBC进行数据访问时怎么控制超时

    http://www.myexception.cn/database/1651797.html 在Spring中基于JDBC进行数据访问时如何控制超时 超时分类 超时根据作用域可做如下层级划分: Tr ...

  3. hdu4099

    要想通这个题目应该很容易,由于斐波纳契数在近100项之后很大,早就超出long long了.而输入最长的序列才40个数字,所以大约保留前50位,前40位是没有误差的!!!其实,想想我们判断double ...

  4. BZOJ 1951: [Sdoi2010]古代猪文( 数论 )

    显然答案是G^∑C(d,N)(d|N).O(N^0.5)枚举N的约数.取模的数999911659是质数, 考虑欧拉定理a^phi(p)=1(mod p)(a与p互质), 那么a^t mod p = a ...

  5. Android 汉字转拼音之工具篇

    /* * Copyright (C) 2011 The Android Open Source Project * * Licensed under the Apache License, Versi ...

  6. 本地环境下 WordPress 环境搭建与安装

    本地环境:Ubuntu 14.04 使用软件: WordPress 4.1.1 中文优化版 EasyEngine 安装步骤: 安装 LNMP 环境; wget -qO ee rt.cx/ee & ...

  7. web编程速度大比拼(nodejs go python)(非专业对比)

    C10K问题的解决,涌现出一大批新框架,或者新语言,那么问题来了:到底谁最快呢?非专业程序猿来个非专业对比. 比较程序:输出Hello World! 测试程序:siege –c 100 –r 100 ...

  8. hdu 2565 放大的X

    题目: http://acm.hdu.edu.cn/showproblem.php?pid=2565 这个题很简单 但是很容易错,写来给自己一个警示把 首先在最后一个x后面没有空格,然后就是那个换行一 ...

  9. SQL Server 中大小写区分的处理

    SQL Server 中大小写区分的处理. 默认情况下,SQL Server 里面是不区分大小写的: E:\>sqlcmd -S "localhost\SQLEXPRESS" ...

  10. Linux 用户信息,组信息,密码信息!

    1: 用户信息保存在  /etc/passwd 文件下 2: 密码信息保存在 /etc/shadow 3: 组相关的信息 /etc/group