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 ...
随机推荐
- hexo搭建静态博客
1. 环境环境 1.1 安装Git 请参考[1] 1.2 安装node.js 下载:http://nodejs.org/download/ 可以下载 node-v0.10.33-x64.msi 安装时 ...
- js 数组的length(javascript教程四)
这是一个简单的函数,就是利用length来判断数组再遍历数组了. <script language="javascript" type="text/javascri ...
- ThinkPHP中的内置标签
ThinkPHP中的内置标签 1.内置标签分类 闭合标签 <tag></tag> 开放标签 <tag /> 2.包含文件标签 主要功能:实现对文件的包含(类似于re ...
- java大数--总结
BigInteger(高精度整数) 1.所在包: java.math.BigInteger 2.大数运算,以下返回类型均为BigInteger BigInteger a; BigInteger b; ...
- functional javascript
(转载请注明出处!) 今早带我的master跟我分享了他最近看<functional javascript>一书的感悟,瞬间觉得写1w行代码都不如看本好书来的好啊! 于是在下午的写的项目中 ...
- eclipse juno版本中没用 ant
下载了谷歌提供的Android集成开发工具ADT,里面封装了Eclipse,但是很奇怪的是竟然没有Ant插件在里面 标准的Eclipse一般都是内置集成了Ant的. 然后到eclipse的plugin ...
- Oracle中Left join的on和where的效率差别
假设有两个表a.b 使用on Select * from a left join b on b.col = a.col and b.col2 = ‘aa’ 使用 where Select * from ...
- struts2 知识梳理
写此文章时,最新struts2版本:2.3.6 一:struts.xml配置详解: 1.<include> 表示引入其他配置文件 2.<constant> 定义常量 3.< ...
- Docker无法启动 Could not find a free IP address range for interface 'docker0' 最方便的解决办法
阿里云的CentOS 6.5上安装Docker会无法启动,如果直接运行docker -d会看到错误提示:Could not find a free IP address range for inter ...
- 在HTML中通过jQuery设置列表项符号
在创建列表的时候,可以通过指定type来设置列表项的符号,如下所示: <body> <form id="form1" runat="server&quo ...