POJ 1416 Shredding Company

Time Limit: 1000MS   Memory Limit: 10000K
Total Submissions: 5231   Accepted: 2964

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
 /*这个题目比较难的地方就是记录是如何划分的,我这里用了一个path,path的位数表示划分成了几份,每一位path表示的是这一划分了几个*/
#include<iostream>
#include<cstdlib>
#include<cmath>
using namespace std;
#include<cstdio>
#define N 10
#include<cstring>
int tar;
int visit[],rel,path=;
char paper[N];
int sum(const char *s)/*计算字符串表示的数*/
{
int len=strlen(s+);
int ans=;
for(int i=;i<=len;++i)
ans=ans*+s[i]-'';
return ans;
}
int get_ws(int n)/*取出n的位数,因为n顶多6位数*/
{
if(n<) return ;
if(n<) return ;
if(n<) return ;
if(n<) return ;
if(n<) return ;
return ;
}
int get_pre(int n,int k)/*取出n的前k位*/
{
int ws=get_ws(n);
return n/(int)pow(10.0,ws-k);/*这里pow中要用10.0,因为会有误差,比如pow(10,2)==99*/
}
void dfs(const char* s,int p,int sum1,int len)
{
if(sum1>tar) return;/*剪枝*/
if(len==)/*边界*//
{
visit[sum1]++;
if(sum1>rel&&sum1<=tar)
{
path=p;
rel=sum1;
}
return;
}
for(int i=;i<=len;++i)
{
char a[]={},b[]={};
int j,t;
for(j=;j<=i;++j)
a[j]=s[j];
a[j+]='\0';
for(t=;j<=len;++j,++t)
b[t]=s[j];
b[++t]='\0';
int now=sum(a);/*把a分为一份,枚举a的长度,然后递归分b*/
p=p*+i;/*记录划分方式*/
dfs(b,p,sum1+now,strlen(b+));
p/=;/*回溯*/
}
}
int main()
{
while(scanf("%d%s",&tar,paper+)==)
{
int now=sum(paper);
if(tar==&&now==) break;
if(tar==now)
{
printf("%d %d\n",tar,tar);
continue;
}
int len=strlen(paper+);
int su=;
for(int i=;i<=len;++i)
su+=paper[i]-'';
if(su>tar)/*如果最小的划分方式都大于tar,那说明是达不到的*/
{
printf("error\n");
continue;
}
dfs(paper,,,len);
if(visit[rel]>)/*到达超过一次,最好用数组统计,不会错,而且空间够*/
{
printf("rejected\n");
}
else{
printf("%d ",rel);
int i=;
while(path)/*输出划分方式*/
{
int l=get_pre(path,);
int k=l+i-;
for(;i<=k;++i)
printf("%d",paper[i]-'');
printf(" ");
int ws=get_ws(path);
path-=l*(int)pow(10.0,ws-);
}
printf("\n");
}
rel=;path=;tar=;
memset(paper,,sizeof(paper));
memset(visit,,sizeof(visit));
/*别忘记初始化*/
}
return ;
}

搜索+剪枝 POJ 1416 Shredding Company的更多相关文章

  1. POJ 1416 Shredding Company【dfs入门】

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

  2. POJ 1416 Shredding Company 回溯搜索 DFS

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

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

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

  4. poj 1416 Shredding Company( dfs )

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

  5. POJ 1416 Shredding Company

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

  6. 搜索+剪枝——POJ 1011 Sticks

    搜索+剪枝--POJ 1011 Sticks 博客分类: 算法 非常经典的搜索题目,第一次做还是暑假集训的时候,前天又把它翻了出来 本来是想找点手感的,不想在原先思路的基础上,竟把它做出来了而且还是0 ...

  7. 搜索 + 剪枝 --- POJ 1101 : Sticks

    Sticks Problem's Link:   http://poj.org/problem?id=1011 Mean: http://poj.org/problem?id=1011&lan ...

  8. POJ 1416:Shredding Company

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

  9. poj1416 Shredding Company

    Shredding Company Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 5379   Accepted: 3023 ...

随机推荐

  1. java中的代码块执行顺序

    /* 代码块:在Java中,使用{}括起来的代码被称为代码块. 根据其位置和声明的不同,可以分为 局部代码块:局部位置,用于限定变量的生命周期. 构造代码块:在类中的成员位置,用{}括起来的代码.每次 ...

  2. 养只爬虫当宠物(Node.js爬虫爬取58同城租房信息)

    先上一个源代码吧. https://github.com/answershuto/Rental 欢迎指导交流. 效果图 搭建Node.js环境及启动服务 安装node以及npm,用express模块启 ...

  3. 对于Oracle analyze table的使用总结 . 对于Oracle analyze table的使用总结 .

    对于Oracle analyze table的使用总结 . 对于Oracle analyze table的使用总结 . analyze table 一般可以指定分析: 表,所有字段,所有索引字段,所有 ...

  4. Mac下利用(xcode)安装git

    Mac下利用(xcode)安装git 一.AppStore 最安全途径:搜索下载Xcode,(需要AppleID). 其他:直接百度Xcode下载. 二.Xcode 打开Xcode-->Pref ...

  5. 错误:StrictMode $ AndroidBlockGuardPolicy.onNetwork

    you have to insert 2 lines "StrictMode" on MainActivity Class, example's below: 在onCreate( ...

  6. ListView嵌套出现的问题

    项目中一个列表子项中也需要用到列表,这就不由得使我想到ListView的嵌套,其实这个东西想想也只是复杂了一点,并没有什么难的地方,可是却依然在这里狠狠滴栽个跟头.问题出在子列表动态展开的操作上.可能 ...

  7. 我们需要专职的QA吗?

    [ 引用评论里的一句话:hurt but true  抛开作者某些偏激的想法外,作者暴露出来的问题还是需要测试思考的: 1.TestCase,TestData,TestConfiguration 没有 ...

  8. jhljx跑跑跑(找规律)

    题目来源:https://biancheng.love/contest/41/problem/D/index jhljx跑跑跑 题目描述 数学不好的jhljx又在和别人打牌,他们一共m人每人n张牌,牌 ...

  9. Android Design Support Library——Snackbar

    Snackbar是一个轻量级控件,它可以很方便的提供消息的提示和动作反馈,类似于Toast.Snackbar包括一段文字信息与一个可选的操作按钮,超时自动隐藏,也可以通过滑动来删除.效果如下所示: S ...

  10. JavaScript Patterns 3.3 Patterns for Enforcing new

    When your constructor has something like  this.member and you invoke the constructor without  new,  ...