题意

如标题。

\(|s1|,|s2| \leq 500\)

分析

既然是dp问题的组合,那么考虑dp。
定义状态f(i,j)表示对第一个序列s1的前i个和第二个序列s2的前j个元素求最长上升公共子序列,并且s1的第i个元素和s2的第j个元素匹配的结果,显然,当且仅当s1[i]=s2[j]时,f(i,j)有意义。
转移方程为:
\[f(i,j)=\max\{f(i',j')|i'<i,j'<j,s2[j']<s1[i]\}+1\]
这个朴素做法的时间复杂度为\(O(n^4)\)。我们尝试优化此方程,记\(opt(j')=\max\{f(i',j')\}\),保证f(i',j')有意义。那么转移方程为:
\[f(i,j)=\max\{opt(j')|j'<j,s2[j']<s1[i]\}+1\]
事实上,只需要从小到大枚举i,然后及时更新opt,就可以将转移的复杂度降为O(n)了。这样时间复杂度为\(O(n^3)\),已经可以过了。

代码

  1. #include<cstdlib>
  2. #include<cstdio>
  3. #include<cmath>
  4. #include<cstring>
  5. #include<ctime>
  6. #include<iostream>
  7. #include<string>
  8. #include<vector>
  9. #include<list>
  10. #include<deque>
  11. #include<stack>
  12. #include<queue>
  13. #include<map>
  14. #include<set>
  15. #include<bitset>
  16. #include<algorithm>
  17. #include<complex>
  18. #pragma GCC optimize ("O0")
  19. using namespace std;
  20. template<class T> inline T read(T&x)
  21. {
  22. T data=0;
  23. int w=1;
  24. char ch=getchar();
  25. while(!isdigit(ch))
  26. {
  27. if(ch=='-')
  28. w=-1;
  29. ch=getchar();
  30. }
  31. while(isdigit(ch))
  32. data=10*data+ch-'0',ch=getchar();
  33. return x=data*w;
  34. }
  35. typedef long long ll;
  36. const int INF=0x7fffffff;
  37. const int MAXN=5e2+7;
  38. int n,m;
  39. int a[MAXN],b[MAXN];
  40. int f[MAXN][MAXN];
  41. int opt[MAXN];
  42. int main()
  43. {
  44. // freopen(".in","r",stdin);
  45. // freopen(".out","w",stdout);
  46. int T;
  47. read(T);
  48. while(T--)
  49. {
  50. memset(f,0,sizeof(f)); // edit 1
  51. memset(opt,0,sizeof(opt));
  52. read(n);
  53. for(int i=1;i<=n;++i)
  54. read(a[i]);
  55. read(m);
  56. for(int i=1;i<=m;++i)
  57. read(b[i]);
  58. int ans=0;
  59. for(int i=1;i<=n;++i)
  60. for(int j=1;j<=m;++j)
  61. {
  62. if(a[i]!=b[j])
  63. continue;
  64. int t=0;
  65. for(int k=1;k<j;++k)
  66. if(b[k]<a[i])
  67. t=max(t,opt[k]);
  68. f[i][j]=t+1;
  69. opt[j]=max(opt[j],f[i][j]);
  70. ans=max(ans,f[i][j]);
  71. }
  72. printf("%d\n",ans);
  73. if(T) // edit 2
  74. puts("");
  75. }
  76. // fclose(stdin);
  77. // fclose(stdout);
  78. return 0;
  79. }

HDU1423 Greatest Common Increasing Subsequence的更多相关文章

  1. HDU4512完美队形I && HDU1423 Greatest Common Increasing Subsequence (LCIS)

    填坑的时候又到啦,校赛因为不会LCIS所以吃了大亏,这里要补起来.LCIS就是在两个串里找最长上升子序列,相关的博客有很多,这里自己就不写那么多了. http://www.cnblogs.com/ja ...

  2. HDU1423 Greatest Common Increasing Subsequence (DP优化)

    LIS和LCS的结合. 容易写出方程,复杂度是nm2,但我们可以去掉一层没有必要的枚举,用一个变量val记录前一阶段的最优解,这样优化成nm. 1<=k<j,j增加1,k的上界也增加1,就 ...

  3. HDU1423:Greatest Common Increasing Subsequence(LICS)

    Problem Description This is a problem from ZOJ 2432.To make it easyer,you just need output the lengt ...

  4. Greatest Common Increasing Subsequence hdu1423

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

  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. ZOJ 2432 Greatest Common Increasing Subsequence(最长公共上升子序列+路径打印)

    Greatest Common Increasing Subsequence 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...

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

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

随机推荐

  1. SpringBoot 中常用注解@Controller/@RestController/@RequestMapping介绍

    原文 SpringBoot 中常用注解 @Controller/@RestController/@RequestMapping介绍 @Controller 处理http请求 @Controller / ...

  2. Windows下openssl的下载安装和使用

    Windows下openssl的下载安装和使用 安装openssl有两种方式,第一种直接下载安装包,装上就可运行:第二种可以自己下载源码,自己编译.下面对两种方式均进行详细描述. 一.下载和安装ope ...

  3. ViewPagerIndicator+viewpager的简单使用,不需要导入Library包

    ViewPagerIndicator作为一款分页指标小部件兼容ViewPager,封装上做得非常不错,目前已为众多知名应用所使用. ViewPagerIndicator+viewpager实现如下效果 ...

  4. Confluence 6 LDAP 用户组结构设置

    用户组对象类(Group Object Class) 这是在 LDAP 用户组对象中使用的类的名字.例如: groupOfUniqueNames group 用户组对象过滤器(Group Object ...

  5. python-day45--mysql索引

    一 .介绍 为何要有索引? 一些复杂的查询操作,对查询语句的优化显然是重中之重.说起加速查询,就不得不提到索引了. 什么是索引? 索引在MySQL中也叫做“键”,是存储引擎用于快速找到记录的一种数据结 ...

  6. 基础最短路(模板 spfa)

    Description 虽然草儿是个路痴(就是在杭电待了一年多,居然还会在校园里迷路的人,汗~),但是草儿仍然很喜欢旅行,因为在旅途中 会遇见很多人(白马王子,^0^),很多事,还能丰富自己的阅历,还 ...

  7. Leetcode 96

    class Solution { public: int numTrees(int n) { ]; dp[] = ; dp[] = ; dp[] = ; ;i <= n;i++){ ; ;j & ...

  8. iOS UI-标签控制器(UITabBarController)

    #import "AppDelegate.h" #import "FirstViewController.h" #import "SecondView ...

  9. 进程控制fork vfork,父子进程,vfork保证子进程先运行

    主要函数: fork 用于创建一个新进程 exit 用于终止进程 exec 用于执行一个程序 wait 将父进程挂起,等待子进程结束 getpid 获取当前进程的进程ID nice 改变进程的优先级 ...

  10. bat批处理文件运行时隐藏cmd窗口

    想让bat运行时隐藏cmd窗口,最好的方法是使用vbs文件实现, 1.新建一个文本文档,改名为123.vbs,编辑内容: set ws=WScript.CreateObject("WScri ...