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. cer证书签名验证

    一个cer还需要一个签名的证书本身,这是为了防止cer证书被篡改. 有两种类型的证书: 1. 根证书 2. 由根证书颁发子证书. 特根证书.它是自签名. 而其它子证书的签名公钥都保存在它的上级证书里面 ...

  2. c++的vector容器

    c++还有一个很常用的容器就是vector容器,他是数组实现的,是一种可变长的容器,在很多的时候可以简化我们的编程.可学习的链接:http://www.cnblogs.com/mr-wid/archi ...

  3. CUMCM--总结

    有些事情经历一次就好,一次的经历足以成长. 其实,对于数模真的没什么要说的,也没什么好写的.从9月11日,8点见到赛题,到今天早上8点的提交,短短的三天,度过寂静的黑夜,见到微曦的黎明.三天三夜,9个 ...

  4. crawler_如何从页面获取新浪cookie

    步奏如下: 1 用chrome浏览器(其他浏览器原理相同)打开地址: http://weibo.com/ 2.点击鼠标右键 右键点击查看元素 点击Network   3.输入用户名  密码 执行登录 ...

  5. php_windows搭建

    Php文件放到: D:\web\Apache2.2\htdocs 修改 D:\web\PHP\php.ini 拓展 修改 D:\web\Apache2.2\conf\httpd.conf  支持php ...

  6. HDU Billboard

    题目分析:给你n张海报,一个宣传板.让你在满足海报能够贴在最高位置的时候则贴的最高,无法满足时贴的最靠左,输出海报所贴的高度.假设不能贴则输出-1. 一道非常easy,可是我没想出的基础线段树. 算法 ...

  7. js中从blob提取二进制

    文章结构: 一.所遇到的问题 二.解决方法 一. 服务器端通过websocket向浏览器端传输图片(二进制),需要根据不同的图片把图片显示在不同的位置,可行的一个方法是先把图片转化成二进制数组,再把二 ...

  8. C# WebBrowser 代理的使用

    原文:C# WebBrowser 代理的使用 The WebBrowser control is just an embeddded IE Control, I believe any setting ...

  9. APP 半自适应 WEB页面

    特别赶,响应式纯自适应的,有空写了新的发. (在手机上看,页面上看一定乱) <!DOCTYPE html><html> <head> <meta http-e ...

  10. ORACLE 中IN和EXISTS比较

    ORACLE 中IN和EXISTS比较 EXISTS的执行流程      select * from t1 where exists ( select null from t2 where y = x ...