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 ...
随机推荐
- android 之 service
在Activity中设置两个按钮,分别为启动和关闭Service: bt01.setOnClickListener(new Button.OnClickListener() { @Override ...
- Convolution Fundamental II
Practical Advice Using Open-Source Implementation We have learned a lot of NNs and ConvNets architec ...
- Leetcode 363.矩形区域不超过k的最大数值和
矩形区域不超过k的最大数值和 给定一个非空二维矩阵 matrix 和一个整数 k,找到这个矩阵内部不大于 k 的最大矩形和. 示例: 输入: matrix = [[1,0,1],[0,-2,3]], ...
- 【C#】堆、栈和堆栈的区别
导读:今天看视频,就看到了堆.栈这一块了.记得当年初相见(VB视频),劈头盖脸一阵蒙,什么都不知道,那时候师傅叫我挂起来,说我随着学习的进度,慢慢的就会懂了.现在,学到了这里,想着自己对自己从前的问题 ...
- 网页QQ唤起
网页QQ唤起 <html> <head> <meta http-equiv="Content-Type" content="text/htm ...
- 什么是JNI?
JNI是Java Native Interface的缩写,它提供了若干的API实现了Java和其他语言的通信(主要是C和C++)
- idea打包SpringBoot项目打包成jar包和war
- 打包成jar包 1. <groupId>com.squpt.springboot</groupId> <artifactId>springbootdemo< ...
- 用 Jackson 来处理 JSON
Jackson 是一个 Java 用来处理 JSON 格式数据的类库,性能非常好. 首先创建一个User对象类 (User.java) package com.sivalabs.json; impor ...
- msp430项目编程60
msp430综合项目---扩展项目八60 1.电路工作原理 2.代码(显示部分) 3.代码(功能实现) 4.项目总结
- poj1149最大流经典构图神题
题意:n个顾客依次来买猪,有n个猪房,每个顾客每次可以开若干个房子,买完时,店主可以调整这位顾客 开的猪房里的猪,共m个猪房,每个猪房有若干猪,求最多能卖多少猪. 构图思想:顾客有先后,每个人想要的猪 ...