Increasing Sequences
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 3081 | Accepted: 1174 |
Description
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
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.
Output
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.
Sample Input
3456 3546 3526 0001 100000101 0
Sample Output
3,4,5,6 35,46 3,5,26 0001 100,000101
Source
[Submit] [Go Back] [Status] [Discuss]
求把一个数划分若干个数然后要满足构成数是递增的 在保证最后一个数最小的情况下第一个数最大
ACcode
#include <iostream>
#include <cstdio>
#include <cstring>
#define maxn 100
using namespace std;
int dp[maxn];
char str[maxn];
bool big(int i,int j,int m,int n){
while(str[i]=='0'&&i<=j)i++;
while(str[m]=='0'&&m<=n)m++;
if(i>j)return false;
if(m>n)return true;
int a=j-i+1,b=n-m+1;
if(a>b)return true;
if(a<b)return false;
for(int t=i,p=m;t<=j&&p<=n;++p,++t)
if(str[t]>str[p])return true;
else if(str[t]<str[p])return false;
return false;
}
int main(){
while(scanf("%s",str+1)!=EOF){
int len=strlen(str+1);
if(len==1&&str[1]=='0')break;
for(int i=1;i<=len;++i)dp[i]=i;
for(int i=2;i<=len;++i)
for(int j=i-1;j>0;--j)
if(big(j+1,i,j-dp[j]+1,j)){
dp[i]=i-j;
break;
}
int pos=len-dp[len]+1;
dp[pos]=dp[len];
for(int i=pos-1;i>0;--i){
if(str[i]=='0'){
dp[i]=dp[i+1]+1;
continue;
}
for(int j=pos;j>i;--j)
if(big(j,j+dp[j]-1,i,j-1)){
dp[i]=j-i;
break;
}
}
// for(int i=1;i<=len;++i)cout<<dp[i]<<' ';
//cout<<'\12';
for(int i=1;i<=dp[1];++i)putchar(str[i]);
pos=dp[1]+1;
while(pos<=len){
putchar(',');
for(int i=pos;i<dp[pos]+pos;++i)putchar(str[i]);
pos=pos+dp[pos];
}
putchar('\n');
}
return 0;
}