TZOJ 5963 Increasing Sequences(线性DP)
描述
Given a string of digits, insert commas to create a sequence of strictly increasing numbers so as to minimize the magnitude of the last number. For this problem, leading zeros are allowed in front of a number.
输入
Input will consist of multiple test cases. Each case will consist of one line, containing a string of digits of maximum length 80. A line consisting of a single 0 terminates input.
输出
For each instance, output the comma separated strictly increasing sequence, with no spaces between commas or numbers. If there are several such sequences, pick the one which has the largest first value;if there's a tie, the largest second number, etc.
样例输入
3456
3546
3526
0001
100000101
0
样例输出
3,4,5,6
35,46
3,5,26
0001
100,000101
题意
一个数字串,让你分割,使得串严格递增,多解输出第一个数最大的,类推。
题解
好题,两次dp,类似于第一次确定一个值,第二次构造答案。
从前往后一个dp求出最后一个数字串最小时是多少,dp[i]=j表示str[0....i]这个串满足最后一个子串最小时最后一个串的下标起始str[j...i]。
接着从后往前第二个dp求出保证最后一个数字最小的情况下满足前面的串尽可能大,dp[i]=j表示str[i...len-1]的串是str[i....j]。这里需要注意对0的处理。
代码
#include<bits/stdc++.h>
using namespace std; char s[];
bool check(int l1,int r1,int l2,int r2,int f)
{
char s1[],s2[];
int p1=,p2=;
for(int i=r1;i>=l1;i--)s1[p1--]=s[i];
for(int i=r2;i>=l2;i--)s2[p2--]=s[i];
int k=abs(p1-p2);
if(p1<=p2)for(int i=;i<k;i++)s2[p2--]='';
else for(int i=;i<k;i++)s1[p1--]='';
/*printf("%d,%d %d,%d\n",l1,r1,l2,r2);
for(int i=p1+1;i<=80;i++)printf("%c",s1[i]);
puts("");
for(int i=p2+1;i<=80;i++)printf("%c",s2[i]);
puts("");*/
for(int i=p1+;i<=;i++)
if(s1[i]==s2[i])continue;
else if(s1[i]<s2[i])return true;
else return false;
return false;
}
int main()
{
while(scanf("%s",s)!=EOF)
{
if(strcmp(s,"")==)break;
int dp[]={},dp1[]={};
int len=strlen(s);
for(int i=;i<len;i++)
for(int j=i-;j>=;j--)
if(check(dp[j],j,j+,i,))
{
dp[i]=j+;
break;
}
int start=dp[len-];
dp1[start]=len-;
while(start->=&&s[start-]=='')
{
start--;
dp1[start]=len-;
}
for(int i=start-;i>=;i--)
for(int j=i;j<dp[len-];j++)
if(check(i,j,j+,dp1[j+],))
dp1[i]=j;
int p=;
do
{
int r=dp1[p];
for(int i=p;i<=r;i++)
putchar(s[i]);
p=r+;
if(p<len)putchar(',');
}while(p<len);
putchar();
}
return ;
}
TZOJ 5963 Increasing Sequences(线性DP)的更多相关文章
- Codeforces 446A. DZY Loves Sequences (线性DP)
<题目链接> 题目大意: 给定一个长度为$n$的序列,现在最多能够改变其中的一个数字,使其变成任意值.问你这个序列的最长严格上升子段的长度是多少. #include <bits/st ...
- 动态规划——线性dp
我们在解决一些线性区间上的最优化问题的时候,往往也能够利用到动态规划的思想,这种问题可以叫做线性dp.在这篇文章中,我们将讨论有关线性dp的一些问题. 在有关线性dp问题中,有着几个比较经典而基础的模 ...
- POJ 1239 Increasing Sequences 动态规划
题目链接: http://poj.org/problem?id=1239 Increasing Sequences Time Limit: 1000MSMemory Limit: 10000K 问题描 ...
- HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】
HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...
- 非常完整的线性DP及记忆化搜索讲义
基础概念 我们之前的课程当中接触了最基础的动态规划. 动态规划最重要的就是找到一个状态和状态转移方程. 除此之外,动态规划问题分析中还有一些重要性质,如:重叠子问题.最优子结构.无后效性等. 最优子结 ...
- Hills——一道转移方程很“有趣”的线性DP
题目描述 Welcome to Innopolis city. Throughout the whole year, Innopolis citizens suffer from everlastin ...
- 最长子序列(线性DP)学习笔记
子序列和子串不一样.子串要求必须连续,而子序列不需要连续. 比如说\(\{a_1,a_2\dots a_n\}\),他的子串就是\(\{a_i,a_{i+1},\dots, a_j|1\leq i\l ...
- LightOJ1044 Palindrome Partitioning(区间DP+线性DP)
问题问的是最少可以把一个字符串分成几段,使每段都是回文串. 一开始想直接区间DP,dp[i][j]表示子串[i,j]的答案,不过字符串长度1000,100W个状态,一个状态从多个状态转移来的,转移的时 ...
- Codeforces 176B (线性DP+字符串)
题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28214 题目大意:源串有如下变形:每次将串切为两半,位置颠倒形成 ...
随机推荐
- css中字体属性的简写
- iOS开发系列-NSURLSession
概述 NSURLSession是从iOS7开始出现的.NSURLSession比NSURLConnection简单很多并且避免了很多坑,因此目前公司项目大部分由NSURLConnection过度为NS ...
- 2019-5-29-Roslyn-让-VisualStudio-急速调试底层库方法
title author date CreateTime categories Roslyn 让 VisualStudio 急速调试底层库方法 lindexi 2019-5-29 20:2:9 +08 ...
- JS流程控制语句 二选一 (if...else语句) 语法: if(条件) { 条件成立时执行的代码} else {条件不成立时执行的代码}
二选一 (if...else语句) if...else语句是在指定的条件成立时执行代码,在条件不成立时执行else后的代码. 语法: if(条件) { 条件成立时执行的代码} else {条件不成立时 ...
- python相关软件安装流程图解————————python安装——————python-3.7.1-amd64
首先查看自己的系统版本 是32位的还是64位的 https://www.python.org/downloads/windows/ —————————python下载安装 开始———————————— ...
- Cesium官方教程11--建模人员必读
原文地址:https://cesium.com/blog/2014/12/15/gltf-tips-for-artists/ 这篇文章是Branden Coker, an artist from AG ...
- MyBatis与JPA的区别
参考博客: https://www.cnblogs.com/llywy/p/10103136.html https://www.jianshu.com/p/32ce87c163d6 MyBatis分为 ...
- nprogress 转
转载:http://www.xuanfengge.com/front-end-nprogress-and-lightweight-web-progress-bar-nanobar.html 前言 进度 ...
- jdom xpath定位带xmlns命名空间的节点(转)
jdom xpath定位带xmlns命名空间的节点 2013-06-29 0个评论 作者:baozhengw 收藏 我要投稿 关键词:jdom xpath xmlns 命名 ...
- hive新加入字段插入数据需要注意事项
hive中新加字段需要注意如下 1)如果表中有分区字段,必须先删除分区才能插入数据否则为null; 2)insert override TABLE table1 select counm1,counm ...