【题解】Greatest Common Increasing Subsequence
【题解】Greatest Common Increasing Subsequence
唉,把自己当做DP入门选手来总结这道题吧,我DP实在太差了
首先是设置状态的技巧,设置状态主要就是要补充不漏并且适合转移。
这样的区间对区间有个设置状态的技巧:一维钦定一维区间
具体来说,是这个意思:
- 我们要方便记录状态 ,所以我们记录一维区间的答案
- 我们要可以转移,所以我们钦定一个状态方便转移
- 我们要方案互斥,所以我们钦定一个状态方便转移(方法同上,钦定这个技巧同时满足了两种要求)
接下来是对于方案的记录:
- 方案随着DP转移,到时候\(O(n)\)回答
对于这一道题目我们这样设计
设\(dp(i,j)\)表示考虑了\(a_1 \to a_i\)的串,钦定以\(b_j\)串结尾的最长公共上升子序列的最大值
有转移方程
\]
记录方案跟着\(dp\)记录即可,很简单。
才怪!
很难(对于我这样的菜鸡来说)
记录的关键是记录\(B\)串,注意一下实现的顺序。
//@winlere
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std; typedef long long ll;
inline int qr(){
register int ret=0,f=0;
register char c=getchar();
while(c<48||c>57)f|=c==45,c=getchar();
while(c>=48&&c<=57)ret=ret*10+c-48,c=getchar();
return f?-ret:ret;
}
const int maxn=505;
int A[maxn],B[maxn],last[maxn][maxn],dp[maxn][maxn],stk[maxn];
int main(){
register int T=qr();
while(T--){
memset(last,-1,sizeof last);
memset(dp,0,sizeof dp);
memset(stk,0,sizeof(stk));
register int n,m,ans=0;
n=qr();
for(register int t=1;t<=n;++t)
A[t]=qr();
m=qr();
for(register int t=1;t<=m;++t)
B[t]=qr();
A[0]=B[0]=1<<31;
for(register int t=0;t<=n;++t)
dp[t][0]=0;
for(register int t=1,fr=0;t<=n;++t){
dp[0][fr=0]=0;
for(register int i=1;i<=m;++i){
dp[t][i]=dp[t-1][i];
if(A[t]==B[i]){
if(dp[t][i]<dp[t-1][fr]+1)
dp[t][i]=dp[t-1][fr]+1,last[t][i]=fr;
//cout<<dp[t][i]<<' '<<t<<' '<<i<<' '<<fr<<' '<<last[t][i]<<endl;
}
if(B[i]<A[t])if(dp[t-1][fr]<dp[t-1][i])fr=i;
}
}
for(register int t=1;t<=m;++t)
if(dp[n][ans]<dp[n][t])
ans=t;
printf("%d\n",dp[n][ans]);
// continue;
if(dp[n][ans]<=0) continue;
int tmp_i=ans;
for(int i=n;i>=1;--i)if(last[i][tmp_i]!=-1)stk[++stk[0]]=A[i],tmp_i=last[i][tmp_i];
for(register int t=stk[0];t>=2;--t)
printf("%d ",stk[t]);
printf("%d \n",stk[1]);
}
return 0;
}
【题解】Greatest Common Increasing Subsequence的更多相关文章
- HDU 1423 Greatest Common Increasing Subsequence LCIS
题目链接: 题目 Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: ...
- POJ 2127 Greatest Common Increasing Subsequence -- 动态规划
题目地址:http://poj.org/problem?id=2127 Description You are given two sequences of integer numbers. Writ ...
- HDOJ 1423 Greatest Common Increasing Subsequence -- 动态规划
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1423 Problem Description This is a problem from ZOJ 2 ...
- ZOJ 2432 Greatest Common Increasing Subsequence(最长公共上升子序列+路径打印)
Greatest Common Increasing Subsequence 题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problem ...
- HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS)
HDU 1423 Greatest Common Increasing Subsequence(最长公共上升LCIS) http://acm.hdu.edu.cn/showproblem.php?pi ...
- 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 ...
- Greatest Common Increasing Subsequence hdu1423
Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
- POJ 2127 Greatest Common Increasing Subsequence
You are given two sequences of integer numbers. Write a program to determine their common increasing ...
- HDUOJ ---1423 Greatest Common Increasing Subsequence(LCS)
Greatest Common Increasing Subsequence Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536 ...
随机推荐
- Topcoder SRM 144 DIV 1
BinaryCode 模拟 题意是:定义串P,Q,其中Q[i]=P[i-1]+P[i]+P[i+1],边界取0,并且P必须是01串.现在给你Q,让你求出P. 做法是:枚举第一位是1还是0,然后就可以推 ...
- STM32命名
STM32产品命名 示例: STM32 F 100 C 6 T 6 B XXX 1 2 3 4 5 6 7 8 9 从上面的料号可以看出以下信息: ST品牌ARM Cortex-Mx系列内核32位超值 ...
- 数据库资源博客---小麦苗BEST
http://blog.csdn.net/lihuarongaini/article/details/60584577 http://blog.csdn.net/lihuarongaini/artic ...
- Git之Github使用(一):Push代码到Github
Git之Github使用(一):Push代码到Github 热度 2已有 58 次阅读2016-8-26 17:56 |个人分类:常见问题|系统分类:移动开发| 互联网, commit, status ...
- Sort Detail Data Block Example - Oracle Forms
Example is given below to sort detail data block data (toggle asc or desc) with push buttons used as ...
- mac复制文件命令
test1下有test01 test02两个文件 ,复制到test2下 则cp -r test1/ test2 权限不够,,则加sudo test2要事先存在, 如果复制test01到当前目录 cp ...
- 第1章 为什么创造WPF、第2章 XAML揭秘
1.2 步入WPF 下面是WPF的一些亮点: 广泛整合:各种媒体类型都能组合起来并一起呈现 与分辨率无关:因为WPF使用矢量图形 硬件加速:WPF是基于Direct3D创建的,工作全部是由GPU完成的 ...
- 微信小程序 - 考试状态不同显示
未开考 .已交卷. 考试中 .考试结束 #ddd #f00 #ff0 默认禁用色 禁用的button仅有style起作用,四个状态,通过wx:if ... elif ... e ...
- 【Python】使用制表符换行符来添加空白
在编程中,在打印时,有时候需要显示出来的数据看着舒服一点,那么使用制表符(\t).换行符(\n)即可轻松实现 >>> print('zhangsan')zhangsan 加入制表符后 ...
- 【转载】ASP.Net请求处理机制初步探索之旅 - Part 3 管道
开篇:上一篇我们了解了一个ASP.Net页面请求的核心处理入口,它经历了三个重要的入口,分别是:ISAPIRuntime.ProcessRequest().HttpRuntime.ProcessReq ...