poj 1239 Increasing Sequences dp lis

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;
}




  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值