Greatest Common Increasing Subsequence

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 5444    Accepted Submission(s): 1755

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   |   We have carefully selected several similar problems for you:  1422 1400 1418 1424 1401 

四种方法代码:

每一种都按照我觉得最容易理解的思路和最精简的写法写了,有些地方i 或者 i-1都可以,不必纠结。

#include<algorithm>
#include<string>
#include<string.h>
#include<stdio.h>
#include<iostream>
using namespace std;
#define N 505 int a[N],l1,l2,b[N],ma,f[N][N],d[N];
void solve1()//O(n^4)
{
ma = ;
memset(f, , sizeof(f) );
for(int i = ; i <= l1; i++)
for(int j = ; j <= l2; j++)
{
if(a[i-] == b[j-])
{
int ma1 = ;
for(int i1 = ; i1 < i; i1++)
for(int j1 = ; j1 < j; j1++)
if(a[i1-] == b[j1-] && a[i1-] < a[i-] && f[i1][j1] > ma1)//实际上a[i-1]==b[j1-1]可以省,因为既然f[i1][j1]+1>f[i][j]了,
ma1 = f[i1][j1]; /*说明肯定a[i-1]==b[j1-1]了,因为这种解法只有当a[i-1]==b[j-1]时,f[i][j]才不等于0*/
f[i][j] = ma1 + ;
}
ma = max(ma, f[i][j]);
}
cout<<ma<<endl;
}
void solve2()//O(n^3)
{
ma = ;
memset(f, , sizeof(f) );
for(int i = ; i <= l1; i++)
for(int j = ; j <= l2; j++)
{
f[i][j] = f[i-][j];
if(a[i-] == b[j-])
{
int ma1 = ;
for(int j1 = ; j1 < j; j1++)
{
if(b[j1-] < b[j-] && f[i-][j1] > ma1)
ma1 = f[i-][j1];
}
f[i][j] = ma1 + ;
}
ma = max(ma, f[i][j]);
}
cout<<ma<<endl;
}
void solve3()//O(n^2)
{
memset(f, , sizeof(f) );
for(int i = ; i <= l1; i++)
{
int ma1 = ;
for(int j = ; j <= l2; j++)
{
f[i][j] = f[i-][j];//带这种的一般都能压缩一维空间,也就是简化空间复杂度
if(a[i-] > b[j-] && f[i-][j] > ma1)
ma1 = f[i-][j];
if(a[i-] == b[j-])
f[i][j] = ma1 + ;
}
}
ma = -;
for(int j = ;j <= l2; j++)
ma=max(ma,f[l1][j]); cout<<ma<<endl;
}
void solve4()//O(n^2)//优化空间复杂度
{
ma = ;
memset(d, , sizeof(d) );
for(int i = ; i <= l1; i++)
{
int ma1 = ;
for(int j = ; j <= l2; j++)
{
if(a[i-] > b[j-] && d[j] > ma1)
ma1 = d[j];
if(a[i-] == b[j-])
d[j] = ma1 + ;
}
}
ma = -;
for(int j = ;j <= l2; j++)
ma = max(ma, d[j]); cout<<ma<<endl;
} int main()
{
int T;cin>>T;
while(T--)
{
scanf("%d", &l1);
for(int i = ; i < l1; i++)
scanf("%d", &a[i]);
scanf("%d", &l2);
for(int i = ; i < l2; i++)
scanf("%d", &b[i]);
// solve1();
// solve2();
// solve3();
solve4(); if(T)
printf("\n");
} return ;
}
/*
99 5
1 4 2 5 -12
4
-12 1 2 4 9
3 5 1 6 7 9 1 5 13
6
4 6 13 9 13 5
*/

HDU 1423 Greatest Common Increasing Subsequence(LICS入门,只要求出最长数)的更多相关文章

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

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

  2. HDU 1423 Greatest Common Increasing Subsequence LCIS

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

  3. HDU 1423 Greatest Common Increasing Subsequence(LCIS)

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

  4. HDU 1423 Greatest Common Increasing Subsequence

    最长公共上升子序列   LCIS 看这个博客  http://www.cnblogs.com/nuoyan2010/archive/2012/10/17/2728289.html #include&l ...

  5. HDU 1423 Greatest Common Increasing Subsequence ——动态规划

    好久以前的坑了. 最长公共上升子序列. 没什么好说的,自己太菜了 #include <map> #include <cmath> #include <queue> ...

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

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

  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 1423 Greatest Common Increasing Subsequence【裸LCIS】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1423 http://acm.hust.edu.cn/vjudge/contest/view.action ...

  9. HDU1423:Greatest Common Increasing Subsequence(LICS)

    Problem Description This is a problem from ZOJ 2432.To make it easyer,you just need output the lengt ...

随机推荐

  1. bs4--官文--修改文档树

    修改文档树 Beautiful Soup的强项是文档树的搜索,但同时也可以方便的修改文档树 修改tag的名称和属性 在 Attributes 的章节中已经介绍过这个功能,但是再看一遍也无妨. 重命名一 ...

  2. ubuntu介绍以及使用

    Ubuntu(友帮拓.优般图.乌班图)是一个以桌面应用为主的开源GNU/Linux操作系统,Ubuntu 是基于Debian GNU/Linux,支持x86.amd64(即x64)和ppc架构,由全球 ...

  3. C#Windows服务安装

    1,做好windows服务后,生成 一下,然后在项目目录中找到bin文件夹下的Debug文件夹,文件夹下有文件xxxx.exe 2,然后在C:\Windows\Microsoft.NET\Framew ...

  4. .NET重构(六):删除用户和结账的理解

    导读:这是第二回机房了,第一回不明不白,不清不楚的就过去了(相对),这一回,有了新的发现.就是在用户删除的时候,涉及到的一些逻辑问题,以及结账时的数据来源问题. 一.用户删除 问题:第一次机房,包括重 ...

  5. DataTable排序

    DataRow[] rows = dt.Select("", "name asc");   DataTable t = dt.Clone();   t.Clea ...

  6. cell展开的几种方式

    一.插入新的cell 原理: (1)定义是否展开,和展开的cell的下标 @property (assign, nonatomic) BOOL isExpand; //是否展开 @property ( ...

  7. BZOJ 1043 [HAOI2008]下落的圆盘 ——计算几何

    倒着考虑,加入一个圆,判断和前面有没有完全覆盖的情况. 如果没有,和圆盘一一取交集,然后计算它们的并集,然后计算即可. #include <map> #include <cmath& ...

  8. (2015大作业)茹何优雅的手写正则表达式引擎(regular expression engine

    貌似刚开学的时候装了个逼,和老师立了个flag说我要写个正则表达式引擎,然后学期末估计老师早就忘了这茬了,在历时3个月的懒癌发作下,终于在这学期末deadline的时候花了一个下午加晚上在没有网的房间 ...

  9. 算法复习——cdq分治

    题目: Description 有n朵花,每朵花有三个属性:花形(s).颜色(c).气味(m),又三个整数表示.现要对每朵花评级,一朵花的级别是它拥有的美丽能超过的花的数量.定义一朵花A比另一朵花B要 ...

  10. jsp 详解request对象

    request对象 客户端的请求信息被封装在request对象中,通过它才能了解到客户的需求,然后做出响应.它是HttpServletRequest类的实例. 序号 方 法 说 明 1  object ...