Poj1416 -- shredding company (DFS)

Source: Internet
Author: User

Shredding Company

Description
You have just been put in charge of developing a new shredder for the shredding company although a "normal" shredder wocould just shred sheets of paper into little pieces so that the contents wocould 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 wocould 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: () 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 wocould 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 shocould "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 shoshould 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
Question:

A Nb shredder, given a maximum target and a maximum of six digits, splits the number.

The number on the split part is the largest and cannot exceed the upper limit of target.

If it can be achieved and the situation is unique, the maximum sum is output, and the number formed after division.

Output rejected if the condition is not unique

If not, an error is output.

Solution:

Simple DFS.

First, the number is decomposed and saved to the edge [I] [J] array. For example, 12346, edge [2] [5]. Goal = 234, edge [2] [5]. Str = "234 ".

DFS stores the current status in the string each time to enter the next layer.

After the result is determined, the result is saved to the score array. If the array is empty, the error is returned. After the result is sorted in descending order, the first two are identical. Rejected.

Code:

1 /************************************** * *********************************** 2> File Name: poj1416.cpp 3> author: enumz 4> mail: [email protected] 5> created time: wednesday, 6 seconds ******************************* **************************************** */7 8 # include <iostream> 9 # include <cstdio> 10 # include <cstdlib> 11 # include <string> 12 # include <cstring> 13 # include <list> 14 # include <Qu EUE> 15 # include <stack> 16 # include <map> 17 # include <set> 18 # include <algorithm> 19 # include <cmath> 20 # include <bitset> 21 # include <climits> 22 # define maxn 1000023 using namespace STD; 24 string num; 25 int T, Len; 26 int flag, rejected; 27 int K; 28 struct s29 {30 int goal; 31 string STR; 32} score [maxn], edge [10] [10]; 33 bool DFS (INT now, int ret, string tmp_str) 34 {35 if (Ret> T | now> Len) return false; 36 IF (Ret <= T & Now = Len) 37 {38 score [K]. goal = ret; 39 score [K]. STR = tmp_str; 40 k ++; 41 flag = 1; 42 return true; 43} 44 int next = now + 1; 45 for (INT I = next; I <= Len; I ++) 46 DFS (I, RET + edge [next] [I]. goal, tmp_str + "" + edge [next] [I]. str); 47} 48 void build_map () 49 {50 for (INT I = 1; I <= Len; I ++) 51 for (Int J = I; j <= Len; j ++) 52 {53 if (I = J) {edge [I] [J]. goal = num [I]-'0'; edge [I] [J]. STR = num [I];} 54 else55 {56 edge [I] [J]. goal = edge [I] [J-1]. goal * 10 + num [J]-'0'; edge [I] [J]. STR = edge [I] [J-1]. STR + num [J]; 57} 58} 59} 60 bool CMP (s a, s B) 61 {62 Return. goal> B. goal; 63} 64 int main () 65 {66 while (CIN> T> num) 67 {68 if (! T & num [0] = '0') break; 69 Len = num. length (); 70 num = "" + num; 71 build_map (); 72 flag = rejected = 0, k = 1; 73 DFS (0, 0 ,""); 74 if (k> 1) sort (score + 1, score + k, CMP); 75 if (score [1]. goal = score [2]. goal & score [1]. goal! = 0) rejected = 1; 76 if (flag &&! Rejected) 77 cout <score [1]. goal <score [1]. STR <Endl; 78 else if (rejected) 79 cout <"REJECTED" <Endl; 80 else81 cout <"error" <Endl; 82} 83 return 0; 84}

 

Poj1416 -- shredding company (DFS)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.