POJ-1416

描述:

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.

输入:

The input consists of several test cases, each on one line, as follows :

1
2
3
4
5
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.

输出:

For each test case in the input, the corresponding output takes one of the following three types :

1
2
3
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.

输入示例:

1
2
3
4
5
6
7
8
9
10
50 12346
376 144139
927438 927438
18 3312
9 3142
25 1299
111 33333
103 862150
6 1104
0 0

输出示例:

1
2
3
4
5
6
7
8
9
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

题目大意:

给你一个有n个数的纸条,一个目标值,你可以任意剪纸条,要找出剪出的数的和最接近并且小于目标数的分解方式。

如果该数可以由多个分解方式组成,则输出error;如果无论如何分解都比目标值大,则输出rejected。如果分解值和目标数相同,则直接输出。

思路:

首先对于剪纸条,无论如何剪,相邻数的位置关系并不会发生变化,只是选择元素的个数会发生变化,则可以dfs枚举所有的可能————对于某一过程,可以选择从该数开始到纸条结尾,每次都选择增加一个数,然后进行下一层的dfs,即可取得所有的分解组合。

接下来要保留选择数的路径,我们使用一个数的每一位来记录当前选择了几个数(N最大为6位,所以一位足够表示)。

然后,对于大于目标数的情况,只有当最小分解方式(每一位都被分解)之和都比目标值大,则可以直接输出rejected。该步可在dfs之前判断。

对于error的情况,只要在每次更新当前值的时候,也更新其相同的次数,如果大于1,则符合该情况。

可以小小的剪枝下:当过程中的取值比目标值大的话,就可以直接剪掉。

最后就是编码的时刻了!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<vector>
#include<cstring>
using namespace std;
vector<vector<int> > ans;
int num; //目标数
int npath; //最优路径
int curNum; //当前数,dfs中间值
int cntNum; //当前数的出现次数。

/*
将结果数组中的各值相加
*/
int getNum(vector<vector<int> > t, int n){
int ans = 0;
int cont = 0;
for(int j = 0; j < t.size(); j++){
int c = 0;
cont += t[j].size();
for(int k = 0; k < t[j].size(); k++){
c = c*10 + t[j][k];
}
ans +=c;
}
return ans;
}
/*
i:当前位置
n:总长度
path:记录每次分割长度
s:总数据
t:结果数组。
*/
void dfs(int i,int n,int path,char s[],vector<vector<int> > t){
int tt = getNum(t,n);
//如果过程中已经比当前数大了,直接剪掉
if(tt > num) return;

if(i == n){
//在当前一轮结束时更新离目标数最近并小于目标数的值、次数与路径。
if(curNum < tt){
cntNum = 1;
curNum = tt;
npath = path;
}else if(curNum == tt){
cntNum++;
}
return;
}
/*
dfs核心:
输入:1234
生成下列排列
1 2 3 4
1 2 34
1 23 4
1 234
12 3 4
12 34
123 4
1234
*/
vector<int> a;
for(int j = i; j < n; j++){
a.push_back(s[j]-'0');
t.push_back(a);
dfs(j+1,n,path*10+j-i+1,s,t);
t.pop_back();
}

}

//根据path输出各个数。
void show(char s[],int path){
vector<int> a;
while(path){
a.push_back(path%10);
path/=10;
}
reverse(a.begin(),a.end());
int j = 0;
for(int i = 0; i < a.size(); i++){
printf(" ");
for(int k = 0; k < a[i]; k++){
printf("%c",s[j++]);
}
}
}

int main(){
//freopen("in.txt","r",stdin);
//freopen("out.txt","w",stdout);
int a;
char s[1000];
while(scanf("%d%s",&a,s) && a){
vector<vector<int> > t;
num = a;
curNum = 0;
cntNum = 0;

int sum = 0;
for(int i = 0; i < strlen(s); i++){
sum += s[i]-'0';
}
//给定数的每一位相加都比目标数大
if(sum > a){
printf("error\\n");
}else{
dfs(0,strlen(s),0,s,t);
if(cntNum > 1){
printf("rejected\\n");
}else{
printf("%d",curNum);
show(s,npath);
printf("\\n");
}
}
}
return 0;
}