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. zookeeper单机安装

    安装zookeeper步骤: 1,下载zookeeper http://mirror.bit.edu.cn/apache/zookeeper/zookeeper-3.4.14/ 2,放到合适目录,解压 ...

  2. PCB 规则引擎之脚本语言JavaScript应用评测

    世界上没有好做的软件,觉得好做,只是你的系统简单而已,而不是哪个行业简单,特别像我们PCB制造企业务逻辑的很复杂的,仅仅靠决策树中的每个节点布置决策逻辑是不能满足要求的,所以我们在制作PCB规则引擎必 ...

  3. c++ pow函数

    函数名称:   pow 函数原型:   double pow( double x, double y ); 函数功能:   计算x的y次幂 例:z=pow(x,y);    x=9,y=8  z就是9 ...

  4. Android内存管理(11)*常见JVM回收机制「Java进程内存堆分代,JVM分代回收内存,三种垃圾回收器」

    参考: http://www.blogjava.net/rosen/archive/2010/05/21/321575.html 1,Java进程内存堆分代: 典型的JVM根据generation(代 ...

  5. 342 Power of Four 4的幂

    给定一个整数 (32位有符整数型),请写出一个函数来检验它是否是4的幂.示例:当 num = 16 时 ,返回 true . 当 num = 5时,返回 false.问题进阶:你能不使用循环/递归来解 ...

  6. 深拷贝js

    JSON 最简单的一个 let obj = {} let newObj = JSON.parse(JSON.stringify(obj)) 弊端:这种方法无法拷贝function函数,undefine ...

  7. JDBC链接数据库步骤

    java中定义链接数据库的标准:JDBC 1.导包:不同数据库有不同的jdbc驱动包,而且jdbc驱动包和数据库版本必须对应 2.测试 3.写代码 try { 1.//加载JDBC驱动    Clas ...

  8. java_randomAccessFile类的使用

    RandomAccessFile java提供的对文件内容的访问,既可以读文件,也可以写文件.RandomAccessFile支持随机访问文件,可以访问文件的任意位置 (1)java文件模型 在硬盘上 ...

  9. Android中Adapter和Bridge模式理解和应用

    一 Adapter模式 意图: 将一个类的接口转换成客户希望的另外一个接口. Adapter模式使得原本由于接口不兼容而不能在一起工作的那些类可以在一起工作. 适用性: 使用一个已存在的类,而它的接口 ...

  10. 中望CAD VBA检测文件是否存在

    Option Explicit Private Declare Function PathFileExists Lib "shlwapi.dll" Alias "Path ...