LCIS POJ 2172 Greatest Common Increasing Subsequence
题意: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)的算法
代码:
- #include <cstdio>
- #include <iostream>
- #include <algorithm>
- #include <cstring>
- #include <map>
- using namespace std;
- const int N = 5e2 + 10;
- const int INF = 0x3f3f3f3f;
- int a[N], b[N], dp[N][N], fx[N][N], fy[N][N];
- int n, m;
- bool fir;
- void print(int x, int y, int last) { //bool fir;
- if (x == 0 || y == 0) return ;
- print (fx[x][y], fy[x][y], y);
- if (y != last) {
- if (fir) printf ("%d", b[y]), fir = false;
- else printf (" %d", b[y]);
- }
- }
- void LCIS(void) {
- memset (dp, 0, sizeof (dp));
- memset (fx, 0, sizeof (fx));
- memset (fy, 0, sizeof (fy));
- int sx = 0, sy = 0;
- int ret = 0, k = 0;
- for (int i=1; i<=n; ++i) {
- k = 0;
- for (int j=1; j<=m; ++j) {
- dp[i][j] = dp[i-1][j]; //以a[]为主循环,每个a[i],去找每个b[j]
- fx[i][j] = i - 1; fy[i][j] = j;
- if (a[i] == b[j] && dp[i][j] < dp[i][k] + 1) { //满足LCS
- dp[i][j] = dp[i][k] + 1; //在1~j-1找到b[k]<a[i],满足LIS,在b[k]上更新dp
- fx[i][j] = i; fy[i][j] = k;
- }
- else if (a[i] > b[j] && dp[i][j] > dp[i][k]) k = j; //找到最优的k
- if (ret < dp[i][j]) {
- ret = dp[i][j]; //更新所有dp中的最大值
- sx = i, sy = j;
- }
- }
- }
- printf ("%d\n", ret);
- fir = true;
- print (sx, sy, -1); puts ("");
- }
- int main(void) {
- while (scanf ("%d", &n) == 1) {
- for (int i=1; i<=n; ++i) scanf ("%d", &a[i]);
- scanf ("%d", &m);
- for (int i=1; i<=m; ++i) scanf ("%d", &b[i]);
- LCIS ();
- }
- return 0;
- }
LCIS POJ 2172 Greatest Common Increasing Subsequence的更多相关文章
- 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 ...
- POJ 2127 Greatest Common Increasing Subsequence -- 动态规划
题目地址:http://poj.org/problem?id=2127 Description You are given two sequences of integer numbers. Writ ...
- POJ 2127 Greatest Common Increasing Subsequence
You are given two sequences of integer numbers. Write a program to determine their common increasing ...
- 最长公共上升子序列 (poj 2127) (Greatest Common Increasing Subsequence)
\(Greatest Common Increasing Subsequence\) 大致题意:给出两个长度不一定相等的数列,求其中最长的公共的且单调递增的子序列(需要具体方案) \(solution ...
- 【简单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 题目:给你两个数字序列,求出这两个序列的最长公共上升子序列.输出最长的长度,并打表 ...
- HDU 1423 Greatest Common Increasing Subsequence LCIS
题目链接: 题目 Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS)
HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS) http://acm.hdu.edu.cn/showproblem.php?pi ...
- HDU 1423 Greatest Common Increasing Subsequence(LCIS)
Greatest Common Increasing Subsequenc Problem Description This is a problem from ZOJ 2432.To make it ...
- HDOJ 1423 Greatest Common Increasing Subsequence -- 动态规划
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1423 Problem Description This is a problem from ZOJ 2 ...
随机推荐
- 微信发明人竟是他!也是WeChat/Line/WhatsApp的发明者
赵建文,很多人不知道他是谁:说到微信大家都耳熟能详吧?没错,他就是初始微信发明人,同时也是WeChat/Line/WhatsApp的发明者!正是他的专利<一种基于或囊括手机电话本的即时通讯方法和 ...
- Awvs如何扫描需要登录的部分
一个小技巧,可能部分的人习惯了按下一步.所以不大知道.给大家说说哈. 到LOGIN步骤的时候,在Login sequen 新建.然后你懂的了.会新出来一个浏览器,直接登录后一直下一步即可
- hiho一下 第九十六周 数论五·欧拉函数
题目1 : 数论五·欧拉函数 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho有时候会用密码写信来互相联系,他们用了一个很大的数当做密钥.小Hi和小Ho约定 ...
- 减小Delphi XE5编译出来的程序体积
默认Delphi XE, XE2, XE3,XE4,XE5, XE6 ... 编译出来的程序体积很大. 一般用两个方法可以很大程度上减少程序体积. 一.在工程中用编译指令禁用RTTI 禁用的方法很简单 ...
- Android自动登录与记住密码
// 获取实例对象 sp = this.getSharedPreferences("userInfo", Context.MODE_WORLD_READABLE); rem_pw ...
- 在 FREEBUF 投放广告
在 FREEBUF 投放广告 FreebuF黑客与极客—高质量的全球互联网安全媒体,同时也是爱好者们交流.分享安全技术的最佳平台.本站读者群以IT.政企信息安全人员.互联网安全爱好者和学生为主,对互联 ...
- 《ASP.NET MVC4 WEB编程》学习笔记------Web API 续
目录 ASP.NET WEB API的出现缘由 ASP.NET WEB API的强大功能 ASP.NET WEB API的出现缘由 随着UI AJAX 请求适量的增加,ASP.NET MVC基于Jso ...
- windows2003批量添加和导出所有ip
批量添加IP 在cmd命令行下运行: FOR /L %i IN (130,1,190) DO netsh interface ip add address "本地连接" 192.1 ...
- 如何使用box2d做碰撞检测
cocos2dx3.0+vs2012编译通过. 主要是通过body->SetTransform来设置body的位置和角度,然后自己写个ContactListener来监听碰撞事件 源代码下载 # ...
- c# 获取屏幕DPI
方法一:用ManagementClass来获取.需要引入System.Management.dll; using (ManagementClass mc = new ManagementClass(& ...