POJ 1239 Increasing Sequences 动态规划

题目链接:

http://poj.org/problem?id=1239

Increasing Sequences


Time Limit: 1000MSMemory Limit: 10000K
问题描述

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两遍,第一遍从前往后求最后一个数的最小值,第二遍从后往前,构造前面大的解。

代码

#include<map>
#include<set>
#include<cmath>
#include<queue>
#include<stack>
#include<ctime>
#include<vector>
#include<cstdio>
#include<string>
#include<bitset>
#include<cstdlib>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<functional>
using namespace std;
#define X first
#define Y second
#define mkp make_pair
#define lson (o<<1)
#define rson ((o<<1)|1)
#define mid (l+(r-l)/2)
#define sz() size()
#define pb(v) push_back(v)
#define all(o) (o).begin(),(o).end()
#define clr(a,v) memset(a,v,sizeof(a))
#define bug(a) cout<<#a<<" = "<<a<<endl
#define rep(i,a,b) for(int i=a;i<(b);i++)
#define scf scanf
#define prf printf

typedef long long LL;
typedef vector<int> VI;
typedef pair<int,int> PII;
typedef vector<pair<int,int> > VPII;

const int INF=0x3f3f3f3f;
const LL INFL=0x3f3f3f3f3f3f3f3fLL;
const double eps=1e-8;
const double PI = acos(-1.0);

//start----------------------------------------------------------------------

const int maxn=88;

char str[maxn];
///dp:从前扫,dp2从后扫
int dp[maxn],dp2[maxn];
///val保存切出来的字符串(不包含前导0),val2包含前导零。
string val[maxn],val2[maxn];
int n;
vector<string> ans;
void print(int i) {
    if(i==n+1) return;
    ans.pb(val2[i]);
    print(dp2[i]);
}

int main() {
    while(scanf("%s",str+1)==1) {
        ans.clear();
        if(strlen(str+1)==1&&str[1]=='0') break;
        n=strlen(str+1);

        ///val[0]不能为“0”!!
        ///从前扫
        dp[0]=0,val[0]="";
        for(int i=1; i<=n; i++) {
            for(int j=i; j>=1; j--) {
                string tmp,tmp2;
                for(int k=j; k<=i; k++) {
                    tmp2+=str[k];
                    if(tmp.length()==0&&str[k]=='0') continue;
                    tmp+=str[k];
                }
                if(tmp.length()==0) tmp+="0";
                if(tmp.length()>val[j-1].length()||tmp.length()==val[j-1].length()&&tmp>val[j-1]) {
                    val[i]=tmp;
                    val2[i]=tmp2;
                    dp[i]=j-1;
                    break;
                }
            }
        }

        ///从后扫
        clr(dp2,-1);
        int st=dp[n]+1;
        dp2[st]=n+1,val[st]=val[n],val2[st]=val2[n];
        for(int i=st-1; i>=1; i--) {
            if(str[i]=='0'){
                ///前导零特殊处理
                dp2[i]=dp2[i+1];
                val[i]=val[i+1];
                val2[i]="0"+val2[i+1];
                continue;
            }
            for(int j=st-1; j>=i; j--) {
                if(dp2[j+1]<0) continue;
                string tmp,tmp2;
                for(int k=i; k<=j; k++) {
                    tmp2+=str[k];
                    if(tmp.length()==0&&str[k]=='0') continue;
                    tmp+=str[k];
                }
                if(tmp.length()==0) tmp+="0";
                if(tmp.length()<val[j+1].length()||tmp.length()==val[j+1].length()&&tmp<val[j+1]) {
                    val[i]=tmp;
                    val2[i]=tmp2;
                    dp2[i]=j+1;
                    break;
                }
            }
        }
        print(1);
        rep(i,0,ans.sz()){
            prf("%s",ans[i].c_str());
            if(i==ans.sz()-1) prf("\n");
            else prf(",");
        }
    }
    return 0;
}

转载于:https://www.cnblogs.com/fenice/p/5945228.html

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值