Shredding Company
InterviewTime limit2sMemory limit512 MB
Split a digit string into contiguous pieces so their sum is as large as possible without exceeding a target, reporting rejection on ties and error when even the smallest sum is too big.
- Level
Medium6 of 10
- Topics
- Backtracking, Brute force, Recursion, Implementation
- Solved
- No attempts yet
Problem
You have just been put in charge of developing a new shredder for the Shredding Company. A normal shredder cuts sheets of paper into pieces small enough that the contents cannot be read. This new shredder works differently.
- The shredder takes a target number and a sheet of paper with a number written on it.
- It cuts the sheet into pieces, and every piece carries one or more digits.
- The sum of the numbers on the pieces is as close to the target number as possible without going over it.
Suppose the target number is 50 and the sheet of paper has the number 12346. The shredder cuts the sheet into four pieces: 1, 2, 34, and 6. Their sum is 43 (= 1 + 2 + 34 + 6), which is the closest to 50 among all combinations that do not exceed 50. Cutting into 1, 23, 4, and 6 is not valid, because that sum is 34 (= 1 + 23 + 4 + 6), which is smaller than 43. Cutting into 12, 34, and 6 is not valid either, because that sum is 52 (= 12 + 34 + 6), which is greater than the target number 50.

Figure 1. Shredding a sheet of paper with the number 12346 when the target number is 50
Three special rules apply.
- If the target number is the same as the number on the sheet, the paper is not cut. When the target number is 100 and the number on the sheet is also 100, the paper stays whole.
- If no combination has a sum less than or equal to the target number, the display prints
error. When the target number is 1 and the number on the sheet is 123, the combination with the smallest sum is 1, 2, 3, whose sum is 6. That is larger than the target number, soerroris printed. - If more than one combination reaches the closest sum without going over the target number, the display prints
rejected. When the target number is 15 and the number on the sheet is 111, two combinations reach the largest possible sum 12: (a) 1 and 11, and (b) 11 and 1. Sorejectedis printed.
To build the shredder, you decide to write a program that simulates the behavior and the rules above. Given a target number and the number on the sheet of paper, work out how the shredder should cut the second number.
Input
The input has several test cases, one per line.
t1 num1
t2 num2
...
tn numn
0 0
Each test case is two positive integers separated by one space. The first integer is the target number. The second integer is the number on the paper to be shredded.
Neither integer has 0 as its first digit, so 123 is allowed and 0123 is not. Both integers are at most 6 digits long. A line holding two zeros ends the input.
Output
For each test case, print one line in one of these three forms.
- ...
rejectederror
In the first form, and mean the following.
- is the number on one piece of shredded paper. The pieces follow the order of the original digits on the sheet.
- is the total of the numbers after shredding, so .
Separate the numbers with one space.
Print error when no combination is possible, and rejected when more than one combination reaches the closest sum.
Do not print extra characters, including spaces, at the start of a line or at the end of a line.