题目链接:

题目

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的更多相关文章

  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 Subsequenc Problem Description This is a problem from ZOJ 2432.To make it ...

  3. HDU 1423 Greatest Common Increasing Subsequence(LICS入门,只要求出最长数)

    Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536 ...

  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. 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 ...

  8. HDOJ 1423 Greatest Common Increasing Subsequence -- 动态规划

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1423 Problem Description This is a problem from ZOJ 2 ...

  9. HDUOJ ---1423 Greatest Common Increasing Subsequence(LCS)

    Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536 ...

随机推荐

  1. 2012的数据库 select @@version 都是说版本为2008 R2

    如图 我使用的是sqlserver2012登录的,select @@version 查询出来的却是2008 ,而且附加不了2012的数据库. 在网上搜到解决方法:1确认是否安装了2012(废话没安装是 ...

  2. Windows 10 (or 8)Chrome 观看视频发生flash不能加载,即"could't load plugins"原因之一

    最近一直如题,不能看视频,后来发现从一个已经使用管理员权限打开的应用转到Chrome就可以加载flash,而从桌面打开Chrome就加载不了. 今天再次查找信息,从Ubuntu下Chrome不能加载f ...

  3. java中vector与hashtable操作详解

    众所周知,java中vector与hashtable是线程安全的,主要是java对两者的操作都加上了synchronized,也就是上锁了.因此 在vector与hashtable的操作是不会出现问题 ...

  4. STL Container和ATL智能包裹类的冲突

    STL Container和ATL智能包裹类的冲突 载自:http://www.codesky.net/article/200504/63245.html Article last modified ...

  5. 关于自定义tabBar时修改系统自带tabBarItem属性造成的按钮顺序错乱的问题相关探究

      关于自定义tabBar时修改系统自带tabBarItem属性造成的按钮顺序错乱的问题相关探究 测试代码:http://git.oschina.net/Xiyue/TabBarItem_TEST 简 ...

  6. FTP协议及工作原理详解

    1. FTP协议 什么是FTP呢?FTP 是 TCP/IP 协议组中的协议之一,是英文File Transfer Protocol的缩写. 该协议是Internet文件传送的基础,它由一系列规格说明文 ...

  7. 探索VS中C++多态实现原理

    引言 最近把<深度探索c++对象模型>读了几遍,收获甚大.明白了很多以前知其然却不知其所以然的姿势.比如构造函数与拷贝构造函数什么时候被编译器合成,虚函数.实例函数.类函数的区别等等.在此 ...

  8. POJ1056 IMMEDIATE DECODABILITY【数据结构】

    题目地址:http://poj.org/problem?id=1056 Description An encoding of a set of symbols is said to be immedi ...

  9. 字节的高低位知识,Ascii,GB2312,UNICODE等编码的关系与来历

    很久很久以前,有一群人,他们决定用8个可以开合的晶体管来组合成不同的状态,以表示世界上的万物.他们看到8个开关状态是好的,于是他们把这称为"字节". 再后来,他们又做了一些可以处理 ...

  10. 这是html5中WebGL的演示

    这是html5中WebGL的演示,让我们与他人分享爱您发送短消息.每次你进入它使用不同的位置,新的爱情点被添加到全球.让世界更明亮的地方与你的朋友分享! 源文件:部分代码:<!DOCTYPE h ...