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

Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)

Total Submission(s): 8768 Accepted Submission(s): 2831

Problem Description

This is a problem from ZOJ 2432.To make it easyer,you just need output the length of the subsequence.

Input

Each sequence is described with M - its length (1 <= M <= 500) and M integer numbers Ai (-2^31 <= Ai < 2^31) - the sequence itself.

Output

output print L - the length of the greatest common increasing subsequence of both sequences.

Sample Input

1

5

1 4 2 5 -12

4

-12 1 2 4

Sample Output

2

题意

找出两个序列中的最长公共上升子序列

思路

动态规划

假如 a[i] != b[j]

那么毫无疑问 a[i] 对这个LCIS是毫无贡献的 所以 dp[i][j] = dp[i - 1][j];

如果a[i] == b[j]

那么 这个最长公共上升子序列的长度至少为1 并且 找出前面可以接的最长的LCIS的长度 + 1 就可以了

AC代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <cstdlib>
#include <ctype.h>
#include <numeric>
#include <sstream>
using namespace std;

typedef long long LL;
const double PI = 3.14159265358979323846264338327;
const double E = 2.718281828459;
const double eps = 1e-6;
const int MAXN = 0x3f3f3f3f;
const int MINN = 0xc0c0c0c0;
const int maxn = 1e2 * 5 + 5;
const int MOD = 1e9 + 7;
int a[maxn], b[maxn], dp[maxn][maxn];
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n, m;
        scanf(" %d", &n);
        int i, j, k;
        for (i = 0; i < n; i++)
            scanf("%d", &a[i]);
        scanf("%d", &m);
        for (i = 0; i < m; i++)
            scanf("%d", &b[i]);
        memset(dp, 0, sizeof(dp));
        int ans = 0;
        for (i = 0; i < n; i++)
        {
            for (j = 0; j < m; j++)
            {
                if (a[i] == b[j])
                {
                    int max_dp = 0;
                    for (k = 0; k < j; k++)
                    {
                        if (dp[i][k] > max_dp && b[j] > b[k])
                            max_dp = dp[i][k];
                    }
                    dp[i][j] += max_dp + 1;
                }
                else if (i)
                    dp[i][j] = dp[i - 1][j];
                if (dp[i][j] > ans)
                    ans = dp[i][j];
            }
        }
        cout << ans << endl;
        if (t)
            cout << endl;
    }
}

优化代码

#include <iostream>             //时间优化
#include <cstdio>
#include <algorithm>
#include <cmath>
#include <deque>
#include <vector>
#include <queue>
#include <string>
#include <cstring>
#include <map>
#include <stack>
#include <set>
#include <cstdlib>
#include <ctype.h>
#include <numeric>
#include <sstream>
using namespace std;

typedef long long LL;
const double PI = 3.14159265358979323846264338327;
const double E = 2.718281828459;
const double eps = 1e-6;
const int MAXN = 0x3f3f3f3f;
const int MINN = 0xc0c0c0c0;
const int maxn = 1e2 * 5 + 5;
const int MOD = 1e9 + 7;
int a[maxn], b[maxn], dp[maxn][maxn];
int main()
{
    int t;
    cin >> t;
    while (t--)
    {
        int n, m;
        scanf(" %d", &n);
        int i, j, k;
        for (i = 0; i < n; i++)
            scanf("%d", &a[i]);
        scanf("%d", &m);
        for (i = 0; i < m; i++)
            scanf("%d", &b[i]);
        memset(dp, 0, sizeof(dp));
        int ans = 0;
        int max_dp;
        for (i = 0; i < n; i++)
        {
            max_dp = 0;
            for (j = 0; j < m; j++)
            {
                if (i)
                    dp[i][j] = dp[i - 1][j];
                if (a[i] > b[j] && dp[i][j] > max_dp)
                    max_dp = dp[i][j];
                if (a[i] == b[j])
                    dp[i][j] = max_dp + 1;
                if (dp[i][j] > ans)
                    ans = dp[i][j];
            }
        }
        cout << ans << endl;
        if (t)
            cout << endl;
    }
}

HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】的更多相关文章

  1. ZOJ 2432 Greatest Common Increasing Subsequence(最长公共上升子序列+路径打印)

    Greatest Common Increasing Subsequence 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...

  2. HDOJ 1423 Greatest Common Increasing Subsequence(dp)

    题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=1423 思路分析:[问题定义]给定两个序列A[0, 1,..., m]和B[0, 1, ..., n], ...

  3. 【简单dp】poj 2127 Greatest Common Increasing Subsequence【最长公共上升子序列】【模板】

    Sample Input 5 1 4 2 5 -12 4 -12 1 2 4 Sample Output 2 1 4 题目:给你两个数字序列,求出这两个序列的最长公共上升子序列.输出最长的长度,并打表 ...

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

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

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

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

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

  7. HDU 1423 Greatest Common Increasing Subsequence LCIS

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

  8. HDU 1423 Greatest Common Increasing Subsequence(LCIS)

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

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

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

随机推荐

  1. SSH初体验系列--Hibernate--1--环境配置及demo

    最近在学hibernate,常见的教程都是搭配mysql,因为公司本地电脑用的是pg,所以就尝试着做个pg的小demo. 自己也是边学边写,只当是加深印象.话不多说,直接开始; 一) 准备工作; 1) ...

  2. nodejs操作图片方法

    最近项目中用到nodejs做图片服务器,用户上传图片生成缩略图返回地址一系列操作. 原来一直用.net平台,所有都封装好了生成缩略图这种分分钟就解决了,遂度娘一番全是调用imagemagick和gra ...

  3. 重写(Override)

    重写(Override) 重写是子类对父类的允许访问的方法的实现过程进行重新编写, 返回值和形参都不能改变.即外壳不变,核心重写! 重写的好处在于子类可以根据需要,定义特定于自己的行为. 也就是说子类 ...

  4. HashTable类

    HashTable类不紧可以像Vector类一样动态的存储一系列的对象,而且对存储的每一个对象(称为值)都安排另一个对象(称为关键字)与它相关联. 用做关键字的类必须覆盖Object.hashCode ...

  5. C# 正则表达式替换换行

    将换行替换成"\n"字符,如下: 代码一: string strContent = txtNote.Text; strContent = Regex.Replace(strCont ...

  6. 解决使用VS2013创建的MVC项目在VS2015中打开的各种问题

    其实很多问题一直都存在,但是因为其不影响编译结果和运行结果,所以我也就一直没理它.但是问题放在那一直存在,偶尔想起来还有某些问题没解决的时候心里总是感觉不得劲,所以今天就彻底的查查资料解决了. 问题一 ...

  7. CI和CD的意思

    openstack中CI和CD的意思: 持续集成(CI)和持续交付(CD)

  8. MySQL中的日期和时间函数

    常用日期函数如下: 函   数 功   能 CURDATE() 获取当前日期 CURTIME() 获取当前时间 NOW() 获取当前的日期和时间 UNIX_TIMESTAMP(date) 获取日期的U ...

  9. 《基础知识》hashCode与equals的区别与联系

    一.equals方法的作用 1.默认情况(没有覆盖equals方法)下equals方法都是调用Object类的equals方法,而Object的equals方法主要用于判断对象的内存地址引用是不是同一 ...

  10. Hadoop2.0中单点故障解决方案分析

    Hadoop 1.0内核主要由两个分支组成:MapReduce和HDFS,众所周知,这两个系统的设计缺陷是单点故障,即MR的JobTracker和HDFS的NameNode两个核心服务均存在单点问题, ...