Shredding Company
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 4595 |
|
Accepted: 2633 |
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
Source
Japan 2002 Kanazawa
Title Link: http://poj.org/problem?id=1416
The main idea: two not more than 6 bits of the integer n and M, the M split, such as M 123, can be split into three digits, or can be split into digital 12,3 or 1, 232 numbers, etc., required to split the sum of these numbers is not greater than N, to meet the maximum number of conditions and M split into what number, If you cannot get the answer, output "error", if there are multiple answers, output "rejeced" directly.
Solution Idea: Enumerate the lengths of the small numbers broken into, the sum of the small numbers in the DFS recursion process, the DFS1 search satisfies the maximum number of criteria and the DFS2 record path. Personally feel that they write some tedious, using two times DFS, I really can't think how to use a Dfs to complete these two steps, because the recursive process to find the best solution, in the case of uncertain optimal solution, the path is not convenient to record. So the first DFS gets the best solution, and the second DFS returns the record path after searching for the best solution.
The code is as follows:
#include <cstdio> #include <cstring>int ln,n,ans,cnt; ln is the length of the integer m, where an array of a[8] is used to denote that the integer m//ans is the optimal solution of the number and CNT is the subscript int a[8],path[8],vis[1000000] used to record the path; Path, Vis is used to determine if there are multiple optimal solutions char S[8];bool p; P Mark whether to search for a number that satisfies the condition and void dfs1 (int st,int sum)//search for the best number and {if (ST>=LN)//This time the integer m is divided into a small number that has all accumulated, getting a number and the solution {P=true;vi s[sum]++; Mark if there is more than one solution value if (ans<sum)//Find the maximum solution Ans=sum;return;} for (int i=0;i<ln;i++)//enumeration small number of length {if (ST+I>=LN) break;int cur=0;for (int j=st;j<=st+i;j++)//st enum array start subscript, The numbers on each of you are constructed into integers cur= (cur*10+a[j]); if (cur+sum>n)//small number and greater than N, do not meet the conditions, leave. BREAK;DFS1 (st+i+1,cur+sum); Start subscript Skips the current small number, and the next layer constructs a small number behind it. }}void dfs2 (int st,int sum)//record path {if (ST>=LN) {if (Sum==ans)//Find target value p=true;return;} for (int i=0;i<ln;i++) {if (ST+I>=LN) break;int cur=0;for (int j=st;j<=st+i;j++) cur= (Cur*10+a[j]); if (cur+sum >n) BREAK;DFS2 (st+i+1,sum+cur); if (p) {path[cnt++]=cur; The path from the back to the forward record return;}}} int main () {while (scanf ("%d", &n)!=eof&&Amp;n) {memset (vis,0,sizeof (VIS));p =false,ans=0,cnt=0;scanf ("%s", s); Ln=strlen (s); for (int i=0;i<ln;i++) a[i]=s[ i]-' 0 ';d fs1 (0,0), if (!p) printf ("error\n"), else if (vis[ans]>1) printf ("rejected\n"), else{printf ("%d", ans);p = FALSE;DFS2 (0,0), for (int i=cnt-1;i>=0;i--) printf ("%d", Path[i]);p rintf ("\ n");}} return 0;}
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
POJ 1416 Shredding Company (DFS)