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 ...
随机推荐
- Centos7使用Python3
1.安装python3替换python2.7 [root@Python src]# wget https://www.python.org/ftp/python/3.5.4/Python-3.5.4. ...
- css篇-简化版
[CSS篇]简化版 (1) CSS盒模型 CSS盒模型 题目:谈谈你对CSS盒模型的认识 1) 基本概念:标准模型+IE模型 2) 标准模型和IE模型的区别 计算宽度和 ...
- 在Python中检测*可用* CPU数量的便携方式
根据这个问题和答案 - Python multiprocessing.cpu_count()在4核Nvidia Jetson TK1上返回'1' - Python multiprocessing.cp ...
- JDBC getConnection细节
https://blog.csdn.net/luanlouis/article/details/29850811 概述 一般情况下,在应用程序中进行数据库连接,调用JDBC接口 ...
- mongoose 常用数据库操作 删除
删除 Model.remove(conditions, [callback]) try.js var User = require("./user.js"); function d ...
- 使用appium1.4在android8.0真机上测试程序时报错command failed shell "ps 'uiautomator'"的解决方式
appium1.4,运行自动化脚本时提示 org.openqa.selenium.SessionNotCreatedException: A new session could not be crea ...
- springBoot框架在idea中创建流程 同时存在一个项目中
1.新建普通maven工程 2.在父级pom中按需修改 3.删除父级src目录 4.创建公共模块common,里面只有service接口和实体类 5.构建微服务模块,provider 6.引用Zook ...
- Java中几种排序算法
1.冒泡排序算法 通过多次比较(相邻两个数)和交换来实现排序 public class bubble { public static void bubbleSort(int[] a) { int te ...
- loadrunner——win7+LR11配置
一. 安装vmware虚拟机 下载安装vmware15后,可使用密钥为:CG392-4PX5J-H816Z-HYZNG-PQRG2 二. 安装win7系统 2.1下载win7镜像文件 2.2 vmwa ...
- apue 第18章 终端I/O
终端I/O有两种不同的工作模式: (1)规范模式:输入以行单位进行处理,每个读请求也最多返回一行. (2)非规范模式:输入字符不装配成行. 终端设备是由通常位于内核中的终端驱动程序控制的.每个终端设备 ...