题目传送门

题意:LCIS(Longest Common Increasing Subsequence) 最长公共上升子序列

分析:a[i] != b[j]: dp[i][j] = dp[i-1][j]; a[i]==b[j]:  dp[j]=max(dp[j],dp[k]); (1<=k<j&&b[k]<b[j])  打印路径时按照b[i]来输出

收获:理解不是很深入,推荐资料: 最长公共上升子序列(LCIS)的O(n^2)算法  最长公共上升子序列的另一个O(mn)的算法

 

代码:

  1. #include <cstdio>
  2. #include <iostream>
  3. #include <algorithm>
  4. #include <cstring>
  5. #include <map>
  6. using namespace std;
  7.  
  8. const int N = 5e2 + 10;
  9. const int INF = 0x3f3f3f3f;
  10. int a[N], b[N], dp[N][N], fx[N][N], fy[N][N];
  11. int n, m;
  12. bool fir;
  13.  
  14. void print(int x, int y, int last) { //bool fir;
  15. if (x == 0 || y == 0) return ;
  16. print (fx[x][y], fy[x][y], y);
  17. if (y != last) {
  18. if (fir) printf ("%d", b[y]), fir = false;
  19. else printf (" %d", b[y]);
  20. }
  21. }
  22.  
  23. void LCIS(void) {
  24. memset (dp, 0, sizeof (dp));
  25. memset (fx, 0, sizeof (fx));
  26. memset (fy, 0, sizeof (fy));
  27. int sx = 0, sy = 0;
  28. int ret = 0, k = 0;
  29. for (int i=1; i<=n; ++i) {
  30. k = 0;
  31. for (int j=1; j<=m; ++j) {
  32. dp[i][j] = dp[i-1][j]; //以a[]为主循环,每个a[i],去找每个b[j]
  33. fx[i][j] = i - 1; fy[i][j] = j;
  34. if (a[i] == b[j] && dp[i][j] < dp[i][k] + 1) { //满足LCS
  35. dp[i][j] = dp[i][k] + 1; //在1~j-1找到b[k]<a[i],满足LIS,在b[k]上更新dp
  36. fx[i][j] = i; fy[i][j] = k;
  37. }
  38. else if (a[i] > b[j] && dp[i][j] > dp[i][k]) k = j; //找到最优的k
  39. if (ret < dp[i][j]) {
  40. ret = dp[i][j]; //更新所有dp中的最大值
  41. sx = i, sy = j;
  42. }
  43. }
  44. }
  45. printf ("%d\n", ret);
  46. fir = true;
  47. print (sx, sy, -1); puts ("");
  48. }
  49.  
  50. int main(void) {
  51. while (scanf ("%d", &n) == 1) {
  52. for (int i=1; i<=n; ++i) scanf ("%d", &a[i]);
  53. scanf ("%d", &m);
  54. for (int i=1; i<=m; ++i) scanf ("%d", &b[i]);
  55. LCIS ();
  56. }
  57.  
  58. return 0;
  59. }

  

LCIS POJ 2172 Greatest Common Increasing Subsequence的更多相关文章

  1. POJ 1423 Greatest Common Increasing Subsequence【裸LCIS】

    链接: http://acm.hdu.edu.cn/showproblem.php?pid=1423 http://acm.hust.edu.cn/vjudge/contest/view.action ...

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

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

  3. POJ 2127 Greatest Common Increasing Subsequence

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

  4. 最长公共上升子序列 (poj 2127) (Greatest Common Increasing Subsequence)

    \(Greatest Common Increasing Subsequence\) 大致题意:给出两个长度不一定相等的数列,求其中最长的公共的且单调递增的子序列(需要具体方案) \(solution ...

  5. 【简单dp】poj 2127 Greatest Common Increasing Subsequence【最长公共上升子序列】【模板】

    Sample Input 5 1 4 2 5 -12 4 -12 1 2 4 Sample Output 2 1 4 题目:给你两个数字序列,求出这两个序列的最长公共上升子序列.输出最长的长度,并打表 ...

  6. HDU 1423 Greatest Common Increasing Subsequence LCIS

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

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

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

  8. HDU 1423 Greatest Common Increasing Subsequence(LCIS)

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

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

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

随机推荐

  1. 微信发明人竟是他!也是WeChat/Line/WhatsApp的发明者

    赵建文,很多人不知道他是谁:说到微信大家都耳熟能详吧?没错,他就是初始微信发明人,同时也是WeChat/Line/WhatsApp的发明者!正是他的专利<一种基于或囊括手机电话本的即时通讯方法和 ...

  2. Awvs如何扫描需要登录的部分

    一个小技巧,可能部分的人习惯了按下一步.所以不大知道.给大家说说哈. 到LOGIN步骤的时候,在Login sequen 新建.然后你懂的了.会新出来一个浏览器,直接登录后一直下一步即可

  3. hiho一下 第九十六周 数论五·欧拉函数

    题目1 : 数论五·欧拉函数 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho有时候会用密码写信来互相联系,他们用了一个很大的数当做密钥.小Hi和小Ho约定 ...

  4. 减小Delphi XE5编译出来的程序体积

    默认Delphi XE, XE2, XE3,XE4,XE5, XE6 ... 编译出来的程序体积很大. 一般用两个方法可以很大程度上减少程序体积. 一.在工程中用编译指令禁用RTTI 禁用的方法很简单 ...

  5. Android自动登录与记住密码

    // 获取实例对象 sp = this.getSharedPreferences("userInfo", Context.MODE_WORLD_READABLE); rem_pw ...

  6. 在 FREEBUF 投放广告

    在 FREEBUF 投放广告 FreebuF黑客与极客—高质量的全球互联网安全媒体,同时也是爱好者们交流.分享安全技术的最佳平台.本站读者群以IT.政企信息安全人员.互联网安全爱好者和学生为主,对互联 ...

  7. 《ASP.NET MVC4 WEB编程》学习笔记------Web API 续

    目录 ASP.NET WEB API的出现缘由 ASP.NET WEB API的强大功能 ASP.NET WEB API的出现缘由 随着UI AJAX 请求适量的增加,ASP.NET MVC基于Jso ...

  8. windows2003批量添加和导出所有ip

    批量添加IP 在cmd命令行下运行: FOR /L %i IN (130,1,190) DO netsh interface ip add address "本地连接" 192.1 ...

  9. 如何使用box2d做碰撞检测

    cocos2dx3.0+vs2012编译通过. 主要是通过body->SetTransform来设置body的位置和角度,然后自己写个ContactListener来监听碰撞事件 源代码下载 # ...

  10. c# 获取屏幕DPI

    方法一:用ManagementClass来获取.需要引入System.Management.dll; using (ManagementClass mc = new ManagementClass(& ...