描述

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 - JUnit整合测试

    1.导包:test.jar - (依赖 aop.jar) 2.使用@RunWith注解创建spring容器 - @RunWith(SpringJUnit4ClassRunner.class) 3.使用 ...

  2. 01_springboot2.x之springboot入门

    1.简介 Spring Boot来简化Spring应用开发,约定大于配置, 去繁从简,just run就能创建一个独立的,产品级别的应用. 优点: 1.简化Spring应用开发的一个框架: 2.整个S ...

  3. HYNB Round 15: PKU Campus 2019

    HYNB Round 15: PKU Campus 2019 C. Parade 题意 将平面上n*2个点安排在长度为n的两行上. 做法 首先可以忽略每个点之间的影响,只用考虑匹配即可 然后把所以点归 ...

  4. wpf 让正执行的程序暂停几秒钟

    public static class DispatcherHelper     {         [SecurityPermissionAttribute(SecurityAction.Deman ...

  5. iOS开发系列-NSOutputStream

    NSOutputStream 创建一个NSOutputStream实例 - (nullable instancetype)initToFileAtPath:(NSString *)path appen ...

  6. 2019-8-31-dotnet-获取程序所在路径的方法

    title author date CreateTime categories dotnet 获取程序所在路径的方法 lindexi 2019-08-31 16:55:58 +0800 2019-03 ...

  7. 【BZOJ3944】Sum

    题面 Description Input 一共T+1行 第1行为数据组数T(T<=10) 第2~T+1行每行一个非负整数N,代表一组询问 Output 一共T行,每行两个用空格分隔的数ans1, ...

  8. Flink Checkpoint 问题排查实用指南

    在 Flink 中,状态可靠性保证由 Checkpoint 支持,当作业出现 failover 的情况下,Flink 会从最近成功的 Checkpoint 恢复.在实际情况中,我们可能会遇到 Chec ...

  9. poj3294Life Forms

    传送门 我真是一个垃圾 模板题都不会做 模板题还要别人教 细节写法还要别人教 别人一分钟AC,教我算法还教我写法,最后写出来的别人算法还比我优秀一百倍 数据结构把脑子学傻了,看到题就想怎么用数据结构, ...

  10. sql自定义日期函数,返回范围内日期和星期数表。

    Create function [dbo].[FUN_GenerateTime] ( @begin_date datetime, -- 起始时间 @end_date datetime -- 结束时间 ...