Shredding Company
Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 6173   Accepted: 3361

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 :

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 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. 

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

Source

 
#include<iostream>
#include<cstdio>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 7
#define INF 1000000009
/*
给一个数字和一个数组,把数组分成多段,输出生成结果最接近目标数字的解
回溯法搜索,两种情况:分割该点作为新的起点 和 将该点接在前一段后面
*/
int aim,n,k,cntsame;
char s[MAXN];
vector<vector<char> > ans;
void solve(int x,int cnt,vector<vector<char> > tmp)//在当前数组中遍历的位置,取的段数,临时数组
{
if (x == n)
{
int sum = ;
for (int i = ; i < tmp.size(); i++)
{
int tt = ;
for (int j = ; j < tmp[i].size(); j++)
{
tt = tt * + (tmp[i][j] - '');
if (tt > aim)
return;
}
sum += tt;
if (sum > aim)
return;
}
if (aim - sum < k)
{
k = aim - sum;
cntsame = ;
ans = tmp;
}
else if (aim - sum == k)
{
cntsame++;
}
return;
}
tmp[cnt].push_back(s[x]);
solve(x + ,cnt,tmp);
if (x)
{
tmp[cnt].pop_back();
tmp[++cnt].push_back(s[x]);
solve(x + , cnt, tmp);
}
}
void print()
{
if (cntsame > )
printf("rejected\n");
else if (cntsame == )
printf("error\n");
else
{
printf("%d", aim-k);
for (int i = ; i < ans.size(); i++)
{
printf(" ");
for (int j = ; j < ans[i].size(); j++)
{
printf("%c", ans[i][j]);
}
}
printf("\n");
}
}
int main()
{
while (scanf("%d%s", &aim, s), s[] != ''&&aim != )
{
int j = atoi(s);
if (j == aim)
{
printf("%d %d\n", aim, aim);
continue;
} cntsame = ;
k = INF;
n = strlen(s);
vector<vector<char> > a;
a.resize();
solve(, , a);
print();
}
return ;
}

POJ 1416 Shredding Company 回溯搜索 DFS的更多相关文章

  1. 搜索+剪枝 POJ 1416 Shredding Company

    POJ 1416 Shredding Company Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5231   Accep ...

  2. POJ 1416 Shredding Company【dfs入门】

    题目传送门:http://poj.org/problem?id=1416 Shredding Company Time Limit: 1000MS   Memory Limit: 10000K Tot ...

  3. poj 1416 Shredding Company( dfs )

    我的dfs真的好虚啊……,又是看的别人的博客做的 题目== 题目:http://poj.org/problem?id=1416 题意:给你两个数n,m;n表示最大数,m则是需要切割的数. 切割m,使得 ...

  4. OpenJudge 2803 碎纸机 / Poj 1416 Shredding Company

    1.链接地址: http://poj.org/problem?id=1416 http://bailian.openjudge.cn/practice/2803 2.题目: 总时间限制: 1000ms ...

  5. POJ 1416 Shredding Company

    题目: http://poj.org/problem?id=1416 又16ms 1A了,这人品... #include <stdio.h> #include <string.h&g ...

  6. Shredding Company (hdu 1539 dfs)

    Shredding Company Time Limit: 5000/2000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others ...

  7. POJ - 1321 棋盘问题 简单搜索 dfs 格子

    点这里去看题 思路:本题的难点在k<n的情况,所以我们可以另dfs中的两个参数分别代表起始行和待放棋子个数(待放棋子只能放在起始行后面的行),然后用一个c[8]来表示每一列放旗子的情况来判断列不 ...

  8. POJ 1416:Shredding Company

    Shredding Company Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4713   Accepted: 2714 ...

  9. POJ1416——Shredding Company(DFS)

    Shredding Company DescriptionYou have just been put in charge of developing a new shredder for the S ...

随机推荐

  1. PCB genesis连孔加除毛刺孔(槽孔与槽孔)实现方法(三)

    一.为什么 连孔加除毛刺孔 原因是 PCB板材中含有玻璃纤维, 毛刺产生位置在于2个孔相交位置,由于此处钻刀受力不均导致纤维切削不断形成毛刺 ,为了解决这个问题:在钻完2个连孔后,在相交处再钻一个孔, ...

  2. 【WIP】Ruby CSV文件操作

    创建: 2017/09/30                                                                                       ...

  3. E20170628-hm

    forgery   n. 伪造; 伪造罪; 伪造物; 伪造签字; distribute   vt. 分配,散布; 散发,分发; 把…分类; [电] 配电; contribute to(contribu ...

  4. [Swift通天遁地]二、表格表单-(15)自定义表单文本框内容的格式

    ★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★★➤微信公众号:山青咏芝(shanqingyongzhi)➤博客园地址:山青咏芝(https://www.cnblogs. ...

  5. drawable的文件名大写

    drawable的文件名大写导致的R文件消失!!!1

  6. 【BZOJ2565】最长双回文串 (Manacher算法)

    题目: BZOJ2565 分析: 首先看到回文串,肯定能想到Manacher算法.下文中字符串\(s\)是输入的字符串\(str\)在Manacher算法中添加了字符'#'后的字符串 (构造方式如下) ...

  7. 【Vijos1083/BZOJ1756】小白逛公园(线段树)

    [写在前面]TYC (Little White) 真是太巨啦! 题目: Vijos1083 分析: 一眼看上去就是线段树啊-- 然而当我这种蒟蒻兴高采烈地把线段树模板敲了一半,却发现一个问题: 这子区 ...

  8. RabbitMQ~消费者实时与消息服务器保持通话

    这个文章主要介绍简单的消费者的实现,rabbitMQ实现的消费者可以对消息服务器进行实时监听,当有消息(生产者把消息推到服务器上之后),消费者可以自动去消费它,这通常是开启一个进程去维护这个对话,它与 ...

  9. CF830A/831D Office Keys

    思路: 问题的关键在于对钥匙按照位置排序之后,最终选择的n个钥匙一定是其中的一个连续的区间. 实现: #include <iostream> #include <cstdio> ...

  10. 内网jenkins如何配置gitlab自动拉取代码打包

    在全局工具配置中添加git安装目录的配置 http://10.2.1.92:8080/jenkins/configureTools/git1.8.3.1/usr/bin/git 打开系统设置配置git ...