HDU 1423 Greatest Common Increasing Subsequence LCIS
题目链接:
题目
Greatest Common Increasing Subsequence
Time Limit: 2000/1000 MS (Java/Others)
Memory Limit: 65536/32768 K (Java/Others)
问题描述
This is a problem from ZOJ 2432.To make it easyer,you just need output the length of the subsequence.
输入
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 print L - the length of the greatest common increasing subsequence of both sequences.
样例
input
1
5
1 4 2 5 -12
4
-12 1 2 4
output
2
题意
求两个串的最长公共上升子序列(LCIS)
题解
dp[j]表示第一个串的前i个和第二个串的前j个的以b[j]结尾的公共最长上升子序列的长度。
代码
O(n^3):
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 555;
int dp[maxn];
int a[maxn], b[maxn];
int n, m;
void init() {
memset(dp, 0, sizeof(dp));
}
int main() {
int tc;
scanf("%d", &tc);
while (tc--) {
init();
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
scanf("%d", &m);
for (int i = 1; i <= m; i++) scanf("%d", &b[i]);
int ans = 0;
for (int i = 1; i <= n; i++) {
for (int j = 1; j <= m; j++) {
if (a[i] == b[j]) {
dp[j] = 1;//b[0]是非法的!
for (int k = 0; k < j; k++) {
if (b[k] < b[j] && dp[j] < dp[k] + 1) {
dp[j] = dp[k] + 1;
}
}
}
ans = max(ans, dp[j]);
}
}
printf("%d\n", ans);
if (tc) printf("\n");
}
return 0;
}
O(n^2):k循环其实可以不用,用Max一边扫j,一边记录就可以了。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
using namespace std;
const int maxn = 555;
int dp[maxn];
int a[maxn], b[maxn];
int n, m;
void init() {
memset(dp, 0, sizeof(dp));
}
int main() {
int tc;
scanf("%d", &tc);
while (tc--) {
init();
scanf("%d", &n);
for (int i = 1; i <= n; i++) scanf("%d", &a[i]);
scanf("%d", &m);
for (int i = 1; i <= m; i++) scanf("%d", &b[i]);
int ans = 0;
for (int i = 1; i <= n; i++) {
int Max = 0;
for (int j = 1; j <= m; j++) {
if (b[j]<a[i]&&Max<dp[j]) {
Max = dp[j];
}
else if (a[i] == b[j]) {
dp[j] = Max + 1;
}
ans = max(ans, dp[j]);
}
}
printf("%d\n", ans);
if (tc) printf("\n");
}
return 0;
}
HDU 1423 Greatest Common Increasing Subsequence LCIS的更多相关文章
- 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 Subsequenc Problem Description This is a problem from ZOJ 2432.To make it ...
- HDU 1423 Greatest Common Increasing Subsequence(LICS入门,只要求出最长数)
Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
- 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 ...
- 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 ...
- HDOJ 1423 Greatest Common Increasing Subsequence -- 动态规划
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1423 Problem Description This is a problem from ZOJ 2 ...
- HDUOJ ---1423 Greatest Common Increasing Subsequence(LCS)
Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
随机推荐
- jquery $.trim()方法使用介绍
$.trim(str)的作用是去掉字符串首尾空格 $.trim(str) 返回:string: 说明:去掉字符串首尾空格. 示例: 先看个错误代码: <input type="text ...
- 【学习笔记】【C语言】指针
一.指针变量的定义 1. 格式:变量类型 *指针变量名; 2. 举例:int *p; char *p2; 3. 注意:定义变量时的*仅仅是指针变量的象征 二.利用指针变量简单修改其他变量的值 1. ...
- IoC容器的初始化过程
1.简单来说,IoC容器的初始化是由前面介绍的refresh()方法来启动的,这个方法标志着IoC容器的正式启动. 2.具体来说,这个启动包括BeanDefinition的Resource定位.载入和 ...
- SharedSDK微信分享不成功,分享之后没有反应
对于一般来说,使用SharedSDK的时候,分享不成功不外乎下面几个原因: 1.测试没有打包2.打包的keystore跟微信开放平台上面的不一致, 导致MD5码不一致3.分享参数错误4.应用没有审核通 ...
- js Table冻结表头示例代码
Table冻结表头的js实现代码. Table冻结表头: <script type="text/javascript"> //冻结table的表头 function ...
- pandas库学习笔记(二)DataFrame入门学习
Pandas基本介绍——DataFrame入门学习 前篇文章中,小生初步介绍pandas库中的Series结构的创建与运算,今天小生继续“死磕自己”为大家介绍pandas库的另一种最为常见的数据结构D ...
- 小课堂Week10 例外处理设计的逆袭Part3
小课堂Week10 例外处理设计的逆袭Part3 今天是<例外处理设计的逆袭>这本书阅读的第三天,也是最后一天,我们会主要通过实例,对Part2中提出的例外处理等级进行解读. Level1 ...
- 关于C语言中的typedef
在C语言中定义一个结构体,要最好使用typedef,使用typedef,实际上就是为我们的结构体起了一个新的名字,即定义了一个新的类型,在后面书写自己代码的时候,就可以直接使用自己定义的新的类型第一变 ...
- C string 函数大全
PS:本文包含了大部分strings函数的说明,并附带举例说明.本来想自己整理一下的,发现已经有前辈整理过了,就转了过来.修改了原文一些源码的问题,主要是用char *字义字符串的问题,导致程序运行时 ...
- awk中文手册
1. 前言 有关本手册 : 这是一本awk学习指引, 其重点着重于 : l awk 适于解决哪些问题 ? l awk 常见的解题模式为何 ? 为使读者快速掌握awk解题的模 ...