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;
}
POJ 1239 Increasing Sequences 动态规划的更多相关文章
- POJ 1239 Increasing Sequences(经典的两次dp)
http://poj.org/problem?id=1239 题意:给出一串序列,现在要添加逗号作为分隔符,使得序列是递增序列,然后让最后一个数尽量小,第一个数尽量大. 思路:先从头到尾进行一次dp, ...
- POJ 1239 Increasing Sequences [DP]
题意:略. 思路:进行两次dp. 第一次dp从前向后,用dp[x]表示从第x位向前dp[x]位可构成一个数字,且与前面的数组符合题意要求.最后求的dp[n]即为最后一个数字的长度. 而题目还有要求,所 ...
- POJ 2127 Greatest Common Increasing Subsequence -- 动态规划
题目地址:http://poj.org/problem?id=2127 Description You are given two sequences of integer numbers. Writ ...
- HDOJ 1423 Greatest Common Increasing Subsequence -- 动态规划
题目地址:http://acm.hdu.edu.cn/showproblem.php?pid=1423 Problem Description This is a problem from ZOJ 2 ...
- poj 1776 Task Sequences
http://poj.org/problem?id=1776 题意: 有一个机器要完成N个作业, 给你一个N*N的矩阵, M[i][j]=1,表示完成第i个作业后不用重启机器,继续去完成第j个作业 M ...
- 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 ...
- TZOJ 5963 Increasing Sequences(线性DP)
描述 Given a string of digits, insert commas to create a sequence of strictly increasing numbers so as ...
- [POJ 3211] Washing Clothes (动态规划)
题目链接:http://poj.org/problem?id=3211 题意:有M件衣服,每种衣服有一种颜色,一共有N种颜色.现在两个人洗衣服,规则是必须把这一种颜色的衣服全部洗完才能去洗下一种颜色的 ...
- POJ 1661 Help Jimmy -- 动态规划
题目地址:http://poj.org/problem?id=1661 Description "Help Jimmy" 是在下图所示的场景上完成的游戏. 场景中包括多个长度和高度 ...
随机推荐
- 2018-11-26 BIG DATA ANALYSIS
- Qt常用控件
Qt常用控件 QWidget与QFrame QWidget所有图形控件的基类 QFrame与QWidget的区别 QFrame是基本控件的基类, QWidget是QFrame的基类. 因此QFrame ...
- rails框架配置
rails框架默认有三个模式development(开发),production(上线),test(测试) Development config.cache_classes = false 每次请求都 ...
- Golint的简易使用方法
根据作者的说法: Golint is a linter for Go source code. Golint differs from gofmt. Gofmt reformats Go source ...
- iOS 开发之UIStackView的应用
————————————————UIStackView的应用———————————————— 一:先讲下优势: 对于排布列表式控件的布局需求,用UIStackView控件,开发中为我们省去了繁琐的代码 ...
- 利用IPC通道进行进程间通信(C#)
有一个解决方案,其中包括一个Windows服务和一个Windows应用程序,两者之间需要进行通信.查了下,可以使用多种方法,如Web service(适用于不同系统及跨平台情况)..NET Remot ...
- 2017-2018-1 20155330 《信息安全系统设计基础》加分项目--实现mypwd
2017-2018-1 20155330 <信息安全系统设计基础>加分项目--实现mypwd pwd命令 命令功能:查看"当前工作目录"的完整路径. 通过man命令查看 ...
- 洛谷 P3795 钟氏映射
洛谷 P3795 钟氏映射 题目背景 2233年,CSSYZ学校的数学老师兼数学竞赛顾问钟JG已经2200+岁啦! 为了庆生,他或她给广大人民群众出了道题. 题目描述 设集合N=M={x∣x∈N+, ...
- 【LG3247】[HNOI2016]最小公倍数
[LG3247][HNOI2016]最小公倍数 题面 洛谷 题解 50pts 因为拼凑起来的部分分比较多,所以就放一起了. 以下设询问的\(a,b\)为\(A,B\), 复杂度\(O(nm)\)的:将 ...
- HDU - 3874 Necklace (树状数组、离线处理)
题目链接:Necklace 题意: 给出一串珠子,每个珠子有它的value,现在给出n(n<=5e4)个珠子的value(1<=value<=1e6),现在给出m(1<=m&l ...