Shredding Company--(DFS)

来源:互联网 发布:素颜霜 知乎 编辑:程序博客网 时间:2024/05/10 19:26

Shredding Company

Time Limit : 2000/1000ms (Java/Other)   Memory Limit : 20000/10000K (Java/Other)
Total Submission(s) : 30   Accepted Submission(s) : 12
Problem Description
You have 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 that the contents would 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 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. 
 
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. 

 

Input
The input consists of several test cases, each on one line, as follows : <br> <br>tl num1 <br>t2 num2 <br>... <br>tn numn <br>0 0 <br> <br>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. <br> <br>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. <br> <br>
 

Output
For each test case in the input, the corresponding output takes one of the following three types : <br> <br>sum part1 part2 ... <br>rejected <br>error <br> <br>In the first type, partj and sum have the following meaning : <br> <br>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. <br> <br>2.sum is the sum of the numbers after being shredded, i.e., sum = part1 + part2 +... <br> <br>Each number should be separated by one space. <br>The message error is printed if it is not possible to make any combination, and rejected if there is <br>more than one possible combination. <br>No extra characters including spaces are allowed at the beginning of each line, nor at the end of each line. <br>
 

Sample Input
50 12346376 144139927438 92743818 33129 314225 1299111 33333103 8621506 11040 0
 

Sample Output
43 1 2 34 6283 144 139927438 92743818 3 3 12error21 1 2 9 9rejected103 86 2 15 0rejected

题意:

给出目标数和一串数字,要求将那串数字分解为几段使其和最接近且小于目标数(要求分段时不能打乱字符串先后次序),若目标数与字符串本身相同直接输出,若无法组出输出error,最终结果有多种不同分段情况输出rejected

解题思路:
想到用dfs,然后想怎么设计递归的过程,发现只要将所有所给数字都分好段就会有一个结果,于是想到用dfs(i)来开启新的一次分段,前面分段和用sum传到形参,看起来很麻烦的题,做着都没信心,没想到一次AC

代码:

#include<iostream>#include<string>#include<cstdio>#include<algorithm>#include<cmath>#include<iomanip>#include<queue>#include<cstring>using namespace std;int flag,n,num[10],total,cnt[10],t[10]; //flag记录dfs结果,n是目标数,num存放所给数字串char s[10];                             //total存放结果分段和,cnt存放分段情况,t是过渡数组。void dfs(int i,int sum,int step)  //i代表从第i个字符开始新的分段,sum是之前分段和,step是说明该分第几段{    if(i>num[0]||sum>n||step>num[0]) return; //超出情况返回    for(int j=i;j<=num[0];j++)       //第i段为从num[i]一直乘到num[j]    {        int tt=0,temp=t[step];       //tt是第i段和,temp暂存t[step]的值,后面用来回溯        for(int k=i;k<=j;k++)            tt=tt*10+num[k];        t[step]=tt;        if(j==num[0])       //如果已经把最后一个也分好了,判断分出的结果        {            if(sum+tt<=n)  //前提是小于n            {                if(sum+tt>total) //大于之前保存的结果,更新结果                {                    total=tt+sum;                    cnt[0]=step; flag=1;                    for(int r=1;r<=step;r++)                        cnt[r]=t[r];                }                else if(sum+tt==total) flag++; //是已经出现过的结果也要记录下来            }        }        dfs(j+1,sum+tt,step+1);        t[step]=temp;    }}int main(){    int i; cnt[0]=0;    while(scanf("%d",&n)&&n)    {        gets(s);        num[0]=strlen(s)-1;        for(i=1;i<=num[0];i++)            num[i]=s[i]-'0';        int sum=0;        for(i=1;i<=num[0];i++)     //前后两个数相等直接输出        {            sum=sum*10+num[i];        }        if(sum==n) {printf("%d %d\n",n,sum); continue;}        total=flag=0;        dfs(1,0,1);        if(!flag) printf("error\n");        else if(flag>1) printf("rejected\n");        else {                printf("%d",total);                for(i=1;i<=cnt[0];i++)                    printf(" %d",cnt[i]);                printf("\n");            }    }    return 0;}

 
原创粉丝点击