hdu6537 /// DP 最长不降子序列->最长公共子序列
题目大意:
给定一个字符串 字符为0~9
求翻转某个区间后使得串中的最长不降子序列最长
因为字符范围为0~9
假设有一个 0 1 2 3 4 5 6 7 8 9 的序列
此时翻转某个区间得到形如 0 1 ... L【R R-1 ... L+1 L】R R+1 ... 9 的序列
用这个序列与原串匹配一个最长公共子序列
题解:
https://www.cnblogs.com/ZERO-/p/9493597.html
https://blog.csdn.net/qkoqhh/article/details/81491097
//#include <bits/stdc++.h>
//using namespace std;
//#define INF 0x3f3f3f3f
//#define LL long long
//#define mem(i,j) memset(i,j,sizeof(i))
//const int N=1e5+5;
//
//int n, b[15];
//char str[N]; int a[N];
//int dp[N][15], pre[N][15];
//
//int main()
//{
// int t; scanf("%d",&t);
// while(t--) {
// int ans=0,ansl,ansr; scanf("%d%s",&n,str);
// for(int i=0;i<n;i++) a[i+1]=str[i]-'0';
// for(int L=1;L<=9;L++)
// for(int R=L;R<=9;R++) {
// int tot=0;
// for(int k=0;k<=L;k++) b[++tot]=k;
// for(int k=R;k>=L;k--) b[++tot]=k;
// for(int k=R;k<=9;k++) b[++tot]=k;
// for(int i=1;i<=n;i++) {
// int t=0;
// for(int j=1;j<=tot;j++) {
// if(dp[i-1][j]>dp[i-1][t]) t=j;
// pre[i][j]=t;
// dp[i][j]=dp[i-1][t]+(a[i]==b[j]);
// }
// }
// for(int j=tot;j>=1;j--)
// if(dp[n][j]>ans) {
// ans=dp[n][j];
// int t=j,l=0,r=0;
// for(int i=n;i>=0;i--) {
// if(!l && t<=L+1) l=i+1;
// if(!r && t<=R+2) r=i;
// t=pre[i][t];
// }
// if(r==0) r=l;
// ansl=l,ansr=r;
// }
// }
// printf("%d %d %d\n",ans,ansl,ansr);
// }
//
// return 0;
//}
#include <bits/stdc++.h>
using namespace std;
#define INF 0x3f3f3f3f
#define LL long long
#define mem(i,j) memset(i,j,sizeof(i))
const int N=1e5+; int n, b[];
char str[N]; int a[N];
int dp[N][], pre[N][]; int main(){
int t; scanf("%d",&t);
while(t--) {
int ansl,ansr,ans=; scanf("%d%s",&n,str);
for(int i=;i<n;i++) a[i+]=str[i]-'';
for(int L=;L<=;L++) /// 枚举翻转区间
for(int R=L;R<=;R++) {
int tot=;
for(int i=;i<=L;i++) b[++tot]=i;
for(int i=R;i>=L;i--) b[++tot]=i;
for(int i=R;i<=;i++) b[++tot]=i;
for(int i=;i<=n;i++) {
int t=;
for(int j=;j<=tot;j++) {
if(dp[i-][j]>dp[i-][t]) t=j;
pre[i][j]=t; // 记录前驱在b[]中的位置
dp[i][j]=dp[i-][t]+(a[i]==b[j]); // 更新LCS长度
}
}
for(int j=tot;j>=;j--)
if(dp[n][j]>ans) {
ans=dp[n][j];
int t=j,l=,r=;
for(int i=n;i>=;i--) {
// 翻转区间为 0 1 ... L【R R-1 ... L+1 L】R R+1 ... 9
// 所以区间左端l位置实际是在L+1
// 所以区间右端r位置实际是在R+2
if(!l && t<=L+) l=i+;
if(!r && t<=R+) r=i;
// 当t满足位置条件 才是找到l r
t=pre[i][t];
}
if(r==)r=l;
ansl=l; ansr=r;
}
}
printf("%d %d %d\n",ans,ansl,ansr);
}
return ;
}
hdu6537 /// DP 最长不降子序列->最长公共子序列的更多相关文章
- DP_最长公共子序列/动规入门
学自:https://open.163.com/movie/2010/12/L/4/M6UTT5U0I_M6V2U1HL4.html 最长公共子序列:(本文先谈如何求出最长公共子序列的长度,求出最长公 ...
- 最长上升子序列(LIS)与最长公共子序列(LCS)
1.LIS : 给定一个序列,求它的最长上升子序列(n<=2000) 第一种 O(n^2): dp[i] 为以i为开头的最长上升子序列长度 code1: #include<cstdio&g ...
- 最长非降/下降子序列问题(DP)(待续...)
注意:抽象成以下描述即为最长非降/下降子序列问题(一维状态) 问题描述:在一个无序的序列a1,a2,a3,a4…an里,找到一个最长的序列满足:(不要求连续) ai<=aj<=ak…< ...
- DP:凑零钱问题/最长非降子序列(C++)
你给出一定数额的钱 i 元给我,我利用手中的硬币(m元, j元, k元...)兑换等值的钱给你,要求硬币数最少. 举例:给出1-11的钱,手中硬币有1元,3元,5元. 重点是找到状态和状态转移方程. ...
- HDU 1025-Constructing Roads In JGShining's Kingdom(最长不降子序列,线段树优化)
分析: 最长不降子序列,n很大o(n^2)肯定超,想到了小明序列那个题用线段树维护前面的最大值即可 该题也可用二分搜索来做. 注意问题输出时的坑,路复数后加s #include <map> ...
- Codeforces Round #198 (Div. 2) D. Bubble Sort Graph (转化为最长非降子序列)
D. Bubble Sort Graph time limit per test 1 second memory limit per test 256 megabytes input standard ...
- (最长不降子序列)最少拦截系统 -- hdu -- 1257
http://acm.hdu.edu.cn/showproblem.php?pid=1257 最少拦截系统 Time Limit: 2000/1000 MS (Java/Others) Memo ...
- dp之最长递增、公共子序列总结
1.最长递增子序列模板poj2533(时间复杂度O(n*n)) #include<iostream> #include<stdio.h> #include<string. ...
- HDU 1159 Common Subsequence --- DP入门之最长公共子序列
题目链接 基础的最长公共子序列 #include <bits/stdc++.h> using namespace std; ; char c[maxn],d[maxn]; int dp[m ...
随机推荐
- 力扣算法——140WordBreakII【H】
Given a non-empty string s and a dictionary wordDict containing a list of non-empty words, add space ...
- web跨域
之前对于跨域相关的知识一致都很零碎,正好现在的代码中用到了跨域相关的,现在来对这些知识做一个汇总整理,方便自己查看,说不定也可能对你有所帮助. 本篇主要内容如下: 浏览器同源策略 http 请求跨域 ...
- 轻松理解https,So easy!
Java技术栈 www.javastack.cn 优秀的Java技术公众号 作者:翟志军 https://showme.codes/2017-02-20/understand-https/ 本文尝试一 ...
- SiteMesh 2.X 的使用(网页结构模板)
SiteMesh是基于Servlet的filter的,即过滤流.它是通过截取reponse,并进行装饰后再交付给客户. 其中涉及到两个名词: 装饰页面(decorator page)和 "被 ...
- 惠普笔记本Ubuntu系统HDMI无输出
- Linux 任务管理器(二)
特殊文件与进程 fuser命令 [root@localhost home]# fuser -muv . 用户 进程号 权限 命令 /home: root kernel mount (root)/hom ...
- codeforces1156D 0-1-Tree 换根dp
题目传送门 题意: 给定一棵n个点的边权为0或1的树,一条合法的路径(x,y)(x≠y)满足,从x走到y,一旦经过边权为1的边,就不能再经过边权为0的边,求有多少边满足条件? 思路: 首先,这道题也可 ...
- 编译错误提示PATH_MAX未声明
解决办法: grep一下,发现PATH_MAX在limits.h中定义,在/scripts/mod/sumversion.c中添加#include <limits.h>即可. #inclu ...
- log库
https://github.com/orocos-toolchain/log4cpp https://github.com/search?q=glog zlog https://github.com ...
- ssh-key添加之后依旧需要密码输入Bug的解决
场景重现 要求从10.183.93.181的root用户ssh免密登录至10.110.155.26的boss用户 1.在10.110.155.26 的boss用户下面新建目录.ssh 2.在10.11 ...