POJ 题目1239 ||ZOJ 题目 1499 Increasing Sequences(正反两次DP)

2023-05-16

Increasing Sequences
Time Limit: 1000MS Memory Limit: 10000K
Total 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


3456
3546
3526
0001
100000101
0  

Sample Output


3,4,5,6
35,46
3,5,26
0001
100,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;
	else
		if(len1>len2)
			return 1;
		else
			return 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");
	}
}


本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

POJ 题目1239 ||ZOJ 题目 1499 Increasing Sequences(正反两次DP) 的相关文章

  • POJ 题目1105 S-Trees(二叉树模拟)

    S Trees Time Limit 1000MS Memory Limit 10000KTotal Submissions 1499 Accepted 807 Description A Strange Tree S tree over
  • POJ-2453

    As we known data stored in the computers is in binary form The problem we discuss now is about the positive integers and
  • Week5 作业D - 滑动窗口[POJ - 2823]

    题目大意 输入 输出 基本思路 这个题的数据规模较大 xff0c k和n最大可以达到1e6 xff0c 因此如果我们暴力判断所有区间 窗口内元素的范围 中的最大值和最小值一定会超时 复杂度 O n 2
  • cf 1169 C Increasing by Modulo

    cf 1169 C Increasing by Modulo 题意 给你一个n个数字的序列 xff0c 有一个操作是选其中的一些数字来 43 1 xff0c 最后使得序列每一个数取模m后是一个非严格单调递增的序列 xff0c 问至少需要多少
  • POJ 3764--The xor-longest Path---DFS + Trie(最大异或值)

    POJ 3764 The xor longest Path Time Limit 2000MS Memory Limit 65536K Description In an edge weighted tree the xor length
  • POJ 2449 Remmarguts‘ Date---SPFA求评估函数 + A*最小堆BFS

    POJ 2449 Remmarguts Date Time Limit 4000MS Memory Limit 65536K Description Good man never makes girls wait or breaks an
  • 浙江省赛2015 _ J - Convert QWERTY to Dvorak -> ZOJ 3878

    模拟水题 题目 xff1a ZOJ 3878 Edward a poor copy typist is a user of the Dvorak Layout But now he has only a QWERTY Keyboard wi
  • ZOJ - 2313 Chinese Girls' Amusement

    You must have heard that the Chinese culture is quite different from that of Europe or Russia So some Chinese habits see
  • poj 2513 colored sticks

    代码 include lt iostream gt include lt stdio h gt using namespace std define MAX 27 define S 500003 struct Node int id Nod
  • POJ 1635 Subway tree systems

    题目 xff1a Some major cities have subway systems in the form of a tree i e between any pair of stations there is one and o
  • [JAVA][2013蓝桥杯预赛 JAVA本科B组][世纪末的星期]

    标题 世纪末的星期 曾有邪教称1999年12月31日是世界末日 当然该谣言已经不攻自破 还有人称今后的某个世纪末的12月31日 如果是星期一则会 有趣的是 任何一个世纪末的年份的12月31日都不可能是星期一 于是 谣言制造商 又修改为星期日
  • poj 1742 Coins

    Problem poj org problem id 1742 Reference www cppblog com flyinghearts archive 2010 09 01 125555 html blog csdn net wang
  • POJ--1328:Radar Installation (贪心)

    1 题目源地址 http poj org problem id 1328 2 解题思路 该题题意是为了求出能够覆盖所有岛屿的最小雷达数目 每个小岛对应x轴上的一个区间 在这个区间内的任何一个点放置雷达 则可以覆盖该小岛 区间范围的计算用 x
  • POJ 2479 Dual Core CPU|网络流|dinic模版

    问题描述 总时间限制 15000ms 单个测试点时间限制 5000ms 内存限制 65536kB 描述 As more and more computers are equipped with dual core CPU SetagLilb
  • POJ--1458:Common Subsequence (DP求最长公共子序列)

    1 题目源地址 http poj org problem id 1458 2 基本题意 给出两个序列 求出最长子序列的长度并输出 经典的动态规划求解 求最长公共子序列的经典DP解法代价为O mn 其中m和n分别为两个字符串的长度 具体实现如
  • POJ--1159:Palindrome (DP求最长公共子序列)

    1 题目源地址 http poj org problem id 1159 2 题目大意 题目就是给你一个字符串 问你添加最少几个字符之后字符串变成回文字符串 求给出的字符串和逆序的字符串的最长公共子序列 用总长度减去这个最长公共子序列的长度
  • poj 2155 Matrix

    Problem poj org problem id 2155 vjudge net contest 146952 problem A Meaning 一个 N N 的矩阵 A 初始时全部值为 0 有两种操作 1 C x1 y1 x2 y2
  • [GVIM] Increasing or decreasing numbers

    原文链接 https vim fandom com wiki Increasing or decreasing numbers In normal mode typing Ctrl A will increment the next num
  • 【扩展KMP】POJ_3450| HDU_2328 Corporate Identity

    原题直通车 POJ 3450 Corporate Identity HDU 2328 Corporate Identity 题意概述 找出N个串中最长公共子串 分析 一 可以直接枚举其中一个串的所有字串 跟所有串进行匹配找到结果 二 用其中
  • poj1463

    1

随机推荐