链接:



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

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
  1. 1
  2. 5
  3. 1 4 2 5 -12
  4. 4
  5. -12 1 2 4
 
Sample Output
  1. 2
 
Source
 
Recommend
lcy



算法:

LCIS 【最长公共上升子序列分析


code:

注意格式 问题:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<algorithm>
  4. #include<iostream>
  5. using namespace std;
  6.  
  7. const int maxn = 500+50;
  8. int dp[maxn][maxn];
  9. int a[maxn],b[maxn];
  10. int m,n;
  11.  
  12. /****
  13. 求序列 A 长度为 N 和序列 B 长度为 M 的 LCS
  14. 序列下标从 1 开始
  15. */
  16. int LCS()
  17. {
  18.     for(int i = 1; i <= n; i++)
  19.     {
  20.         int tmp = 0; //记录在i确定,且a[i]>b[j]的时候dp[i,j]的最大值
  21.         for(int j = 1; j <= m; j++)
  22.         {
  23.             dp[i][j] = dp[i-1][j];
  24.             if(a[i] > b[j])
  25.             {
  26.                 tmp = dp[i-1][j];
  27.             }
  28.             else if(a[i] == b[j])
  29.                 dp[i][j] = tmp+1;
  30.         }
  31.     }
  32. //for(int i = 1; i <= m; i++) printf("%d ", dp[n][i]); printf("\n");
  33.  
  34.     int ans = 0;
  35.     for(int i = 1; i <= m; i++)
  36.         ans = max(ans, dp[n][i]);
  37.     return ans;
  38.  
  39. }
  40.  
  41. int main()
  42. {
  43.     int T;
  44.     scanf("%d", &T);
  45.     while(T--)
  46.     {
  47.         memset(dp,0,sizeof(dp));
  48.  
  49.         scanf("%d", &n);
  50.         for(int i = 1; i <= n; i++)
  51.             scanf("%d", &a[i]);
  52.         scanf("%d", &m);
  53.         for(int j = 1; j <= m; j++)
  54.             scanf("%d", &b[j]);
  55.  
  56.         printf("%d\n",LCS());
  57.         if(T != 0) printf("\n");
  58.     }
  59. }



内存优化:

  1. #include<stdio.h>
  2. #include<string.h>
  3. #include<algorithm>
  4. #include<iostream>
  5. using namespace std;
  6.  
  7. const int maxn = 500+50;
  8. int dp[maxn];
  9. int a[maxn],b[maxn];
  10. int m,n;
  11.  
  12. /****
  13. 求序列 A 长度为 N 和序列 B 长度为 M 的 LCS
  14. 序列下标从 1 开始
  15. */
  16. int LCS()
  17. {
  18. for(int i = 1; i <= n; i++)
  19. {
  20. int tmp = 0;
  21. for(int j = 1; j <= m; j++)
  22. {
  23. if(a[i] > b[j] && dp[j] > tmp)
  24. {
  25. tmp = dp[j];
  26. }
  27. else if(a[i] == b[j])
  28. dp[j] = tmp+1;
  29. }
  30. }
  31.  
  32. int ans = 0;
  33. for(int i = 1; i <= m; i++)
  34. ans = max(ans, dp[i]);
  35. return ans;
  36. }
  37.  
  38. int main()
  39. {
  40. int T;
  41. scanf("%d", &T);
  42. while(T--)
  43. {
  44. memset(dp,0,sizeof(dp));
  45.  
  46. scanf("%d", &n);
  47. for(int i = 1; i <= n; i++)
  48. scanf("%d", &a[i]);
  49. scanf("%d", &m);
  50. for(int j = 1; j <= m; j++)
  51. scanf("%d", &b[j]);
  52.  
  53. printf("%d\n",LCS());
  54. if(T != 0) printf("\n");
  55. }
  56. }
















POJ 1423 Greatest Common Increasing Subsequence【裸LCIS】的更多相关文章

  1. HDU 1423 Greatest Common Increasing Subsequence(LCIS)

    Greatest Common Increasing Subsequenc Problem Description This is a problem from ZOJ 2432.To make it ...

  2. 1423 Greatest Common Increasing Subsequence (LCIS)

    讲解摘自百度; 最长公共上升子序列(LCIS)的O(n^2)算法? 预备知识:动态规划的基本思想,LCS,LIS.? 问题:字符串a,字符串b,求a和b的LCIS(最长公共上升子序列).? 首先我们可 ...

  3. HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS)

    HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS) http://acm.hdu.edu.cn/showproblem.php?pi ...

  4. HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】

    HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...

  5. HDU 1423 Greatest Common Increasing Subsequence LCIS

    题目链接: 题目 Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...

  6. POJ 2127 Greatest Common Increasing Subsequence -- 动态规划

    题目地址:http://poj.org/problem?id=2127 Description You are given two sequences of integer numbers. Writ ...

  7. HDOJ 1423 Greatest Common Increasing Subsequence -- 动态规划

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1423 Problem Description This is a problem from ZOJ 2 ...

  8. POJ 2127 Greatest Common Increasing Subsequence

    You are given two sequences of integer numbers. Write a program to determine their common increasing ...

  9. HDUOJ ---1423 Greatest Common Increasing Subsequence(LCS)

    Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536 ...

随机推荐

  1. 纹理mag filter不能取GL_XXX_MIPMAP_XXXX

    今天遇到OpenGL error 0x0500错误,定位到 glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter); 查看ma ...

  2. navigate是Router类的一个方法,主要用来跳转路由。

    navigate是Router类的一个方法,主要用来跳转路由. 1 2 3 4 5 6 7 8 9 interface NavigationExtras {  relativeTo : Activat ...

  3. Atitit.prototype-base class-based  基于“类” vs 基于“原型”

    Atitit.prototype-base class-based  基于“类” vs 基于“原型” 1. 基于“类” vs 基于“原型”1 2.  对象的产生有两种基本方式.一种是以原型(proto ...

  4. html 基本标签 ---字体

    <b> </b>加粗 <i> </i> 斜体 <del> </del> 删除 <ins> </ins> ...

  5. hdu6070 Dirt Ratio 二分+线段树

    /** 题目:hdu6070 Dirt Ratio 链接:http://acm.hdu.edu.cn/showproblem.php?pid=6070 题意:给定n个数,求1.0*x/y最小是多少.x ...

  6. CDH 安装配置指南(Tarball方式)

    采用CDH Tarbal方式安装Hadoop集群. 1. 环境组件版本 组件名称 组件版本 用途 jdk 1.8 jdk-8u191-linux-x64 oracle jdk mysql mysql- ...

  7. Oracle 计算两个时间的差值

    有两个日期数据START_DATE,END_DATE,欲得到这两个日期的时间差(以天,小时,分钟,秒,毫秒):天:ROUND(TO_NUMBER(END_DATE - START_DATE))小时:R ...

  8. hdu1027(n个数的按字典序排列的第m个序列)

    题目信息:给出n.m,求n个数的按字典序排列的第m个序列 http://acm.hdu.edu.cn/showproblem.php? pid=1027 AC代码: /**  *全排列的个数(次序) ...

  9. IPMI特点和功能

    IPMI独立于操作系统外自行运作,并容许管理者即使在缺少操作系统或系统管理软件.或受监控的系统关机但有接电源的情况下仍能远程管理系统. ipmi可以实现对机器的操作举例如下: 开机,关机,重启,查看机 ...

  10. 利用Java编写简单的WebService实例

    使用Axis编写WebService比較简单,就我的理解,WebService的实现代码和编写Java代码事实上没有什么差别,主要是将哪些Java类公布为WebService. 以下是一个从编写測试样 ...