题目链接:

题目

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. CSS之简单树形菜单

    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/ ...

  2. 网站整站变灰的方法(不支持IE10)

    html { -ms-filter: grayscale(100%); -webkit-filter: grayscale(100%); -moz-filter: grayscale(100%); - ...

  3. UI3_UIViewController生命周期

    // // SecondViewController.h // UI3_UIViewController生命周期 // // Created by zhangxueming on 15/7/2. // ...

  4. AMQ学习笔记 - 18. 持久化的测试

    概述 对持久化的有效性进行测试. 测试实例 测试实例 结果预测 持久化递送 重启ActiveMQ后,消息还在队列中 非持久化递送 重启ActiveMQ后,消息不在队列中 demo设计 jms-prod ...

  5. javascript构造函数小记

    function outer(){ function inner(){} return inner; } var a=outer(); var b=outer(); var c=new outer() ...

  6. 移动端边框1px的实现

    查看京东的移动端1px实现原理,用的是:after和css3的scale(0.5)缩放. border-right fr:after{ height:100%; content:' '; width: ...

  7. Microsoft Office Access 不能在追加查询中追加所有记录

    有客户反映,我们的软件ACCESS数据库版本在使用时会出现"Microsoft Office Access 不能在追加查询中追加所有记录"的错误.使用客户的数据库调试软件发现错误出 ...

  8. BCB6中SCALERICHVIEW加入GIF动画

    记载下,花了不少时间. 1.  项目导入文件GIFImage.pas 来源:http://melander.dk/delphi/gifimage/ 2.  项目导入文件RVGifAnimate.pas ...

  9. centos6.5安装flume

    这里安装flume是因为游戏业务日志搜集和分析用的 1.安装java 环境rpm -ivh jdk-8u51-linux-x64.rpm Preparing... ################## ...

  10. zedboard 构建嵌入式linux

    本文通过五部完成zedboard的嵌入式LINUX搭建,所谓磨刀不五砍材工嘛 1:系统环境搭建 要准备好交叉编译环境 见http://blog.csdn.net/xiabodan/article/de ...