【题解】Greatest Common Increasing Subsequence

vj

唉,把自己当做DP入门选手来总结这道题吧,我DP实在太差了

首先是设置状态的技巧,设置状态主要就是要补充不漏并且适合转移。

这样的区间对区间有个设置状态的技巧:一维钦定一维区间

具体来说,是这个意思:

  • 我们要方便记录状态 ,所以我们记录一维区间的答案
  • 我们要可以转移,所以我们钦定一个状态方便转移
  • 我们要方案互斥,所以我们钦定一个状态方便转移(方法同上,钦定这个技巧同时满足了两种要求)

接下来是对于方案的记录:

  • 方案随着DP转移,到时候\(O(n)\)回答

对于这一道题目我们这样设计

设\(dp(i,j)\)表示考虑了\(a_1 \to a_i\)的串,钦定以\(b_j\)串结尾的最长公共上升子序列的最大值

有转移方程

\[dp(i,j)=max\{dp(x|x<i,j),dp(i-1,x|x<j\and B[x]<A[i])\}
\]

记录方案跟着\(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的更多相关文章

  1. HDU 1423 Greatest Common Increasing Subsequence LCIS

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

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

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

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

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

  4. ZOJ 2432 Greatest Common Increasing Subsequence(最长公共上升子序列+路径打印)

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

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

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

  6. 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 ...

  7. Greatest Common Increasing Subsequence hdu1423

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

  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. Java中final和static关键字总结

    1.final: final关键字可用于类.方法.变量前. final修饰的类不可被继承,例如java.lang.Math就是一个 final类,不可被继承. final修饰变量,在显示初始化后不可改 ...

  2. servlet源码查看

    1,下载源码,点击此处可下载 2,创建web项目 我这里以jdbc这个web项目为例讲解 在javaee libraries中有个javaee.jar包,选中它-->右击-->Proper ...

  3. COFF - 中间文件格式解析

    http://www.cnblogs.com/weikanzong/p/5296739.html

  4. 3 Suggested Oracle Certifications For Oracle Form's Developers

    The following are the most suggested Oracle Certifications for Oracle Application Developers in Form ...

  5. 利用mvn/maven如何检查依赖冲突,并解决依赖冲突

    mvn/maven如何检查依赖冲突,并解决依赖冲突 如图,点击图示位置,就可以把整个项目的依赖关系展示出来 在图里选中一个artifact,则所有依赖该artifact的地方都会一起连带出来突出显示, ...

  6. spring Multiple MongoTemplate

    <!-- 数据环境配置 --> <mongo:repositories base-package="com.my9yu.manager.module.server.repo ...

  7. Another unnamed CacheManager already exists in the same VM

    今天学习Spring 缓存机制.遇到不少问题~ 好不easy缓存的单元測试用例调试成功了,在同一项目下单元測试另外一个文件时,发生了异常: org.springframework.beans.fact ...

  8. AngularJS取得后台Jason数据显示在页面上

    代码下载:https://files.cnblogs.com/files/xiandedanteng/angularjsAjax.rar 前台代码: <%@ page language=&quo ...

  9. 路飞学城Python爬虫课第一章笔记

    前言 原创文章,转载引用务必注明链接.水平有限,如有疏漏,欢迎指正. 之前看阮一峰的博客文章,介绍到路飞学城爬虫课程限免,看了眼内容还不错,就兴冲冲报了名,99块钱满足以下条件会返还并送书送视频. 缴 ...

  10. Shell脚本之:while

    while循环用于不断执行一系列命令,也用于从输入文件中读取数据:命令通常为测试条件.其格式为: while command do Statement(s) to be executed if com ...