链接:



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. Xgboost 模型保存和载入()

    https://blog.csdn.net/u012884015/article/details/78653178 xgb_model.get_booster().save_model('xgb.mo ...

  2. C#面试基础问题0

    传入某个属性的set方法的隐含参数的名称是什么?value,它的类型和属性所声名的类型相同. 如何在C#中实现继承?在类名后加上一个冒号,再加上基类的名称. C#支持多重继承么?不支持.可以用接口来实 ...

  3. jquery cookie操作方法

    1. 设置cookie的值,把name变量的值设为value     $.cookie(’name’, ‘value’);  2.新建一个cookie 包括有效期 路径 域名等 $.cookie(’n ...

  4. Mysql的replace into语句

    Mysql语句 replace into 跟 insert 功能类似,不同点在于:replace into 首先尝试插入数据到表中, 1. 如果发现表中已经有此行数据(根据主键或者唯一索引判断)则先删 ...

  5. MongoDB GridFS最佳应用概述

    <MongoDB GridFS最佳应用概述> 作者:chszs,转载需注明.博客主页:http://blog.csdn.net/chszs GridFS是MongoDB数据库之上的一个简单 ...

  6. Atitit.导出excel功能的设计 与解决方案

    Atitit.导出excel功能的设计 与解决方案 1.1. 项目起源于背景1 1.2. Js  jquery方案(推荐)jquery.table2excel1 1.3. 服务器方案2 1.4. 详细 ...

  7. Vivado使用技巧:封装自己设计的IP核

    概述   Vivado在设计时可以感觉到一种趋势,它鼓励用IP核的方式进行设计.“IP Integrator”提供了原理图设计的方式,只需要在其中调用设计好的IP核连线.IP核一部分来自于Xilinx ...

  8. [elk]logstash grok原理

    logstash语法 http://www.ttlsa.com/elk/elk-logstash-configuration-syntax/ https://www.elastic.co/guide/ ...

  9. 学习笔记:iOS 视图控制器(UIViewController)剖析

    转自:http://www.cnblogs.com/martin1009/archive/2012/06/01/2531136.html 视图控制器在iOS编程中占据非常重要的位置,因此我们一定要掌握 ...

  10. Spring事务的隔离级别

    1.  ISOLATION_DEFAULT: 这是一个 PlatfromTransactionManager  默认的隔离级别,使用数据库默认的事务隔离级别. 另外四个与 JDBC的隔离级别相对应: ...