1239 Increasing Sequences

原创 2007年10月15日 08:28:00
Increasing Sequences
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 482   Accepted: 162

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

 

Sample Output

 

Source
East Central North America 2002

 

**************************************************************************************

************************************************************************************9**

Source Code
#include <stdio.h>
#include <string.h>
#define N_MAX 81
char seqStr[N_MAX];
char d[N_MAX][N_MAX];
char res[N_MAX],resTemp[N_MAX];
int num;
bool Great(int start1,int len1,int start2,int len2)
{
 while(len1 > 0 && seqStr[start1] == '0')
 {
  len1--;
  start1++;
 }
 while(len2 > 0 && seqStr[start2] == '0')
 {
  len2--;
  start2++;
 }
 if(len1 > len2)
  return true;
 else if(len1 == len2)
 {
  while(len1 > 0)
  {
   if(seqStr[start1] > seqStr[start2])
    return true;
   else if(seqStr[start1] < seqStr[start2])
    return false;
   else
   {
    start1++;
    start2++;
    len1--;
   }
  }
  return false;
 }
 else
  return false;
}
void GetResult(int idx,int len,int numTemp)
{
 int i,j;
 if(idx == len)
 {
  if(num == 0)
  {
   resTemp[numTemp] = len;
   num = numTemp + 1;
   memcpy(res,resTemp,num);
  }
  else
  {
   bool best = false;
   resTemp[numTemp] = len;
   for(i = numTemp,j = num - 1 ; i >= 0 && j >= 0 ; i--,j--)
   {
    if(resTemp[i] > res[j])
     best = true;
    if(resTemp[i] == res[j])
     continue;
    break;
   }
   if(best == true)
   {
    num = numTemp + 1;
    memcpy(res,resTemp,num);
   }
  }
 }
 else
 {
  resTemp[numTemp] = len;
  for(i = 1 ; i <= d[idx][len] ; i++)
  {
   if(d[idx-len][i] == 0)
    continue;
   GetResult(idx-len,i,numTemp+1);
  }
 }
}
void DP()
{
 int i,j,t,prev;
 bool bGreat;
 int len = strlen(seqStr + 1) + 1;
 d[1][1] = 1;
 for(i = 2 ; i < len ; i++)
 {
  d[i][i] = 1;
  for(j = 1 ; j < i ; j++)
  {
   prev = i - j;
   t = j;
   while(t < prev && seqStr[prev - t] == '0')
    t++;
   for( ; t > 0 ; t--)
   {
    if(d[prev][t] == 0)
     continue;
    
    if(Great(i - j + 1,j,prev - t + 1,t))
    {
     bGreat = true;
     break;
    }
   }
   if(bGreat == true)
    d[i][j] = t;
   else
    d[i][j] = 0;
  }
 }
 prev = len - 1;
 for(i = 1 ; i < len ; i++)
 {
  if(d[prev][i] > 0)
   break;
 }
 j = i;
 while(j < len && seqStr[prev - j] == '0')
  j++;
 num = 0;
 for( ; i <= j ; i++)
  GetResult(prev,i,0);
 prev = 1;
 for(i = num - 1 ; i > 0 ; i--)
 {
  j = prev + res[i];
  for( ; prev < j ; prev++)
   printf("%c",seqStr[prev]);
  printf(",");
 }
 j = prev + res[i];
 for( ; prev < j ; prev++)
  printf("%c",seqStr[prev]);
 printf("/n");
}
int main() 
{
 //freopen("test.txt","r",stdin);
 while(scanf("%s",seqStr + 1) != EOF)
 {
  if(strlen(seqStr + 1) == 1 && seqStr[1] == '0')
   break;
  DP();
 }
    return 0;
}
3,4,5,6
35,46
3,5,26
0001
100,000101
3456
3546
3526
0001
100000101
0

POJ 1239-Increasing Sequences(LIS 分割成上升序列-两次DP)

Increasing Sequences Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: ...
  • MIKASA3
  • MIKASA3
  • 2016-05-21 13:10:25
  • 803

poj1239(两次Dp)

两次dp求解,题目要求在整个是递增子序列的前提下最后一个元素的值要最小,并且在此前提下第一个的值要最大,不看题解根本想不到怎么做。 做法:定义两个dp数组dp_min dp_max  第一个记录某个i...
  • My_ACM_Dream
  • My_ACM_Dream
  • 2014-11-28 17:27:41
  • 837

poj 1239 Increasing Sequences dp lis

Increasing Sequences Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 30...
  • zp___waj
  • zp___waj
  • 2016-04-16 08:47:49
  • 492

leetcode801. Minimum Swaps To Make Sequences Increasing(python)

leetcode801. Minimum Swaps To Make Sequences Increasing(python) 原题地址:https://leetcode.com/problem...
  • birdreamer
  • birdreamer
  • 2018-03-18 14:31:18
  • 117

POJ 1239 Increasing Sequences

POJ 1239 Increasing Sequences
  • Rising_shit
  • Rising_shit
  • 2018-01-07 18:05:04
  • 82

801. Minimum Swaps To Make Sequences Increasing

We have two integer sequences A and B of the same non-zero length.We are allowed to swap elements A[...
  • zjucor
  • zjucor
  • 2018-03-18 11:56:10
  • 237

poj 1239 Increasing Sequences

题目链接:http://poj.org/problem?id=1239 题目大意:求使最后一个数最大且前面的数尽量大的加逗号方式,两次dp,先从前到后求得最后一个数,然后反向dp,求出满足条件且当前...
  • Wings_of_Liberty
  • Wings_of_Liberty
  • 2012-03-25 21:49:58
  • 666

leetcode801 Minimum Swaps To Make Sequences Increasing (动态规划)

leetcode801 Minimum Swaps To Make Sequences Increasing (动态规划) Description We have two integer sequ...
  • Exupery_
  • Exupery_
  • 2018-03-19 15:59:44
  • 105

Increasing Sequences - POJ 1239 dp

Increasing Sequences Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 29...
  • u014733623
  • u014733623
  • 2014-08-25 16:51:40
  • 498

zoj 1499 Increasing Sequences

都说经典dp,但是居然没有反应过来。dp方式和最长递增子串这种问题类似:从首位开始长度为n的子串,假设可以获得的最小的最后位数字为f(n)。那么f(n)可以通过比n小的子串得到。具体就不说了 然后这...
  • cherry_sun
  • cherry_sun
  • 2012-05-08 20:16:51
  • 1031
收藏助手
不良信息举报
您举报文章:1239 Increasing Sequences
举报原因:
原因补充:

(最多只允许输入30个字)