描述

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. MyBatis的核心API

    MyBatis核心Api 上次简单的写了一个MyBatis的简介以及编写了一个MyBatis的入门程序,但是在入门程序中出现多很多比较陌生的词,比如SqlSessionFactoryBuilder.S ...

  2. python基础语法(运算符及优先级)

    python基础语法(运算符及优先级) python语言支持的运算符类型 算数运算符 假设变量a为10,变量b为21 算数符 描述 实例 + 加-两个对象相加 a+b结果31 - 减-得到一个负数或者 ...

  3. 多项式模板&题目整理

    注:多项式的题目,数组应开:N的最近2的整数次幂的4倍. 多项式乘法 FFT模板 时间复杂度\(O(n\log n)\). 模板: void FFT(Z *a,int x,int K){ static ...

  4. Atcoder arc085

    C:HSI 期望模型,不想说. #include<cstdio> using namespace std; typedef long long ll; int main() { int n ...

  5. linux yum 安装 卸载

    安装一个软件时 yum -y install httpd 安装多个相类似的软件时 yum -y install httpd* 安装多个非类似软件时 yum -y install httpd php p ...

  6. php中date() 函数

    实例 格式化本地日期和时间,并返回格式化的日期字符串: <?php // Prints the day echo date("l") . "<br>&q ...

  7. safari跨域cookie的问题

    最近做了一个项目,是将自己公司的H5页面嵌入到其他公司的pc和移动端,采用的方案是iframe,跨域数据传输用的postMessage,最后在联调过程中发现iPhone的微信中无法打开,在 Setti ...

  8. vuex mutation,action理解

    1. 在store中分别注册mutation和action,action中用commit同步调用mutation来执行修改state,但是在组件中则使用dispatch异步调用action 2. 通俗 ...

  9. phpCOW机制详解

    写时复制(Copy-on-Write,也缩写为COW),顾名思义,就是在写入时才真正复制一份内存进行修改. COW最早应用在*nix系统中对线程与内存使用的优化,后面广泛的被使用在各种编程语言中,如C ...

  10. VS2010-MFC(MFC消息映射机制概述)

    转自:http://www.jizhuomi.com/software/147.html 前面已经说过,Windows应用程序是消息驱动的.在MFC软件开发中,界面操作或者线程之间通信都会经常用到消息 ...