POJ 题目1239 ||ZOJ 题目 1499 Increasing Sequences(正反两次DP)
来源:互联网 发布:王小二果园淘宝店铺 编辑:程序博客网 时间:2024/05/19 04:06
Increasing Sequences
Time Limit: 1000MS Memory Limit: 10000KTotal Submissions: 3025 Accepted: 1147
Description
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
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.
Output
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.
Sample Input
34563546352600011000001010
Sample Output
3,4,5,635,463,5,260001100,000101
Source
East Central North America 2002
思路:http://blog.csdn.net/vecri/article/details/4758786
题目大意: 给定一个字符串, 如3456, 将其分割成多个整数,使该整数序列递增,且尽可能使最大的数也就是序列最后一个数最小,在这个前提下使序列前面的数最大分析:两次DP, 一次前向DP,一次后向DP第一次DP来确定最后一个数字,因为这个数字是递增序列的最后一个数,且这个数必须最小,代码中dp[i]=j是指由str[1...i]序列生成的递增序列的最后一个数是str[j...i]第二次DP是在确定最后一个数字的基础上,往前规划,使得前面的数尽可能的大,代码中dp2[i]=j是指在确定最后一个数的情况下,str[i...end]序列中最大的开头数为str[i...j]注意最后一个数前面可以加零。
ac代码
#include<stdio.h>#include<string.h>char str[110],s[110];int dp[110],dp2[110];int cmp(int b1,int e1,int b2,int e2){while(str[b1]=='0')b1++;while(str[b2]=='0')b2++;int len1=e1-b1+1;int len2=e2-b2+1;if(len1<len2)return -1;elseif(len1>len2)return 1;elsereturn strncmp(str+b1,str+b2,len1);}int main(){while(scanf("%s",str)!=EOF){if(strcmp(str,"0")==0)break;memset(dp,0,sizeof(dp));memset(dp2,0,sizeof(dp2));int len=strlen(str);int i,j;for(i=1;i<len;i++){for(j=i-1;j>=0;j--){if(cmp(dp[j],j,j+1,i)<0){dp[i]=j+1;break;}}}i=dp[len-1];dp2[i]=len-1;while(str[i-1]=='0'){dp2[i-1]=len-1;--i;}for(i=dp[len-1]-1;i>=0;i--){for(j=i;j<dp[len-1];j++){if(cmp(i,j,j+1,dp2[j+1])<0&& dp2[i]<j){dp2[i]=j;}}}i=0;while(i<len){strncpy(s,str+i,dp2[i]-i+1);s[dp2[i]-i+1]='\0';if(i!=0)printf(",");printf("%s",s);i=dp2[i]+1;}printf("\n");}}
0 0
- POJ 题目1239 ||ZOJ 题目 1499 Increasing Sequences(正反两次DP)
- 两次dp-poj-1239-Increasing Sequences
- POJ 1239-Increasing Sequences(LIS 分割成上升序列-两次DP)
- Increasing Sequences - POJ 1239 dp
- Increasing Sequences-ZOJ 1499 POJ 1039
- zoj 1499 Increasing Sequences
- poj 1239 dp(拆分成Increasing Sequences )
- poj 1239 Increasing Sequences dp lis
- poj 1239 Increasing Sequences
- POJ 1239 Increasing Sequences 笔记
- zoj poj 题目分类
- ZOJ POJ题目分类
- zoj、poj 题目分类
- poj,zoj题目分类
- POJ ZOJ题目分类
- ZOJ题目分类 POJ题目分类
- POJ DP题目列表
- poj dp分类题目:
- 开始怀念
- unity官方demo学习之Stealth(三)标签管理
- 谱聚类(NJW算法 Matlab代码)
- Java微信公众号开发--开发环境的搭建
- C/C++中typedef关键字总结
- POJ 题目1239 ||ZOJ 题目 1499 Increasing Sequences(正反两次DP)
- 数据结构【线性表(二)链表】项目之单链表:逆置
- unity官方demo学习之Stealth(四)屏幕渐变效果
- 关于Homebrew的一些错误总结
- 配置mysql5.5主从服务器
- php bind_param()函数
- VBA 第9课 合并单元格
- Spring MVC Cookie example
- 冒泡排序法