Shredding Company

Time Limit: 1000MS Memory Limit: 10000K

Total Submissions: 4653 Accepted: 2675

Description

You have just been put in charge of developing a new shredder for the Shredding Company Although a “normal” shredder would just shred sheets of paper into little pieces so that the contents would become unreadable, this new shredder needs to have the following unusual basic characteristics.

1.The shredder takes as input a target number and a sheet of paper with a number written on it.

2.It shreds (or cuts) the sheet into pieces each of which has one or more digits on it.

3.The sum of the numbers written on each piece is the closest possible number to the target number, without going over it.

For example, suppose that the target number is 50, and the sheet of paper has the number 12346. The shredder would cut the sheet into four pieces, where one piece has 1, another has 2, the third has 34, and the fourth has 6. This is because their sum 43 (= 1 + 2 + 34 + 6) is closest to the target number 50 of all possible combinations without going over 50. For example, a combination where the pieces are 1, 23, 4, and 6 is not valid, because the sum of this combination 34 (= 1 + 23 + 4 + 6) is less than the above combination’s 43. The combination of 12, 34, and 6 is not valid either, because the sum 52 (= 12 + 34 + 6) is greater than the target number of 50.

Figure 1. Shredding a sheet of paper having the number 12346 when the target number is 50

There are also three special rules :

1.If the target number is the same as the number on the sheet of paper, then the paper is not cut.

For example, if the target number is 100 and the number on the sheet of paper is also 100, then

the paper is not cut.

2.If it is not possible to make any combination whose sum is less than or equal to the target number, then error is printed on a display. For example, if the target number is 1 and the number on the sheet of paper is 123, it is not possible to make any valid combination, as the combination with the smallest possible sum is 1, 2, 3. The sum for this combination is 6, which is greater than the target number, and thus error is printed.

3.If there is more than one possible combination where the sum is closest to the target number without going over it, then rejected is printed on a display. For example, if the target number is 15, and the number on the sheet of paper is 111, then there are two possible combinations with the highest possible sum of 12: (a) 1 and 11 and (b) 11 and 1; thus rejected is printed. In order to develop such a shredder, you have decided to first make a simple program that would simulate the above characteristics and rules. Given two numbers, where the first is the target number and the second is the number on the sheet of paper to be shredded, you need to figure out how the shredder should “cut up” the second number.

Input

The input consists of several test cases, each on one line, as follows :

tl num1

t2 num2



tn numn

0 0

Each test case consists of the following two positive integers, which are separated by one space : (1) the first integer (ti above) is the target number, (2) the second integer (numi above) is the number that is on the paper to be shredded.

Neither integers may have a 0 as the first digit, e.g., 123 is allowed but 0123 is not. You may assume that both integers are at most 6 digits in length. A line consisting of two zeros signals the end of the input.

Output

For each test case in the input, the corresponding output takes one of the following three types :

sum part1 part2 …

rejected

error

In the first type, partj and sum have the following meaning :

1.Each partj is a number on one piece of shredded paper. The order of partj corresponds to the order of the original digits on the sheet of paper.

2.sum is the sum of the numbers after being shredded, i.e., sum = part1 + part2 +…

Each number should be separated by one space.

The message error is printed if it is not possible to make any combination, and rejected if there is

more than one possible combination.

No extra characters including spaces are allowed at the beginning of each line, nor at the end of each line.

Sample Input

50 12346

376 144139

927438 927438

18 3312

9 3142

25 1299

111 33333

103 862150

6 1104

0 0

Sample Output

43 1 2 34 6

283 144 139

927438 927438

18 3 3 12

error

21 1 2 9 9

rejected

103 86 2 15 0

rejected

Source

Japan 2002 Kanazawa

简单的DFS搜索,可是自己理解题意不深,WA了两次

  1. #include <map>
  2. #include <cmath>
  3. #include <queue>
  4. #include <stack>
  5. #include <string>
  6. #include <cstdio>
  7. #include <cstring>
  8. #include <cstdlib>
  9. #include <iostream>
  10. #include <algorithm>
  11. using namespace std;
  12. const int INF =0x3f3f3f3f;
  13. const double eps =1e-8;
  14. const int Max =3000;
  15. int n,m;
  16. int Arr[15];
  17. int b[15];
  18. int c[15];
  19. int MM;
  20. bool flag;
  21. int top;
  22. int nn;
  23. void DFS(int site,int num)
  24. {
  25. if(site==top)
  26. {
  27. int sum=0;
  28. for(int i=0;i<num;i++)
  29. {
  30. sum+=Arr[i];
  31. }
  32. if(sum<=n)
  33. {
  34. if(sum==MM)
  35. {
  36. flag=true;
  37. }
  38. else if(sum>MM)
  39. {
  40. for(int i=0;i<num;i++)
  41. {
  42. c[i]=Arr[i];
  43. }
  44. MM=sum;
  45. nn=num;
  46. flag=false;
  47. }
  48. }
  49. }
  50. Arr[num]=0;
  51. for(int i=site;i<top;i++)
  52. {
  53. Arr[num]=(Arr[num]*10+b[i]);
  54. DFS(i+1,num+1);
  55. }
  56. }
  57. int main()
  58. {
  59. while(scanf("%d %d",&n,&m)&&(n||m))
  60. {
  61. if(n==m)
  62. {
  63. printf("%d %d\n",n,m);
  64. continue;
  65. }
  66. top=0;
  67. while(m)
  68. {
  69. Arr[top++]=m%10;
  70. m/=10;
  71. }
  72. for(int i=top-1,j=0;i>=0;i--,j++)
  73. {
  74. b[j]=Arr[i];
  75. }
  76. MM=0;
  77. flag=false;
  78. memset(Arr,0,sizeof(Arr));
  79. DFS(0,0);
  80. if(MM==0)
  81. {
  82. printf("error\n");
  83. }
  84. else
  85. {
  86. if(flag)
  87. {
  88. printf("rejected\n");
  89. }
  90. else
  91. {
  92. printf("%d",MM);
  93. for(int i=0;i<nn;i++)
  94. {
  95. printf(" %d",c[i]);
  96. }
  97. printf("\n");
  98. }
  99. }
  100. }
  101. return 0;
  102. }

Shredding Company的更多相关文章

  1. poj1416 Shredding Company

    Shredding Company Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5379   Accepted: 3023 ...

  2. 搜索+剪枝 POJ 1416 Shredding Company

    POJ 1416 Shredding Company Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5231   Accep ...

  3. POJ1416——Shredding Company(DFS)

    Shredding Company DescriptionYou have just been put in charge of developing a new shredder for the S ...

  4. Shredding Company(dfs)

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3519   Accepted: 2009 Description You h ...

  5. POJ 1416 Shredding Company【dfs入门】

    题目传送门:http://poj.org/problem?id=1416 Shredding Company Time Limit: 1000MS   Memory Limit: 10000K Tot ...

  6. POJ 1416 Shredding Company 回溯搜索 DFS

    Shredding Company Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 6173   Accepted: 3361 ...

  7. Shredding Company (hdu 1539 dfs)

    Shredding Company Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  8. POJ 1416:Shredding Company

    Shredding Company Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4713   Accepted: 2714 ...

  9. poj 1416 (hdu 1539)Shredding Company:剪枝搜索

    点击打开链接 题目大意是有一个分割机,可以把一串数字分割成若干个数字之后求和,题目输入一个数字上界和待分割的数字,让我们求出分割后数字之和在不超过给定max的情况下的最大值,并且给出分割方案,如果没有 ...

随机推荐

  1. C# 以管理员方式启动Winform,进而使用管理员控制Windows Service

    问题起因: 1,) 问题自动分析Windows服务在正常运行时,确实会存在程序及人为原因导致该服务停止.为了在应用程序使用时确保该服务正常运行,于是有了该讨论主题. 2,)一般账户(尽管是管理员组账户 ...

  2. C# 微信支付教程系列之扫码支付

    微信支付教程系列之扫码支付            今天,我们来一起探讨一下这个微信扫码支付.何为扫码支付呢?这里面,扫的码就是二维码了,就是我们经常扫一扫的那种二维码图片,例如,我们自己添加好友的时候 ...

  3. Java基础(32):String与StringBuilder、StringBuffer的区别(String类)

    在Java中,除了可以使用 String 类来存储字符串,还可以使用 StringBuilder 类或 StringBuffer 类存储字符串,那么它们之间有什么区别呢? String 类具有是不可变 ...

  4. php扩展的基本安装

    phpize ./config --with-php-config=.. make&make install php.ini

  5. java-excel导出

    java excel导出分为两种2003年的格式和2007年的格式. 2003年的xls一个sheet限制65536. 2007年的xlsx限制为1048576. jxl导入2003 gradle j ...

  6. po line received is canceled(恢复PO被取消的余量)

    1張PO已部分收貨,後來由于某種原因,將部分收貨的PO明行取消,現在要對已收料的這一部分進行退貨處理,要怎麼做才好呢? [@more@]DATA COLLECTED===============COL ...

  7. Power Gating的设计(概述)

    Leakage power随着CMOS电路工艺进程,功耗越来越大. Power Domain的开关一般通过硬件中的timer和系统层次的功耗管理软件来进行控制,需要在一下几方面做trade-off: ...

  8. 【《zw版·Halcon与delphi系列原创教程》Halcon图层与常用绘图函数

    [<zw版·Halcon与delphi系列原创教程>Halcon图层与常用绘图函数 Halcon的绘图函数,与传统编程vb.c.delphi语言完全不同,     传统编程语言,甚至cad ...

  9. XMl的解析

    MainActivitypackage com.example.secondweek_test2; import java.io.BufferedInputStream; import java.io ...

  10. 161122、BOM 操作写法示例

    浏览器相关信息 // 浏览器信息 navigator.userAgent // Mozilla/5.0 (Macintosh; Intel Mac OS X 10_11_6) AppleWebKit/ ...