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 ...
随机推荐
- shell编程:字符串练习题string.sh
string.sh脚本 #!/bin/bash # string="Bigdata process framework is Hadoop,Hadoop is an open source ...
- __init__ 和__new__的区别
__init__和__new__的区别 __init__是当实例对象创建完成后被调用的,然后设置对象属性的一些初始值. __new__是在实例创建之前被调用的,因为它的任务就是创建实例然后返回该实例, ...
- springboot多数据库及分布式事务配置
1.导入相应的jar包依赖 <!-- 集成mybatis --> <dependency> <groupId>org.mybatis.spring.boot< ...
- Java技术专区-虚拟机系列-虚拟机参数(常用)
基础参数系类(内存分配) -server:一定要作为第一个参数,在多个CPU时性能佳 -Xmn:young generation的heap大小,一般设置为Xmx的3.4分之一-Xms:初始Heap大小 ...
- pytest-skip详解
import pytestimport sysenvironment='android' #设置系统变量为android @pytest.mark.skipif(environment==" ...
- Python第十二节 异常
Python 第十三节 异常 python的错误一般包含两类: 语法错误 : 语法错误一般就是, 当你写完程序提交的时候, 发现无法提交, 提示存在错误.例如: 英文逗号 错写成了 中文逗号 异常 : ...
- Codeforces 1148F Foo Fighters 贪心
题意:给你若干个数对,每个数对有两个属性,一个属性是权值,一个属性是位标志,假设这些数对的的权值和是sum,你可以选择一个二进制数s,与所有的数对的位标志按位与,如果按位与之后的位标志有奇数个1,那么 ...
- 通信有连接有消息队列选择boost.asio
通信有连接有消息队列选择boost.asio 连接自主管理 消息队列自主管理
- linux中CentOS、Ubuntu、Debian三个版本系统 差别
Linux有非常多的发行版本,从性质上划分,大体分为由商业公司维护的商业版本与由开源社区维护的免费发行版本. 商业版本以Redhat为代表,开源社区版本则以debian为代表.这些版本各有不同的特点, ...
- JZOI1142 排队布局
#include <bits/stdc++.h> using namespace std; inline int read() { int x = 0,tmp = 1;char ch = ...