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" 是在下图所示的场景上完成的游戏. 场景中包括多个长度和高度 ...
随机推荐
- Eclipse开发工具常用快捷键的使用技巧
Eclipse作为软件开发的常用工具,被很多的人所欢迎,尤其是丰富的快捷键,可以极大的提高编码的效率,下面将常用的快捷键做了整理,便于大家学习和使用. Eclipse常用快捷键 1代码提示 Alt ...
- jQuery----获取兄弟元素的方法
① $(this).next(): 获取的是当前元素的下一个兄弟元素 ②$(this).nextAll(); 获取的是当前元素的后面的所有的兄弟元素 ③$(this).pre ...
- 单片机、CPU、指令集和操作系统的关系
郑重声明:转载自http://blog.csdn.net/zhongjin616/article/details/18765301 1> 首先讨论各种单片机与操作系统的关系 说到单片机,大家第一 ...
- 一个C语言内存管理模块的实现
C 内存管理模块的编写 C语言手动管理内存很困难,有时候很难发现内存泄漏,这两天看了一下里面有写了一个简单的内存管理模块,发现挺精巧,可以有效检测内存泄漏 原理很简单,就是把C的malloc函数分配的 ...
- scala_类的继承
Scala继承一个基类跟Java很相似, 但我们需要注意以下几点: 重写一个非抽象方法必须使用override修饰符,以及重写父类属性也必须使用override修饰符. 只有主构造函数才可以往基类的构 ...
- PosgreSQL 9.0 High Performance中文版瑕疵
磨砺技术珠矶,践行数据之道,追求卓越价值 回到上一级页面: PostgreSQL杂记页 回到顶级页面:PostgreSQL索引页 发表此文不是为了吐槽,而是为了防止更多的受害者出现啊,拿到书后 ...
- 使用 lxml 中的 xpath 高效提取文本与标签属性值
以下代码在 python 3.5 + jupyter notebook 中运行测试无误! # 我们爬取网页的目的,无非是先定位到DOM树的节点,然后取其文本或属性值 myPage = '''<h ...
- 4542: [Hnoi2016]大数
4542: [Hnoi2016]大数 链接 分析: 如果p等于2或者5,可以根据最后一位直接知道是不是p的倍数,所以直接记录一个前缀和即可. 如果p不是2或者5,那么一个区间是p的倍数,当且仅当$\f ...
- [NOI2016]区间 线段树
[NOI2016]区间 LG传送门 考虑到这题的代价是最长边减最短边,可以先把边按长度排个序,双指针维护一个尺取的过程,如果存在包含某个点的区间数\(\ge m\),就更新答案并把左指针右移,这样做的 ...
- ModelForm解密
一.复用model表和字段 models.py文件 class User(models.Model): username = models.CharField(max_length=32) emai ...