POJ 1423 Greatest Common Increasing Subsequence【裸LCIS】
链接:
Greatest Common Increasing Subsequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 2757 Accepted Submission(s): 855
1 5
1 4 2 5 -12
4
-12 1 2 4
2
算法:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std; const int maxn = 500+50;
int dp[maxn][maxn];
int a[maxn],b[maxn];
int m,n; /****
求序列 A 长度为 N 和序列 B 长度为 M 的 LCS
序列下标从 1 开始
*/
int LCS()
{
for(int i = 1; i <= n; i++)
{
int tmp = 0; //记录在i确定,且a[i]>b[j]的时候dp[i,j]的最大值
for(int j = 1; j <= m; j++)
{
dp[i][j] = dp[i-1][j];
if(a[i] > b[j])
{
tmp = dp[i-1][j];
}
else if(a[i] == b[j])
dp[i][j] = tmp+1;
}
}
//for(int i = 1; i <= m; i++) printf("%d ", dp[n][i]); printf("\n"); int ans = 0;
for(int i = 1; i <= m; i++)
ans = max(ans, dp[n][i]);
return ans; } int main()
{
int T;
scanf("%d", &T);
while(T--)
{
memset(dp,0,sizeof(dp)); scanf("%d", &n);
for(int i = 1; i <= n; i++)
scanf("%d", &a[i]);
scanf("%d", &m);
for(int j = 1; j <= m; j++)
scanf("%d", &b[j]); printf("%d\n",LCS());
if(T != 0) printf("\n");
}
}
内存优化:
#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std; const int maxn = 500+50;
int dp[maxn];
int a[maxn],b[maxn];
int m,n; /****
求序列 A 长度为 N 和序列 B 长度为 M 的 LCS
序列下标从 1 开始
*/
int LCS()
{
for(int i = 1; i <= n; i++)
{
int tmp = 0;
for(int j = 1; j <= m; j++)
{
if(a[i] > b[j] && dp[j] > tmp)
{
tmp = dp[j];
}
else if(a[i] == b[j])
dp[j] = tmp+1;
}
} int ans = 0;
for(int i = 1; i <= m; i++)
ans = max(ans, dp[i]);
return ans;
} int main()
{
int T;
scanf("%d", &T);
while(T--)
{
memset(dp,0,sizeof(dp)); scanf("%d", &n);
for(int i = 1; i <= n; i++)
scanf("%d", &a[i]);
scanf("%d", &m);
for(int j = 1; j <= m; j++)
scanf("%d", &b[j]); printf("%d\n",LCS());
if(T != 0) printf("\n");
}
}
POJ 1423 Greatest Common Increasing Subsequence【裸LCIS】的更多相关文章
- HDU 1423 Greatest Common Increasing Subsequence(LCIS)
Greatest Common Increasing Subsequenc Problem Description This is a problem from ZOJ 2432.To make it ...
- 1423 Greatest Common Increasing Subsequence (LCIS)
讲解摘自百度; 最长公共上升子序列(LCIS)的O(n^2)算法? 预备知识:动态规划的基本思想,LCS,LIS.? 问题:字符串a,字符串b,求a和b的LCIS(最长公共上升子序列).? 首先我们可 ...
- HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS)
HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS) http://acm.hdu.edu.cn/showproblem.php?pi ...
- HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】
HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...
- HDU 1423 Greatest Common Increasing Subsequence LCIS
题目链接: 题目 Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- POJ 2127 Greatest Common Increasing Subsequence -- 动态规划
题目地址:http://poj.org/problem?id=2127 Description You are given two sequences of integer numbers. Writ ...
- HDOJ 1423 Greatest Common Increasing Subsequence -- 动态规划
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1423 Problem Description This is a problem from ZOJ 2 ...
- POJ 2127 Greatest Common Increasing Subsequence
You are given two sequences of integer numbers. Write a program to determine their common increasing ...
- HDUOJ ---1423 Greatest Common Increasing Subsequence(LCS)
Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
随机推荐
- 阅读《Android 从入门到精通》(29)——四大布局
LinearLayout 类方法 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQ ...
- php get_called_class()函数与get_class函数的区别
get_class (): 获取当前调用方法的类名: get_called_class():获取静态绑定后的类名: 有例为证: class Foo{ public function test(){ v ...
- php-fig组织fig-standards的一些标准
参考: http://psr.phphub.org/ https://github.com/php-fig/fig-standards https://github.com/PizzaLiu/PHP- ...
- 点滴积累【JS】---JS小功能(setInterval实现图片效果显示时间)
效果: 代码: <head runat="server"> <title></title> <script type="text ...
- Android Studio怎样查看branch列表及切换branch
针对Android Studio的系列文章,都是一个小问题为一篇,并没有整理到一起,主要是方便大家依据自己的须要来查找,同一时候为了便于大家理解,都会直接上图. 我这里使用的版本号控制工具是git,由 ...
- matplotlib之创建极坐标系
#!/usr/bin/env python3 #-*- coding:utf-8 -*- ############################ #File Name: polar.py #Auth ...
- ajax发送请求时为url添加参数(使用函数)
<script> // ajax的get请求,使用函数向其url添加参数 function addURLParam(url,name,value){ url+=(url.indexOf(' ...
- The Boss on Mars
The Boss on Mars Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) ...
- 14 javaBean 组件
bean类不应该有公开的实例变量. 持续性的值应该通过 getXxx 和 setXxx 方法访问. <jsp: useBean id=”beanName” class=”package.Clas ...
- 拖拽 支持ie6
可随意拖拽方块至任一位置: 1.setCapture方法:多用于容器对象,效果是对指定的对象设置鼠标捕获.使在容器内的子对象的鼠标事件均由容器对象触发,因此,只能在容器对象的鼠标事件函数中进行处理.当 ...