链接:



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. 5V转3.3v电路

    方案一: MIC5205-3.3 输出电流150ma 输出电压3.3V 其中:CT24为钽电容: 方案二: AMS1117-3.3 输出电流800ma 输出电压:3.3V 输入电压:4.75~12v

  2. Atitit.提升软件Web应用程序 app性能的方法原理 h5 js java c# php python android .net

    Atitit.提升软件Web应用程序 app性能的方法原理 h5 js java c# php python android .net 1. 提升单例有能力的1 2. 减少工作数量2 2.1. 减少距 ...

  3. Verilog语言:还真的是人格分裂的语言

    人气腹语术师天愿在现场披露了被人偶搭档夺取灵魂的腹语术师将妻子杀害的表演节目.天愿真的陷入了多重人格,命令自己杀害妻子和子的人偶的人格出现了.为了不(让自己)杀害和弟子登川有外遇的妻子,天愿提出委托想 ...

  4. git commit 、CHANGELOG 和版本发布的标准自动化

    一直以来,因为团队项目迭代节奏很快,每次发布的更新日志和版本更新都是通过人肉来完成的.有时候实在忙的团团转,对于手动的写这些更新信息就显得力不从心了.对于团队新来的小伙伴,有时候遇到些紧急情况,就更显 ...

  5. SQL基础之聚合与排序

    聚合函数是用来求和,平均值,最大最小值一类的函数. 常用的有COUNT.SUM.MAX.MIN.AVG. count() 参数为列名,也可以使用*,表示全部列. 默认*会统计所有行的数据,如果想过滤掉 ...

  6. react-native在win10下用adb报错的解决方案

    react-native在WIN10上面运行用adb链接模拟器,会直接在powershell里报错, 报错大概意思是识别不了adb这个cmdlet函数. 找了很久的解决办法,直接找到adb.exe所在 ...

  7. jsp中 自定义 tag的几种方式

    在jsp文件中,可以引用tag和tld文件. 1.对于tag文件,使用tagdir引用(这个直接是引用的后缀tag文件的jsp文件) <%@ taglib prefix="ui&quo ...

  8. ubuntu MySQL数据库输入中文乱码 解决方案

    一.登录MySQL查看用SHOW VARIABLES LIKE ‘character%’;下字符集,显示如下:+--------------------------+----------------- ...

  9. iOS开发之-收集的不常用却实用的小方法和技巧

    原文链接:http://blog.5ibc.net/p/90386.html 1.颜色转变成图片 - (UIImage *)createImageWithColor:(UIColor *)color ...

  10. tomcat 内存溢出原因分析及解决

    一.错误提示:java.lang.OutOfMemoryError: Java heap space [原因分析] tomcat默认可以使用内存为128MB,在较大型的应用项目中不足以满足运行要求,在 ...