hdu 1423 Greatest Common Increasing Subsequence 最大公共上升子序列 DP
来源:互联网 发布:浩辰cad建筑软件 编辑:程序博客网 时间:2024/06/06 12:57
Greatest Common Increasing Subsequence
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 4590 Accepted Submission(s): 1462
Problem Description
This is a problem from ZOJ 2432.To make it easyer,you just need output the length of the subsequence.
Input
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
output print L - the length of the greatest common increasing subsequence of both sequences.
Sample Input
151 4 2 5 -124-12 1 2 4
Sample Output
2
最近一直在做DP题。。现在的进步是,可以看得懂别人的题解了,。
上一个百度文库的文章讲的还是非常不错的。最长公共上升子序列(LCIS)的平方算法
下面是用O(n^3)的算法。还会更新O(n^2)的算法 。
#include <stdio.h>#include <string.h>#define MAX 600int dp[MAX][MAX] ;int max(int a ,int b){return a>b?a:b ;}int main(){int num1[MAX],num2[MAX], t;scanf("%d",&t);while(t--){int n , m ;scanf("%d",&n);for(int i = 1 ; i <= n ; ++i)scanf("%d",&num1[i]);scanf("%d",&m);for(int i = 1 ; i <= m ; ++i)scanf("%d",&num2[i]);memset(dp,0,sizeof(dp)) ;int ans = 0 ;for(int i = 1 ; i <= n ; ++i){for(int j = 1 ; j <= m ; ++j){if(num1[i]!=num2[j])dp[i][j] = dp[i-1][j];if(num1[i] == num2[j]){dp[i][j] = 1 ;for(int k = 1 ; k < j ; ++k){if(num2[j]>num2[k])dp[i][j] = max(dp[i][j],dp[i-1][k]+1) ;}}ans = max(ans,dp[i][j]) ;}}printf("%d\n",ans) ;if(t != 0){printf("\n") ;}}return 0 ;}
-----------------------------------------------------------------分割线------------------------------------------------------------
晚上更新:
捣鼓了O(n^2)+一维数组的算法,,不得不佩服搞出这个算法的人,神牛啊!!我看都这么费劲,人家却能发明出来。哎,深刻的感受到了这个世界恶意
o(╯□╰)o
看代码:
#include <stdio.h>#include <string.h>#define MAX 505 int getMax(int a , int b){return a>b?a:b ;}int main(){int num1[MAX],num2[MAX],dp[MAX],t;scanf("%d",&t);while(t--){int n , m ;scanf("%d",&n);for(int i = 1 ; i <= n ; ++i)scanf("%d",&num1[i]);scanf("%d",&m);for(int i = 1 ; i <= m ; ++i)scanf("%d",&num2[i]);memset(dp,0,sizeof(dp)) ;int ans = 0 ;for(int i = 1 ; i <= n ; ++i){int max = 0 ;for(int j = 1 ; j <= m ; ++j){if(num1[i]>num2[j] && max < dp[j])max = dp[j] ;if(num1[i] == num2[j]){dp[j] = max+1 ;}ans = getMax(ans,dp[j]) ;}}printf("%d\n",ans) ;if(t!=0){printf("\n") ;}}return 0 ;}
0 0
- hdu 1423 Greatest Common Increasing Subsequence 最大公共上升子序列 DP
- hdu 1423 Greatest Common Increasing Subsequence(DP 最长公共上升子序列)
- hdu 1423 Greatest Common Increasing Subsequence(最长公共上升子序列dp)
- hdu 1423 Greatest Common Increasing Subsequence (最长上升子序列)
- hdu 1423 Greatest Common Increasing Subsequence (最长上升子序列)
- hdu 1423 Greatest Common Increasing Subsequence 最长公共上升子序列
- HDU 1423 Greatest Common Increasing Subsequence(最长公共上升子序列)
- hdu 1423 Greatest Common Increasing Subsequence(最长上升公共子序列)
- hdu 1423 Greatest Common Increasing Subsequence(最长公共上升子序列、LCIS)
- HDU OJ 1423Greatest Common Increasing Subsequence 最长公共上升子序列
- hdu 1423 Greatest Common Increasing Subsequence 最长公共上升子序列
- hdu 1423 Greatest Common Increasing Subsequence (最长上升公共子序列)
- HDU 1423 Greatest Common Increasing Subsequence(动态规划+最长公共上升子序列)
- HDU 1423 Greatest Common Increasing Subsequence (最长公共上升子序列)【模板】
- hdu1423---Greatest Common Increasing Subsequence(最长公共上升子序列)
- Greatest Common Increasing Subsequence-最长公共上升子序列
- poj 2127 Greatest Common Increasing Subsequence(最长公共上升子序列dp)
- HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS)
- 3.在Ubuntu上为Android增加硬件抽象层(HAL)模块访问Linux内核驱动程序
- Fork and Exec
- 推荐!国外程序员整理的 C++ 资源大全
- Python线程池简单实现
- Linux内核双向循环链表分析
- hdu 1423 Greatest Common Increasing Subsequence 最大公共上升子序列 DP
- 4.在Ubuntu为Android硬件抽象层(HAL)模块编写JNI方法提供Java访问硬件服务接口
- 搭建PHP环境
- 5.在Ubuntu上为Android系统的Application Frameworks层增加硬件访问服务
- SQL从一张表的某个字段更新为另一张表的字段
- [LeetCode]Valid Palindrome
- 6.在Ubuntu上为Android系统内置Java应用程序测试Application Frameworks层的硬件服务
- 【数据结构】静态链表
- 构造函数不能声明为虚函数,析构函数可以声明为虚函数