题目链接:

题目

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. Javascript验证用户输入URL地址是否正确

    <script type="text/javascript">function checkUrl() { var url = document.getElementBy ...

  2. DEEPIN下搭建FTP服务器步骤(备忘录)

    1.打开终端,执行命令[apt-get install vsftpd],安装VSFTPD 2.安装完成后,修改以下配置信息(否则文件无法传输) [echo 'listen=YES'>>/e ...

  3. 【学习笔记】【C语言】循环结构-while

    1. 简单使用 while ( 条件 ) {     语句1;     语句2;     .... } 如果条件成立,就会执行循环体中的语句(“循环体”就是while后面大括号{}中的内容).然后再次 ...

  4. 如何给xml应用样式

    引子:可扩展标记语言xml(Extensible Markup Language)自己平常也用到的不多,除了在ajax处理服务器返回的数据可能会用到外(不过一般用json处理数据的比较常见)还真没怎么 ...

  5. Angular2中的metadata(元数据)

    @Attrubute() 从host element 中获得普通(不是@Input)属性对应的值 适用于组件嵌套或指令, 从父组件向子组件传递数据 app.component.ts import {C ...

  6. Windows下安装GnuRadio最简单的方法(没有之一)

    作者在Windows XP SP3 32位下亲测通过,理论上Win7也没问题. 1. 如果系统中安装有Python,请先把Python卸载. 2. 下载安装Python(x,y) 2.7.5.0, 下 ...

  7. USB接口介绍

        USB设备系统分为两个部分,USB Host端和USB Device端,以USB接口的U盘为例子,U盘自身是一个USB Device,PC机的USB接口以及相关的控制电路为USB Host部分 ...

  8. java 设计模式之单例模式

    -------Success is getting what you want, happiness is wanting what you get. java设计模式之单例模式(Singleton) ...

  9. Xcode中为代码添加特殊标记

    有时候,我们需要在代码中搜索特殊的符号或者代码段,根据符号或使用搜索功能导航代码段效率并不算高.为了使用普通的英语标识重要的代码片段,可在代码中插入特殊格式的注释.这些注释不会在应用程序中添加任何特殊 ...

  10. ASP.NET中利用Split实现对Checkbox的字符串分离放到DataTable里面

    一.背景 昨天唐欢问了我一个问题: 现在有一个CheckBox和一个Label如下图: 要实现选中CheckBox,点击下面打印按钮的时候要做成这个样子的如下图: 简单的说就是档案编号作为表中的一个列 ...