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
 

题意:求最长递增公共子序列的长度

思路:直接模板

#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std; int n,m,a[505],b[505],dp[505][505]; int LICS()
{
int MAX,i,j;
memset(dp,0,sizeof(dp));
for(i = 1; i<=n; i++)
{
MAX = 0;
for(j = 1; j<=m; j++)
{
dp[i][j] = dp[i-1][j];
if(a[i]>b[j] && MAX<dp[i-1][j])
MAX = dp[i-1][j];
if(a[i]==b[j])
dp[i][j] = MAX+1;
}
}
MAX = 0;
for(i = 1; i<=m; i++)
if(MAX<dp[n][i])
MAX = dp[n][i];
return MAX;
} int main()
{
int i,t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i = 1; i<=n; i++)
scanf("%d",&a[i]);
scanf("%d",&m);
for(i = 1; i<=m; i++)
scanf("%d",&b[i]);
printf("%d\n",LICS());
if(t)
printf("\n");
} return 0;
}

上面的虽然可以解决,但是二维浪费空间较大,我们注意到在LICS函数中有一句dp[i][j] = dp[i-1][j],这证明dp数组前后没有变化!于是可以优化成一维数组!

#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int a[505],b[505],dp[505],n,m; int LICS()
{
int i,j,MAX;
memset(dp,0,sizeof(dp));
for(i = 1; i<=n; i++)
{
MAX = 0;
for(j = 1; j<=m; j++)
{
if(a[i]>b[j] && MAX<dp[j])
MAX = dp[j];
if(a[i]==b[j])
dp[j] = MAX+1;
}
}
MAX = 0;
for(i = 1; i<=m; i++)
if(MAX<dp[i])
MAX = dp[i];
return MAX;
} int main()
{
int t,i;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i = 1; i<=n; i++)
scanf("%d",&a[i]);
scanf("%d",&m);
for(i = 1; i<=m; i++)
scanf("%d",&b[i]);
printf("%d\n",LICS());
if(t)
printf("\n");
} return 0;
}

HDU1423:Greatest Common Increasing Subsequence(LICS)的更多相关文章

  1. HDU1423:Greatest Common Increasing Subsequence

    浅谈\(DP\):https://www.cnblogs.com/AKMer/p/10437525.html 题目传送门:http://acm.hdu.edu.cn/showproblem.php?p ...

  2. HDU 1423 Greatest Common Increasing Subsequence(LICS入门,只要求出最长数)

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

  3. Greatest Common Increasing Subsequence hdu1423

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

  4. HDU 1423 Greatest Common Increasing Subsequence LCIS

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

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

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

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

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

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

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

  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. Android(Lollipop/5.0) Material Design(六) 自定义动画

    官网地址:https://developer.android.com/intl/zh-tw/training/material/animations.html 动画在Material设计中,为用户与a ...

  2. HDU 1203 I NEED A OFFER!(dp)

    Problem Description Speakless很长时间,我想出国.现在,他已经完成了所有需要的检查.准备好所有要准备的材料,于是,便须要去申请学校了.要申请国外的不论什么大学.你都要交纳一 ...

  3. STL algorithmi算法s_sorted和is_sorted_until(28)

    is_sort原型: ::is_sorted default (1) template <class ForwardIterator> bool is_sorted (ForwardIte ...

  4. .pb.h:9:42: fatal error: google/protobuf/stubs/common.h: No such file or directory

    看看这个你应该知道,找不到头文件,它可用于g++ 的-I 参数: -I/usr/local/lib/protobuf/include如需订购g++在/usr/local/lib/protobuf 以上 ...

  5. MyEclipse2014 设备 checkstyle、PMD、findbugs 最简单的方法 详细说明

    最近的实验需要的代码审查和应用程序性能优化.在需求MyEclipse安装某些插件,由于如今的MyEclipse版本号和大多数教程的不一样了,一些安装选项也已经改变,所以安装起来非常费事,通过不断的尝试 ...

  6. Before和After用法小结

    Before和After用法小结 定义 :before 选择器在被选元素的内容前面插入内容.:after选择器在被选元素的内容后面插入内容.(注:必须包含content 属性) 一.特性:不能左右:e ...

  7. 14行脚本配置Linux下一个Java环境变量

    供Java人们刚开始学习.多半Java它需要花费大量的精力在开发环境的配置,于Linux下一个,构造Java环境变量,很可能加入这一努力. 为此,我做了一个bash脚本来配置自己主动Java环境变量. ...

  8. 左右linuxserver自己主动重启过程监控和简单的解决方案

    转载请注明出处:帘卷西风的专栏(http://blog.csdn.net/ljxfblog) 本周開始,新手游进行删档封測阶段,前两天表现还好,今天更新后出现几次宕机行为.比較影响玩家的測试和体验,我 ...

  9. linux复制文件命令scp

    linux大多数复制我们的递送工具使用,有着ftp,scp等一下. 当中scp命令很easy快捷, 本机到远程:scp (-r) 本地目录或者文件路径 远程ip:目录 远程到本机:scp (-r) 远 ...

  10. Gaea是支持跨平台具有高并发、高性能、高可靠性,并提供异步、多协议、事件驱动的中间层服务框架

    Gaea是支持跨平台具有高并发.高性能.高可靠性,并提供异步.多协议.事件驱动的中间层服务框架 Gaea:58同城开源的中间层服务框架 https://github.com/58code/Gaea 中 ...