题目链接:

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

Increasing Sequences

Time Limit: 1000MS
Memory 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;
}

POJ 1239 Increasing Sequences 动态规划的更多相关文章

  1. POJ 1239 Increasing Sequences(经典的两次dp)

    http://poj.org/problem?id=1239 题意:给出一串序列,现在要添加逗号作为分隔符,使得序列是递增序列,然后让最后一个数尽量小,第一个数尽量大. 思路:先从头到尾进行一次dp, ...

  2. POJ 1239 Increasing Sequences [DP]

    题意:略. 思路:进行两次dp. 第一次dp从前向后,用dp[x]表示从第x位向前dp[x]位可构成一个数字,且与前面的数组符合题意要求.最后求的dp[n]即为最后一个数字的长度. 而题目还有要求,所 ...

  3. POJ 2127 Greatest Common Increasing Subsequence -- 动态规划

    题目地址:http://poj.org/problem?id=2127 Description You are given two sequences of integer numbers. Writ ...

  4. HDOJ 1423 Greatest Common Increasing Subsequence -- 动态规划

    题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1423 Problem Description This is a problem from ZOJ 2 ...

  5. poj 1776 Task Sequences

    http://poj.org/problem?id=1776 题意: 有一个机器要完成N个作业, 给你一个N*N的矩阵, M[i][j]=1,表示完成第i个作业后不用重启机器,继续去完成第j个作业 M ...

  6. Codeforces Round #FF (Div. 1) A. DZY Loves Sequences 动态规划

    A. DZY Loves Sequences 题目连接: http://www.codeforces.com/contest/446/problem/A Description DZY has a s ...

  7. TZOJ 5963 Increasing Sequences(线性DP)

    描述 Given a string of digits, insert commas to create a sequence of strictly increasing numbers so as ...

  8. [POJ 3211] Washing Clothes (动态规划)

    题目链接:http://poj.org/problem?id=3211 题意:有M件衣服,每种衣服有一种颜色,一共有N种颜色.现在两个人洗衣服,规则是必须把这一种颜色的衣服全部洗完才能去洗下一种颜色的 ...

  9. POJ 1661 Help Jimmy -- 动态规划

    题目地址:http://poj.org/problem?id=1661 Description "Help Jimmy" 是在下图所示的场景上完成的游戏. 场景中包括多个长度和高度 ...

随机推荐

  1. mac 设置mysql开机自启动

    1.编辑一个mysql启动文件. 在终端里面输入: sudo vi /Library/LaunchDaemons/com.mysql.mysql.plist 2.输入启动文件内容: <?xml ...

  2. 分子量 (Molar Mass,ACM/ICPC Seoul 2005,UVa1586)

    习题 3-3 分子量 (Molar Mass,ACM/ICPC Seoul 2005,UVa1586) 给出一种物质的分子式(不带括号),求分子量.本题中的分子式只包含4种原子,分别为C,H,O,N, ...

  3. Truck History(prime)

    Truck History Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 31871   Accepted: 12427 D ...

  4. lua协程实现

    协程是个很好的东西,它能做的事情与线程相似,区别在于:协程是使用者可控的,有API给使用者来暂停和继续执行,而线程由操作系统内核控制:另外,协程也更加轻量级.这样,在遇到某些可能阻塞的操作时,可以使用 ...

  5. 关于manacher

    由一个题引入: 求一个串A的最长回文串: A=abababa最长回文串长度:5(ababa) 先思考用hash怎么做? 一.暴力 枚举左端,右端点(确定一个区间),线性扫一遍当前区间. Ans=max ...

  6. PLSQL Developer 客户端没有TNS监听,无法连接数据库

    在Windows Server 2008 中安装了 64位的Oracle,好不容易将监听做好,在使用客户端 PLSQL Developer 的时候发现竟然没有TNS监听. 问题如下: 如上图所示,打开 ...

  7. 【ASP.NET Core】运行原理(1):创建WebHost

    本系列将分析ASP.NET Core运行原理 [ASP.NET Core]运行原理[1]:创建WebHost [ASP.NET Core]运行原理[2]:启动WebHost [ASP.NET Core ...

  8. UWP Xaml设计器中输入特殊字符

    <TextBox Text="欢迎使用小冰科技最新研发的自然语言处理程序.小冰科技旗下还有强大的人脸识别软件——<微识别>,自动追踪和识别人脸:具有科普性质的.清新脱俗的识 ...

  9. 设置pdsh的默认登录模式

    1.check your pdsh default rcmd rsh pdsh -q -w localhostSee what your pdsh default rcmd is. 2.Modify ...

  10. Rxjava - 操作符,线程操作的简单使用

    目录 创建操作符 10种常用的操作符定义 下面做几个操作符的demo演示 create from repeat defer interval Scheduler 什么是Scheduler? 如何使用S ...