Shredding Company
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 4397 |
|
Accepted: 2519 |
Description
You are 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 then the contents would become unreadable, this new shredder needs to ha ve 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 the which have one or more digits on It.
3.The sum of the numbers written on each piece are the closest possible number to the target number, and without going over it.
For example, suppose then the target number is, and the sheet of paper have the number 12346. The shredder would cut the sheet into four pieces, where one piece have 1, another have 2, the third has, and the fourth Has 6. This is because their sum (= 1 + 2 + + 6) are closest to the target number in all possible combinations without go ing over 50. For example, a combination where the pieces was 1, 4, and 6 is not valid, because the sum of this combination 34 (= 1 + + + 4 + 6) is less than the above combination ' s 43. The combination of 6, 6 is not valid either, because the sum (= + + +) is greater than the target number of 50.
Figure 1. Shredding a sheet of paper have the number 12346 when the target number is 50
There is also three special rules:
1.If the target number is the same as the number on the sheet of paper and then the paper was not cut.
For example, if the target number is a and the number on the sheet of paper is also
The paper is not cut.
2.If It is not possible to make any combination whose sum are less than or equal to the target number and then error is Printe D on a display. For example, if the target number was 1 and the number on the sheet of paper is 123, it was not possible-make any valid C Ombination, as the combination with the smallest possible sum is 1, 2, 3. The sum for this combination are 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 was closest to the target number without going over it and then Rejected is printed on a display. For example, if the target number is a, and the number on the sheet of paper was 111, then there is, possible Combinat Ions with the highest possible sum of: (a) 1 and one and (b) and 1; Thus rejected is printed. In order to develop such a shredder, you are decided to first make a simple program that would simulate the above charact Eristics and rules. Given-Numbers, where the first is the target number and the second are the number on the sheet of paper-be shredded, You need 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-positive integers, which is separated by one space: (1) the first integer ( Ti above) is the target number and (2) the second integer (Numi above) is the number, which is on the paper to be shredded.
Neither integers may have a 0 as the first digit, e.g., 123 are allowed but 0123 are not. Assume that both integers is at the most 6 digits in length. A line consisting of 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 has 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 is separated by one space.
The message error is printed if it isn't possible to make any combination, and rejected if there is
More than one possible combination.
No extra characters including spaces is allowed at the beginning of all line and nor at the end of each line.
Sample Input
50 12346376 144139927438 92743818 33129 314225 1299111 33333103 8621506 11040 0
Sample Output
1 2 6283 144 139927438 92743818 3 3 12error21 1 2 9 9rejected103 2 0rejected
Transmission Door
Test instructions
Give a target number, and then give a paper on which the number is written, paper-cutting machine can be any position paper-cut, cut down the number of fragments and the closest to the target digital output paper-cutting scheme,
There are several rules in the details: cut down the number and cannot exceed the target number, so DFS can take advantage of this pruning.
If all the paper-cutting scheme produces the sum of the number is greater than the target number, then output error.
If there are two or more scenarios, then the output rejected.
Idea: DFS, more details, such as using an ANS array to record the paper-cut scheme, how to let the simulation of paper-cutting process, how to let the number of records program has a certain amount of detail, all of my current level has a certain degree of exercise ability.
memory:360 kbtime:0 ms#include<cstdio> #include <cstring> #define INF 0x7fffffffusing namespace Std;int s, G;int wei,minn,cnt;int stc[110],top,flag;int ans[110];void ini () {cnt=wei=1; top=flag=0; Minn=inf; int tmp=s; while (tmp/=10) wei++; Calculates the number of bits on a piece of paper}void dfs (int le,int sum,int POS)//le represents the remaining number on the current piece of paper, sum is the current cut-down paper number and {//pos is the current cut or The number of if (SUM>G) return on the paper-cut piece; Pruning if (pos==0) {if (G-sum<minn) {minn=g-sum; flag=1; for (int i=1;i<=top;i++) ans[i]=stc[i];//uses ANS to record the cut fragments that are closer to the target each time. for (int i=top+1;i<110;i++) ans[i]=-1;//tip, if you don't add this, you'll have a few numbers behind the ANS array, not the The number of cnt=1 produced by the round paper-cutting; Details, is currently closer to the goal of the programme, there is only one option. return; } if (G-sum==minn) {for (int i=1;i<=top;i++) ans[i]=stc[i]; for (int i=top+1;i<110;i++) ans[i]=-1; cnt++; The closest target to the currentPaper-Cut has several methods of paper-cut return; } if (G-sum>minn) return; } int tmp=1; for (int i=1;i<pos;i++) tmp*=10; The following three lines simulate how to cut the int nxs=sum+le/tmp from the POS position; int nxl=le%tmp; stc[++top]=le/tmp; DFS (NXL,NXS,POS-1); top--; if (pos!=1) {//detail, Pos=1 does not need to be cut, or said to have been cut, because the end of the paper must be disconnected DFS (LE,SUM,POS-1); }}int Main () {while (~scanf ("%d%d", &g,&s) && (s| | g)) {ini (); DFS (S,0,wei); if (cnt>=2) printf ("rejected\n"); else if (!flag) printf ("error\n"); else{printf ("%d", G-minn); for (int i=1;i<110;i++) {if (ans[i]!=-1) printf ("%d", ans[i]); } puts (""); }} return 0;}
Poj-1416-shredding Company (Analog +dfs)