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. 回文串--- Girls' research

    HDU   3294 Problem Description One day, sailormoon girls are so delighted that they intend to resear ...

  2. 启动Tomcat出现“Bad version number in .class file (unable to load class XXX)”解决

    转载自:http://blog.csdn.net/justdb/article/details/8067887 主要是jdk版本的不搭配 保证tomcat 的jdk版本 项目的jdk版本 还有就是编译 ...

  3. A -- HDU 4585 Shaolin

    Shaolin Time Limit: 1000 MS Memory Limit: 32768 KB 64-bit integer IO format: %I64d , %I64u Java clas ...

  4. [Architecture Design] 3-Layer基础架构

    [Architecture Design] 3-Layer基础架构 三层式体系结构 只要是软件从业人员,不管是不是本科系出身的,相信对于三层式体系结构一定都不陌生.在三层式体系结构中,将软件开发所产出 ...

  5. js封装tab标签页

    <html> <head> <title></title> <meta charset="UTF-8"> <sty ...

  6. 链接错误——无法解析的外部符号 ConvertStringToBST

    今天做COM组件时,编译之后,出现了一个数个编译错误:error LNK2019: 无法解析的外部符号 "wchar_t * __stdcall _com_util::ConvertStri ...

  7. SharePoint 2013 删除母版页报错“This file may not be moved, deleted, renamed, or otherwise edited”

    在使用SharePoint 2013母版页的时候,我复制了一个seattle.master页面,然后想重命名一下发现报错,删除也报错,spd.页面分别试过签入签出以后均报错,错误如下: 尝试找了一下错 ...

  8. iOS开发网络篇—NSURLConnection基本使用(一)

      一.NSURLConnection的常用类 (1)NSURL:请求地址 (2)NSURLRequest:封装一个请求,保存发给服务器的全部数据,包括一个NSURL对象,请求方法.请求头.请求体.. ...

  9. Monyer's Game 6~10关过关方法

    从Monyer's Game开通到现在,已经有50多人通关了.其中绝大部分人,不管是自己独立完成也好,参考别人也罢,都是自己一步一步过去的.像陆羽兄弟甚至已经为游戏做好了整个通关的教程,在此Monye ...

  10. 使用batch insert解决MySQL的insert吞吐量问题

    最近使用了一个非常简单易用的方法解决了业务上的一个insert吞吐量的问题,在此总结一下. 首先我们明确一下,insert吞吐量其实并不是指的IPS(insert per second),而是指的RP ...