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 ...
随机推荐
- 纹理mag filter不能取GL_XXX_MIPMAP_XXXX
今天遇到OpenGL error 0x0500错误,定位到 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter); 查看ma ...
- navigate是Router类的一个方法,主要用来跳转路由。
navigate是Router类的一个方法,主要用来跳转路由. 1 2 3 4 5 6 7 8 9 interface NavigationExtras { relativeTo : Activat ...
- Atitit.prototype-base class-based 基于“类” vs 基于“原型”
Atitit.prototype-base class-based 基于“类” vs 基于“原型” 1. 基于“类” vs 基于“原型”1 2. 对象的产生有两种基本方式.一种是以原型(proto ...
- html 基本标签 ---字体
<b> </b>加粗 <i> </i> 斜体 <del> </del> 删除 <ins> </ins> ...
- hdu6070 Dirt Ratio 二分+线段树
/** 题目:hdu6070 Dirt Ratio 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6070 题意:给定n个数,求1.0*x/y最小是多少.x ...
- CDH 安装配置指南(Tarball方式)
采用CDH Tarbal方式安装Hadoop集群. 1. 环境组件版本 组件名称 组件版本 用途 jdk 1.8 jdk-8u191-linux-x64 oracle jdk mysql mysql- ...
- Oracle 计算两个时间的差值
有两个日期数据START_DATE,END_DATE,欲得到这两个日期的时间差(以天,小时,分钟,秒,毫秒):天:ROUND(TO_NUMBER(END_DATE - START_DATE))小时:R ...
- hdu1027(n个数的按字典序排列的第m个序列)
题目信息:给出n.m,求n个数的按字典序排列的第m个序列 http://acm.hdu.edu.cn/showproblem.php? pid=1027 AC代码: /** *全排列的个数(次序) ...
- IPMI特点和功能
IPMI独立于操作系统外自行运作,并容许管理者即使在缺少操作系统或系统管理软件.或受监控的系统关机但有接电源的情况下仍能远程管理系统. ipmi可以实现对机器的操作举例如下: 开机,关机,重启,查看机 ...
- 利用Java编写简单的WebService实例
使用Axis编写WebService比較简单,就我的理解,WebService的实现代码和编写Java代码事实上没有什么差别,主要是将哪些Java类公布为WebService. 以下是一个从编写測试样 ...