链接:



Greatest Common Increasing Subsequence

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

Total Submission(s): 2757    Accepted Submission(s): 855

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
 
Source
 
Recommend
lcy



算法:

LCIS 【最长公共上升子序列分析


code:

注意格式 问题:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std; const int maxn = 500+50;
int dp[maxn][maxn];
int a[maxn],b[maxn];
int m,n; /****
求序列 A 长度为 N 和序列 B 长度为 M 的 LCS
序列下标从 1 开始
*/
int LCS()
{
    for(int i = 1; i <= n; i++)
    {
        int tmp = 0; //记录在i确定,且a[i]>b[j]的时候dp[i,j]的最大值
        for(int j = 1; j <= m; j++)
        {
            dp[i][j] = dp[i-1][j];
            if(a[i] > b[j])
            {
                tmp = dp[i-1][j];
            }
            else if(a[i] == b[j])
                dp[i][j] = tmp+1;
        }
    }
//for(int i = 1; i <= m; i++) printf("%d ", dp[n][i]); printf("\n");     int ans = 0;
    for(int i = 1; i <= m; i++)
        ans = max(ans, dp[n][i]);
    return ans; } int main()
{
    int T;
    scanf("%d", &T);
    while(T--)
    {
        memset(dp,0,sizeof(dp));         scanf("%d", &n);
        for(int i = 1; i <= n; i++)
            scanf("%d", &a[i]);
        scanf("%d", &m);
        for(int j = 1; j <= m; j++)
            scanf("%d", &b[j]);         printf("%d\n",LCS());
        if(T != 0) printf("\n");
    }
}



内存优化:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std; const int maxn = 500+50;
int dp[maxn];
int a[maxn],b[maxn];
int m,n; /****
求序列 A 长度为 N 和序列 B 长度为 M 的 LCS
序列下标从 1 开始
*/
int LCS()
{
for(int i = 1; i <= n; i++)
{
int tmp = 0;
for(int j = 1; j <= m; j++)
{
if(a[i] > b[j] && dp[j] > tmp)
{
tmp = dp[j];
}
else if(a[i] == b[j])
dp[j] = tmp+1;
}
} int ans = 0;
for(int i = 1; i <= m; i++)
ans = max(ans, dp[i]);
return ans;
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
memset(dp,0,sizeof(dp)); scanf("%d", &n);
for(int i = 1; i <= n; i++)
scanf("%d", &a[i]);
scanf("%d", &m);
for(int j = 1; j <= m; j++)
scanf("%d", &b[j]); printf("%d\n",LCS());
if(T != 0) printf("\n");
}
}
















POJ 1423 Greatest Common Increasing Subsequence【裸LCIS】的更多相关文章

  1. HDU 1423 Greatest Common Increasing Subsequence(LCIS)

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

  2. 1423 Greatest Common Increasing Subsequence (LCIS)

    讲解摘自百度; 最长公共上升子序列(LCIS)的O(n^2)算法? 预备知识:动态规划的基本思想,LCS,LIS.? 问题:字符串a,字符串b,求a和b的LCIS(最长公共上升子序列).? 首先我们可 ...

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

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

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

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

  5. HDU 1423 Greatest Common Increasing Subsequence LCIS

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

  6. POJ 2127 Greatest Common Increasing Subsequence -- 动态规划

    题目地址:http://poj.org/problem?id=2127 Description You are given two sequences of integer numbers. Writ ...

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

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

  8. POJ 2127 Greatest Common Increasing Subsequence

    You are given two sequences of integer numbers. Write a program to determine their common increasing ...

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

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

随机推荐

  1. size_t详细解释

    在学习sizeof运算符的时候,它的值类型为size_t,结果在使用printf函数显示的时候,凭空多了很多警告,有点不不理解,为啥搞这么复杂?直接用个int类型多省事? 经过一番搜索和阅读文档,找到 ...

  2. 调试JDK1.8源码的方法

    背景 在学习JDK源码的时候,免不了需要调试JDK的源码. 比如:想理解ConcurrentHashMap的put(K k, V v)方法,JDK自带的rt.jar文件是支持断点调试,但是却看不到变量 ...

  3. JS高程3:BOM-window对象

    全局作用域 BOM的核心就是window对象,他是浏览器的一个实例. 它既是JS访问浏览器窗口的接口,又是ECMAScript中的global对象. 在全局作用域中,global对象,this对象,w ...

  4. CentOS上扩充lv-root空间步骤详解

    查看服务器发现vg_host01-lv_root下的空间占用的比较多,需要扩容. 有以下两种方案: )利用空余的磁盘,扩展lv_root的大小(推荐) )将lv_home的空间挪出一部分给lv_roo ...

  5. 快速解读GC日志

    本文是 Plumbr 发行的 Java垃圾收集指南 的部分内容.文中将介绍GC日志的输出格式, 以及如何解读GC日志, 从中提取有用的信息.我们通过 -XX:+UseSerialGC 选项,指定JVM ...

  6. CSS学习笔记(3)--表格边框

    http://www.alixixi.com/web/a/2009082657736.shtml 对于很多初学HTML的人来说,表格<table>是最常用的标签了,但对于表格边框的控制,很 ...

  7. iOS 小数点精度问题处理

    实际开发中有时候经常会需要去处理小数点精度问题,如果是整数保留整数,如果有小数则保留不为0的的小数,最大2位小数 eg: 2.013 ---> 2.01 2.0 ---> 2 2.10 - ...

  8. iOS-回收键盘的几种方法

    在开发过程中,为了实现点击屏幕其它位置收起键盘的目的,我们使用过许多的方法. 如果是在UIViewController中收起键盘,除了通过调用控件的resignFirstResponder方法外,还有 ...

  9. python学习笔记3---浅拷贝和深拷贝,file操作

    import copy a=[1,2,3,['a','b']] b=a c= copy.copy(a)---浅拷贝 d=copy.deepcopy(a)---深拷贝 file操作: python 文件 ...

  10. linux学习笔记5--命令rmdir和rm

    昨天学习了创建目录的命令mkdir ,接下来学习一下linux中删除文件和目录的命令: rm命令. rm是一个危险的命令,使用的时候要特别当心,尤其对于新手,否则整个系统就会毁在这个命令(比如在/(根 ...