题目链接:

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. PHP代码优化—array_push

    PHP中数组插入数据通常有这么几种: 定义的时候直接赋值 $arr = array('apple', 'banana'); 使用数组变量操作 $arr = array(); $arr[] = 'app ...

  2. Android ViewPager里的所有图片设置监听打开同一活动显示不同图片

    Android ViewPager里的所有图片设置监听请看前一文章 为了省时所以2层菜单只做一个点击任意图片后显示相应图片的活动 关键点是每个点击对应的图片如何传参给显示的活动 因为只启动一个活动,所 ...

  3. Python的scrapy之爬取6毛小说网的圣墟

    闲来无事想看个小说,打算下载到电脑上看,找了半天,没找到可以下载的网站,于是就想自己爬取一下小说内容并保存到本地 圣墟 第一章 沙漠中的彼岸花 - 辰东 - 6毛小说网  http://www.6ma ...

  4. inux下进程的最大线程数、进程最大数、进程打开的文件数

    inux下进程的最大线程数.进程最大数.进程打开的文件数 2008-12-07 23:48 =========================    如下转载自这里. linux 系统中单个进程的最大 ...

  5. 20155236范晨歌_MSF基础应用

    20155236范晨歌_MSF基础应用 20155236范晨歌_MSF基础应用 目录 概述 MS08-067漏洞攻击 MS11-050漏洞攻击 MS10-087漏洞攻击 辅助模块 概述 MSF的六种模 ...

  6. Python码农福音,GitHub增加Python语言安全漏洞告警

    在 2017 年 GitHub 开始对托管在其网站的代码仓库和依赖库开始提供安全漏洞检查和告警,开始时候只支持 Ruby 和 JavaScript 语言的项目.根据 GitHub 官方数据显示截止目前 ...

  7. html5 初试 indexedDB

    indexedDB是存储大量结构化数据的API,demo中用到的是异步API,麻烦的就是所有对indexedDB的操作都会发生一个异步的‘请求’,只要熟悉了API操作起来也很简单. http://ww ...

  8. JavaScript验证时间格式

    1. 短时间,形如 (13:04:06) function isTime(str) { var a = str.match(/^(\d{1,2})(:)?(\d{1,2})\2(\d{1,2})$/) ...

  9. Jmeter(二十一)_完整Demo

    1:创建一个线程组   2:添加一个cookie管理器     3:设置你的信息头管理器:application/json;text/plain;charset=UTF-8   44 4:添加一个用户 ...

  10. Java编辑PPT的柱状图,与内嵌的Excel联动

    /** * 条形图:柱形图 的数据写入方法 * @param slide 图表 * @param index 柱状图的下标 * @param data 要填充的数据 * @param titles 内 ...