Shredding Company(DFS)
Shredding Company
Time Limit : 2000/1000ms (Java/Other) Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 15 Accepted Submission(s) : 11
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.
![](http://poj.org/images/1416_1.jpg)
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.
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.
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.
题意:
公司现在要发明一种新的碎纸机,要求新的碎纸机能够把纸条上的数字切成最接近而不超过target值。比如,target的值是50,而纸条上的数字是12346,应该把数字切成四部分,分别是1、2、34、6。因为这样所得到的和43 (= 1 + 2 + 34 + 6) 是所有可能中最接近而不超过50的。(比如1, 23, 4, 和6 就不可以,因为它们的和不如43接近50,而12, 34, 6也不可以,因为它们的和超过50了。碎纸还有以下三个要求:
1、如果target的值等于纸条上的值,则不能切。
2、如果没有办法把纸条上的数字切成小于target,则输出error。如target是1而纸条上的数字是123,则无论你如何切得到的和都比1大。
3、如果有超过一种以上的切法得到最佳值,则输出rejected。如target为15,纸条上的数字是111,则有以下两种切法11、1或者1、11.
你的任务是编写程序对数字进行划分以达到最佳值。
解题思路:
用DFS深搜
(1)比如一个6位数n,切成为6个数的话,这6个数的和如果大于目标数aim则不用再搜索了,因为这肯定是所有划分中和最小的,而最小都比目标数大,自然就没有合要求的答案了.
(2) 如何切分,假如以50 12346 为例。
第一步,先切下一个“1”,然后递归去切“2346”;
第二步,再切下一个“12”,然后递归去切“346”;
第三步,再切下一个“123”,然后递归去切“46”;
第四步,再切下一个“1234” 然后递归去切“6”
第五步,再切下“12346”。
(3)当将数全部切完后比较是否是最大值,如果和大于已知的最大值,则将数组从头开始存储,如果等于已知的最大值,则将其存入数组中,如果小于已知最大值,则不用管,直接结束递归程序。
(4)如果递归结束后,数组中存储的元素超过1个则输出rejected
(5)如果递归结束后,数组中只有一个元素,则输出和,并进行分配输出。
AC代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include<iostream> 2 #include<cmath> 3 #include<string> 4 #include<cstdio> 5 #include<cstring> 6 7 8 using namespace std; 9 10 int n,number; 11 int sgin; 12 int sum=0; 13 int ma=0; 14 struct code 15 { 16 int sg; 17 int sum; 18 }; 19 code qun[100005]; 20 int ls=0; 21 22 23 int cut(int &num,int nu,int sd)//将长度为sd的数num,切下前长度nu,并返回切下的值; 24 { 25 int a=num; 26 for(int i=1;i<=sd-nu;i++){ 27 a/=10; 28 } 29 int sks=a; 30 for(int i=1;i<=sd-nu;i++){ 31 sks*=10; 32 } 33 num-=sks; 34 return a; 35 } 36 int DFS(int num,int k) 37 { 38 if(num==0&&k==0){//跳出条件,数的位数为0 39 if(sum<=n){//限制和的值必须小于n 40 if(ma<sum){//跟新和的最大值 41 ma=sum; 42 ls=0; 43 } 44 if(sum==ma){//经最大值得分法进行记录 45 qun[ls].sg=sgin; 46 qun[ls++].sum=sum; 47 } 48 } 49 return 0; 50 } 51 int i; 52 int sn=num; 53 for(i=1;i<=k;i++){ 54 sn=num; 55 int a=cut(sn,i,k); 56 sum+=a; 57 sgin*=10; 58 sgin+=i; 59 DFS(sn,k-i); 60 sgin/=10; 61 sum-=a; 62 } 63 return 0; 64 } 65 66 67 68 int main() 69 { 70 // freopen("input.txt","r",stdin); 71 while(cin>>n>>number){ 72 if(n==0&number==0) 73 break; 74 int k; 75 if(number<10) 76 k=1; 77 else if(number<100) 78 k=2; 79 else if(number<1000) 80 k=3; 81 else if(number<10000) 82 k=4; 83 else if(number<100000) 84 k=5; 85 else 86 k=6; 87 sum=0; 88 sgin=0; 89 int snu=number; 90 for(int i=k;i>=1;i--){ 91 int a=cut(snu,1,i); 92 sum+=a; 93 sgin*=10; 94 sgin+=1; 95 } 96 if(sum>n){//如果最小的分法都大于n,则证明该数是不可以分的 97 cout<<"error"<<endl; 98 continue; 99 } 100 sum=0; 101 sgin=0; 102 ma=0; 103 DFS(number,k); 104 if(ls>1){//数组中存储的元素超过了1个说明和最大的分法有多种; 105 cout<<"rejected"<<endl; 106 continue; 107 } 108 sgin=qun[0].sg; 109 cout<<ma<<" "; 110 int s=0; 111 int ss=sgin; 112 for(s=1;;s++){//检查sgin的长度 113 ss/=10; 114 if(ss==0) 115 break; 116 } 117 for(int i=s;i>=1;i--){//进行分配的输出 118 int a=cut(sgin,1,i); 119 cout<<cut(number,a,k)<<" "; 120 k-=a; 121 } 122 cout<<endl; 123 } 124 return 0; 125 }