描述

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. <scrapy爬虫>scrapy命令行操作

    1.mysql数据库 2.mongoDB数据库 3.redis数据库 1.创建项目 scrapy startproject myproject cd myproject 2.创建爬虫 scrapy g ...

  2. 【LGP4714】「数学」约数个数和

    题目 众所周知,除数个数函数\(\sigma_0=I^2\),\(I\)就是狄利克雷卷积里的\(1\)函数 于是熟悉狄利克雷卷积的话很快就能看出我们要求的就是\(I\times I^{k}\),即\( ...

  3. StringUtils工具

    ppublic class StringUtils { private StringUtils() { } /** * 文本左边补零 * * @param maxLength 文本长度 * @para ...

  4. printk函数打开和关闭消息

    在驱动开发的早期, printk 非常有助于调试和测试新代码. 当你正式发行驱动时, 换句 话说, 你应当去掉, 或者至少关闭, 这些打印语句. 不幸的是, 你很可能会发现, 就在你 认为你不再需要这 ...

  5. C++萃取技术的一个简单初探

    首先本文并不是讲解C++萃取技术,关于C++的萃取技术网上有很多文章,推荐http://www.cppblog.com/woaidongmao/archive/2008/11/09/66387.htm ...

  6. 从微服务治理的角度看RSocket、. Envoy和. Istio

    很多同学看到这个题目,一定会提这样的问题:RSocket是个协议,Envoy是一个 proxy,Istio是service mesh control plane + data plane. 这三种技术 ...

  7. springboot与任务(定时任务)

    描述: 项目开发中经常需要执行一些定时任务,比如需要在每天凌晨时候,分析一次前一天的日志信息.Spring为我们提供了异步执行任务调度的方式,提供TaskExecutor .TaskScheduler ...

  8. php链表笔记:单链表反转

    <?php /** * Created by PhpStorm. * User: huizhou * Date: 2018/12/1 * Time: 11:41 */ /** * 1.链表的反转 ...

  9. windows环境下,svn未备份情况下重新恢复

    公司有个同事在未打招呼的情况下把公司服务器进行重新装系统,崩溃啊.SVN之前未备份,还好SVN的库(Repositories)还在,如下图: 恢复办法如下: 由于之前安装的就是VisualSVN-Se ...

  10. android 头像选择以及裁剪

    一.布局申明 <ImageView android:id="@+id/head_image" android:layout_width="80dp" an ...