HDU 1423 Greatest Common Increasing Subsequence(LICS入门,只要求出最长数)
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
sequence is described with M - its length (1 <= M <= 500) and M
integer numbers Ai (-2^31 <= Ai < 2^31) - the sequence itself.
5
1 4 2 5 -12
4
-12 1 2 4
四种方法代码:
每一种都按照我觉得最容易理解的思路和最精简的写法写了,有些地方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入门,只要求出最长数)的更多相关文章
- HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS)
HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS) http://acm.hdu.edu.cn/showproblem.php?pi ...
- HDU 1423 Greatest Common Increasing Subsequence LCIS
题目链接: 题目 Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- HDU 1423 Greatest Common Increasing Subsequence(LCIS)
Greatest Common Increasing Subsequenc Problem Description This is a problem from ZOJ 2432.To make it ...
- HDU 1423 Greatest Common Increasing Subsequence
最长公共上升子序列 LCIS 看这个博客 http://www.cnblogs.com/nuoyan2010/archive/2012/10/17/2728289.html #include&l ...
- HDU 1423 Greatest Common Increasing Subsequence ——动态规划
好久以前的坑了. 最长公共上升子序列. 没什么好说的,自己太菜了 #include <map> #include <cmath> #include <queue> ...
- HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】
HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...
- HDOJ 1423 Greatest Common Increasing Subsequence -- 动态规划
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1423 Problem Description This is a problem from ZOJ 2 ...
- 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 ...
- 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 ...
随机推荐
- Oracle跟踪分析数据库启动的各个阶段
目录 启动到nomount状态 设置trace 启动数据库到mount状态并打开 查阅trace 查阅trace的另外方法 v$diag_info 视图 演示如下: 启动到nomount状态 SYS@ ...
- mysql无法创建外键问题
在阿里云上面安装配置了Mysql后,无法创建外键, 原因及解决方法: 选择InnoDB引擎,因为MyISAM引擎不支持外键,默默地说一句,MySQL真神奇
- xtu数据结构 D. Necklace
D. Necklace Time Limit: 5000ms Memory Limit: 32768KB 64-bit integer IO format: %I64d Java class ...
- 九度oj 题目1125:大整数的因子
题目描述: 已知正整数k满足2<=k<=9,现给出长度最大为30位的十进制非负整数c,求所有能整除c的k. 输入: 若干个非负整数c,c的位数<=30每行一个c,当c=-1时中止 ( ...
- 九度oj 题目1120:全排列
题目描述: 给定一个由不同的小写字母组成的字符串,输出这个字符串的所有全排列. 我们假设对于小写字母有'a' < 'b' < ... < 'y' < 'z',而且给定的字符串中 ...
- HDU 1071 The area ——微积分
[题目分析] 求二次函数和一次函数围成的面积. 先解方程求出一次函数和二次函数. 然后积分. 现在还是不会积分. [代码] #include <cstdio> #include <c ...
- 修改系统dpi
系统dpi设置不合理,会导致屏幕显示效果差.配置系统dpi设置ro.sf.lcd_density的值即可,不同项目设置位置不同,以高通为例:需要修改 项目/device/qcom/common/roo ...
- 【Tomcat】linux下实时查看tomcat运行日志
今天在部署一个项目到linux服务器的时候一直报错,可是在日志文件中也没有记录.但是在本地测试的时候都没有错误,在windoesServer服务器上也没错误,实在找不到原因,因此想的实时查看tomca ...
- Day 13 Python 一之helloworld
直接肝程序吧! """ # 作业六:用户登录测试(三次机会) count = 1 while count <= 3: user = input('请输入用户名: ' ...
- elasticsearch入门使用(四) 索引、安装IK分词器及增删改查数据
一.查看.创建索引 创建一个名字为user索引: curl -X PUT 'localhost:9200/stu' {"acknowledged":true,"shard ...