题目大意:

给定一个字符串 字符为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 最长不降子序列->最长公共子序列的更多相关文章

  1. DP_最长公共子序列/动规入门

    学自:https://open.163.com/movie/2010/12/L/4/M6UTT5U0I_M6V2U1HL4.html 最长公共子序列:(本文先谈如何求出最长公共子序列的长度,求出最长公 ...

  2. 最长上升子序列(LIS)与最长公共子序列(LCS)

    1.LIS : 给定一个序列,求它的最长上升子序列(n<=2000) 第一种 O(n^2): dp[i] 为以i为开头的最长上升子序列长度 code1: #include<cstdio&g ...

  3. 最长非降/下降子序列问题(DP)(待续...)

    注意:抽象成以下描述即为最长非降/下降子序列问题(一维状态) 问题描述:在一个无序的序列a1,a2,a3,a4…an里,找到一个最长的序列满足:(不要求连续) ai<=aj<=ak…< ...

  4. DP:凑零钱问题/最长非降子序列(C++)

    你给出一定数额的钱 i 元给我,我利用手中的硬币(m元, j元, k元...)兑换等值的钱给你,要求硬币数最少. 举例:给出1-11的钱,手中硬币有1元,3元,5元. 重点是找到状态和状态转移方程. ...

  5. HDU 1025-Constructing Roads In JGShining's Kingdom(最长不降子序列,线段树优化)

    分析: 最长不降子序列,n很大o(n^2)肯定超,想到了小明序列那个题用线段树维护前面的最大值即可 该题也可用二分搜索来做. 注意问题输出时的坑,路复数后加s #include <map> ...

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

  7. (最长不降子序列)最少拦截系统 -- hdu -- 1257

    http://acm.hdu.edu.cn/showproblem.php?pid=1257 最少拦截系统 Time Limit: 2000/1000 MS (Java/Others)    Memo ...

  8. dp之最长递增、公共子序列总结

    1.最长递增子序列模板poj2533(时间复杂度O(n*n)) #include<iostream> #include<stdio.h> #include<string. ...

  9. HDU 1159 Common Subsequence --- DP入门之最长公共子序列

    题目链接 基础的最长公共子序列 #include <bits/stdc++.h> using namespace std; ; char c[maxn],d[maxn]; int dp[m ...

随机推荐

  1. 数据库(一)—— MySQL介绍

    目录 MySQL介绍 一.MySQL版本 1.mysql主流版本 2.版本选择 二.MySQL连接与实例 1.MySQL的C/S结构 2.MySQL实例 三.mysql三层结构 1.连接层(连接上数据 ...

  2. VS2008中所有快捷键

    转载自:http://itfocus.diandian.com/post/2011-09-16/5091994 微软开发环境的可视化界面做的很全面,几乎所有的操作都可以通过可视化界面完成,但是你是否在 ...

  3. 更换nginx默认端口以及配置文件位置

    前言 近段时间在准备毕业设计的前期准备,基本确定了前后端分离的架构,于是就需要用到了nginx. 在之前nginx是放在docker上,所以没有端口更改跟配置文件配置的烦恼.但是现在是直接放在服务器上 ...

  4. jumpserver注意事项以及报错处理

    需要注意下面亮点 在使用jumpserver过程中,有一步是系统用户推送,要推送成功,client(后端服务器)要满足以下条件: 后端服务器需要有python.sudo环境才能使用推送用户,批量命令等 ...

  5. Jsoup爬虫任务总结

    这两周由于公司需要大量数据爬取进数据库给用户展示素材,在不停的做爬虫工作,现在总算基本完成就剩清理数据的工作: 公司有一个采集器管理后台的项目,可以直接把爬虫代码打包成jar导入进去设置定时参数即可: ...

  6. input输入框数字转带千分位的字符串

    数字转带千分位的字符串 思路 先获取要转换的数字,对其进行分割 小数部分具体需要保留多少位,具体处理 整数部分用正则做替换 将小数部分和整数部分合计 代码 注意: 本文是基于 jQuery 写的,稍稍 ...

  7. react-native run-android出现红屏错误

    react-native run-android出现 unable to load script from assets 'index.android.bundle'.Make sure your b ...

  8. Splay(区间翻转)&树套树(Splay+线段树,90分)

    study from: https://tiger0132.blog.luogu.org/slay-notes P3369 [模板]普通平衡树 #include <cstdio> #inc ...

  9. Mina---系统学习

    1.为何使用Mina? java提供的BIO.NIO使用的复杂性等原因,导致Mina框架的诞生: 2.什么时候使用Mina? 易于使用 高并发的用户量 被证明的系统: Mina已被全球数以万计的应用使 ...

  10. Dart编程实例 - Final 关键字

    Dart编程实例 - Final 关键字 void main() { final val1 = 12; print(val1); } 本文转自:http://codingdict.com/articl ...