描述

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)的更多相关文章

  1. Codeforces 446A. DZY Loves Sequences (线性DP)

    <题目链接> 题目大意: 给定一个长度为$n$的序列,现在最多能够改变其中的一个数字,使其变成任意值.问你这个序列的最长严格上升子段的长度是多少. #include <bits/st ...

  2. 动态规划——线性dp

    我们在解决一些线性区间上的最优化问题的时候,往往也能够利用到动态规划的思想,这种问题可以叫做线性dp.在这篇文章中,我们将讨论有关线性dp的一些问题. 在有关线性dp问题中,有着几个比较经典而基础的模 ...

  3. POJ 1239 Increasing Sequences 动态规划

    题目链接: http://poj.org/problem?id=1239 Increasing Sequences Time Limit: 1000MSMemory Limit: 10000K 问题描 ...

  4. HDOJ 1423 Greatest Common Increasing Subsequence 【DP】【最长公共上升子序列】

    HDOJ 1423 Greatest Common Increasing Subsequence [DP][最长公共上升子序列] Time Limit: 2000/1000 MS (Java/Othe ...

  5. 非常完整的线性DP及记忆化搜索讲义

    基础概念 我们之前的课程当中接触了最基础的动态规划. 动态规划最重要的就是找到一个状态和状态转移方程. 除此之外,动态规划问题分析中还有一些重要性质,如:重叠子问题.最优子结构.无后效性等. 最优子结 ...

  6. Hills——一道转移方程很“有趣”的线性DP

    题目描述 Welcome to Innopolis city. Throughout the whole year, Innopolis citizens suffer from everlastin ...

  7. 最长子序列(线性DP)学习笔记

    子序列和子串不一样.子串要求必须连续,而子序列不需要连续. 比如说\(\{a_1,a_2\dots a_n\}\),他的子串就是\(\{a_i,a_{i+1},\dots, a_j|1\leq i\l ...

  8. LightOJ1044 Palindrome Partitioning(区间DP+线性DP)

    问题问的是最少可以把一个字符串分成几段,使每段都是回文串. 一开始想直接区间DP,dp[i][j]表示子串[i,j]的答案,不过字符串长度1000,100W个状态,一个状态从多个状态转移来的,转移的时 ...

  9. Codeforces 176B (线性DP+字符串)

    题目链接: http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=28214 题目大意:源串有如下变形:每次将串切为两半,位置颠倒形成 ...

随机推荐

  1. Spring Boot 发布 jar 包转为 war 包秘籍。

    Spring Boot是支持发布jar包和war的,但它推荐的是使用jar形式发布.使用jar包比较方便,但如果是频繁修改更新的项目,需要打补丁包,那这么大的jar包上传都是问题.所以,jar包不一定 ...

  2. appium + python 自动化调试手机时 UiAutomator exited unexpectedly with code 0, signal null

    放上appium报错图,appium在手机里安装了appium setting 和unlock 软件,输入法也被变成了appium input ,但是就是点不到目标软件,手机也可以被cmd  adb ...

  3. Office2016只安装三件套方法

    转载 Office2016只安装三件套方法(word,ppt,excel) 2019-03-01 23:30:03 Kellen5l 阅读数 11618更多 分类专栏: Office   版权声明:本 ...

  4. shell脚本练习01

    ######################################################################### # File Name: 4.5.sh # Auth ...

  5. netty 使用Java序列化

    SubscribeReq package com.zhaowb.netty.ch7_1; import java.io.Serializable; public class SubscribeReq ...

  6. ajax长轮询 (转)

    javaWeb项目中需要一个实时提醒的功能,也就是某人做了某一操作,可以立即提醒到当前在线的用户 最开始想在用户做了操作后,储存一个状态到数据库中然后用每隔几秒用ajax去请求后台查询数据库来确定是否 ...

  7. uboot 的移植过程

    1. 工作用户 uboot 2. u­boot 版本 1.1.4 3. 工具链 2.95.3 步骤 我们为开发板取名叫: crane2410, 并在 u­boot 中建立自己的开发板类型 修改 Mak ...

  8. 对this的理解与总结

    this既不指向函数自身,也不指向函数的词法作用域!它指向谁完全取决于它在哪里被调用,被谁调用! 绑定规则 总体来说,this的绑定规则有: 默认绑定(严格模式/非严格模式) 隐式绑定 显式绑定 ne ...

  9. 读《深入PHP 面向对象、模式与实践》笔记

    1. include() 和require() 语句的不同在于它们如何处理错误.使用require()调用文件发生错误时,将会停止整个程序;调用include()时遇到相同的错误,则会生成警告并停止执 ...

  10. 迭代器/生成器/装饰器 /Json & pickle 数据序列化

    本节内容 迭代器&生成器 装饰器 Json & pickle 数据序列化 软件目录结构规范 作业:ATM项目开发 1.列表生成式,迭代器&生成器 列表生成式 孩子,我现在有个需 ...