HDU1423:Greatest Common Increasing Subsequence(LICS)
5
1 4 2 5 -12
4
-12 1 2 4
题意:求最长递增公共子序列的长度
思路:直接模板
#include <stdio.h>
#include <algorithm>
#include <string.h>
using namespace std; int n,m,a[505],b[505],dp[505][505]; int LICS()
{
int MAX,i,j;
memset(dp,0,sizeof(dp));
for(i = 1; i<=n; i++)
{
MAX = 0;
for(j = 1; j<=m; j++)
{
dp[i][j] = dp[i-1][j];
if(a[i]>b[j] && MAX<dp[i-1][j])
MAX = dp[i-1][j];
if(a[i]==b[j])
dp[i][j] = MAX+1;
}
}
MAX = 0;
for(i = 1; i<=m; i++)
if(MAX<dp[n][i])
MAX = dp[n][i];
return MAX;
} int main()
{
int i,t;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i = 1; i<=n; i++)
scanf("%d",&a[i]);
scanf("%d",&m);
for(i = 1; i<=m; i++)
scanf("%d",&b[i]);
printf("%d\n",LICS());
if(t)
printf("\n");
} return 0;
}
上面的虽然可以解决,但是二维浪费空间较大,我们注意到在LICS函数中有一句dp[i][j] = dp[i-1][j],这证明dp数组前后没有变化!于是可以优化成一维数组!
#include <stdio.h>
#include <string.h>
#include <algorithm>
using namespace std; int a[505],b[505],dp[505],n,m; int LICS()
{
int i,j,MAX;
memset(dp,0,sizeof(dp));
for(i = 1; i<=n; i++)
{
MAX = 0;
for(j = 1; j<=m; j++)
{
if(a[i]>b[j] && MAX<dp[j])
MAX = dp[j];
if(a[i]==b[j])
dp[j] = MAX+1;
}
}
MAX = 0;
for(i = 1; i<=m; i++)
if(MAX<dp[i])
MAX = dp[i];
return MAX;
} int main()
{
int t,i;
scanf("%d",&t);
while(t--)
{
scanf("%d",&n);
for(i = 1; i<=n; i++)
scanf("%d",&a[i]);
scanf("%d",&m);
for(i = 1; i<=m; i++)
scanf("%d",&b[i]);
printf("%d\n",LICS());
if(t)
printf("\n");
} return 0;
}
HDU1423:Greatest Common Increasing Subsequence(LICS)的更多相关文章
- HDU1423:Greatest Common Increasing Subsequence
浅谈\(DP\):https://www.cnblogs.com/AKMer/p/10437525.html 题目传送门:http://acm.hdu.edu.cn/showproblem.php?p ...
- HDU 1423 Greatest Common Increasing Subsequence(LICS入门,只要求出最长数)
Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
- Greatest Common Increasing Subsequence hdu1423
Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
- 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 ...
- ZOJ 2432 Greatest Common Increasing Subsequence(最长公共上升子序列+路径打印)
Greatest Common Increasing Subsequence 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...
- 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 ...
随机推荐
- MySQL Scale Out
原文:MySQL Scale Out 简介 MySQL复制中较常见的复制架构有“一主一从”.“一主多从”.“双主”.“多级复制”和“多主环形机构”等,见下图: 最常用,也最灵活的就要数“一主多从”复制 ...
- Android - 错: java.lang.IllegalStateException: Already attached
错: java.lang.IllegalStateException: Already attached 本文地址: http://blog.csdn.net/caroline_wendy 可能原因: ...
- 采用oracle官方文件(11G)——初步Concept
采用oracle官方文件(11G)示例 这里是oracle官方文档界面,想了解oracle,阅读官方文档是唯一的方法,大致了解官方文档的使用,对官方文档有一个更直观的认识.文档可通过文章关联的链接查看 ...
- Android4.4 Framework分析——startService创建过程
我们经常使用context.startService()要启动service.下面就来分析这service启动过程,下图是service启动序列图: watermark/2/text/aHR0cDov ...
- opencv2对于读书笔记——二值化——thresholded功能
opencv二进制图象值功能threshold功能 其结构 double cv::threshold( //二值化函数 const CvArr* src, //原始图像 CvArr* dst, //输 ...
- POJ 1664 把苹果
把苹果 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 25785 Accepted: 16403 Descript ...
- 【转】Java 工程师成神之路
一.基础篇 1.1 JVM 1.1.1. Java内存模型,Java内存管理,Java堆和栈,垃圾回收 http://www.jcp.org/en/jsr/detail?id=133 http://i ...
- hdu 3874
求一个序列中全部数字的和,当中数值同样的仅仅能计算一次. 先储存全部的请求,然后依照它们的右边界排序,在查询的同一时候更新区间.这里事实上有一点点DP的味道,在它进行某个查询之前,保证全部的反复数字( ...
- Team City的安装1
持续集成工具 Team City的安装 前两个月很大一部分精力投入在做部门的持续集成,从概念的了解和工具的选型,再到安装,部署,操作,到最后的真实项目持续集成应用的上线,写了一份手册,包括安装,配置, ...
- C++中的class
C++中的class是C++不同于C的关键所在: 是面向对象中声明的类: 公有成员public member 在程序的不论什么地方都能够被訪问实行信息隐藏的类将 其publ ...