Shredding Company
Time Limit: 1000MS |
|
Memory Limit: 10000K |
Total Submissions: 4550 |
|
Accepted: 2605 |
Description
You have just been put in charge of developing anew shredder for the Shredding Company Although a "normal" shredderwould just shred sheets of paper into little pieces so that thecontents would become unreadable, this new shredder needs to havethe following unusual basiccharacteristics.
1.The shredder takes as input a target number and a sheet of paperwith a number written on it.
2.It shreds (or cuts) the sheet into pieces each of which has oneor more digits on it.
3.The sum of the numbers written on each piece is the closestpossible number to the target number, without going overit.
For example, suppose that the target number is 50, and the sheet ofpaper has the number 12346. The shredder would cut the sheet intofour pieces, where one piece has 1, another has 2, the third has34, and the fourth has 6. This is because their sum 43 (= 1 + 2 +34 + 6) is closest to the target number 50 of all possiblecombinations without going over 50. For example, a combinationwhere the pieces are 1, 23, 4, and 6 is not valid, because the sumof this combination 34 (= 1 + 23 + 4 + 6) is less than the abovecombination's 43. The combination of 12, 34, and 6 is not valideither, because the sum 52 (= 12 + 34 + 6) is greater than thetarget number of 50.
Figure 1. Shredding a sheet of paper having the number 12346 whenthe target number is 50
There are also three special rules :
1.If the target number is the same as the number on the sheet ofpaper, then the paper is not cut.
For example, if the target number is 100 and the number on thesheet of paper is also 100, then
the paper is not cut.
2.If it is not possible to make any combination whose sum is lessthan or equal to the target number, then error is printed on adisplay. For example, if the target number is 1 and the number onthe sheet of paper is 123, it is not possible to make any validcombination, as the combination with the smallest possible sum is1, 2, 3. The sum for this combination is 6, which is greater thanthe target number, and thus error isprinted.
3.If there is more than one possible combination where the sum isclosest to the target number without going over it, then rejectedis 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 twopossible combinations with the highest possible sum of 12: (a) 1and 11 and (b) 11 and 1; thus rejected is printed. In order todevelop such a shredder, you have decided to first make a simpleprogram that would simulate the above characteristics and rules.Given two numbers, where the first is the target number and thesecond is the number on the sheet of paper to be shredded, you needto figure out how the shredder should "cut up" the secondnumber.
Input
The input consists of several test cases, each onone 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 thenumber that is on the paper to be shredded.
Neither integers may have a 0 as the first digit, e.g., 123 isallowed but 0123 is not. You may assume that both integers are atmost 6 digits in length. A line consisting of two zeros signals theend of the input.
Output
For each test case in the input, the correspondingoutput 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 orderof partj corresponds to the order of the original digits on thesheet of paper.
2.sum is the sum of the numbers after being shredded, i.e., sum =part1 + part2 +...
Each number should be separated by onespace.
The message error is printed if it is not possible to make anycombination, and rejected if there is
more than one possible combination.
No extra characters including spaces are allowed at the beginningof each line, nor at the end of eachline.
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
分析:由于数据水,所以只需要暴力来处理分段,记录最优值即可。
代码:
#include
#include
#include
#include
#include
using namespace std;
int a[1991];
int ans[1991];
int n,m;
int can[1000000],maxx=0;
void solve(int x,int y)
{
if(m==n)
{
a[++a[0]]=m;
goto z;
}
if(y==0)
{
z: int i,j=0;
for(i=1;i<=a[0];i++)
j+=a[i];
if(can[j]==0&&j>maxx&&j<=n)
{
for(i=0;i<=a[0];i++)
ans[i]=a[i];
maxx=j;
}
can[j]++;
return ;
}
else
{
int i,j,k=0,mod=1,t=m;
a[0]++;
for(i=1;i<=y;i++)
{
mod=1;
for(j=i;j
mod*=10;
a[a[0]]=m/mod;
m%=mod;
solve(x,y-i);
a[a[0]]=0;
m=t;
}
a[0]--;
m=t;
return ;
}
}
int size(int x)
{
int res=0;
while(x>0)
res++,x/=10;
return res;
}
void read()
{
freopen("std.in","r",stdin);
freopen("self.out","w",stdout);
do
{
scanf("%d%d",&n,&m);
maxx=0;
memset(can,0,sizeof(can));
memset(a,0,sizeof(a));
memset(ans,0,sizeof(ans));
if(n==m&&m==0)
exit(0);
if(n==m)
{
printf("%d %d \n",n,m);
continue;
}
solve (1,size(m));
if(maxx==0)
{
printf("error\n");
continue;
}
else if(can[maxx]>1)
{
printf("rejected\n");
continue;
}
else if(can[maxx]==1)
{
printf("%d ",maxx);
for(int i=1;i<=ans[0];i++)
printf("%d ",ans[i]);
printf("\n");
}
} while(n!=0&&m!=0);
}
int main()
{
read();
return 0;
}