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. jdbcTemplate queryForObject 查询 结果集 数量

    1.组织sql语句, 查询参数 数组, 设置返回类型 public int countByCondtion(String title, int mediaType, String currentSta ...

  2. C#中的接口实现多态

    我们都知道虚方法实现多态,抽象方法实现多态等,我们今天来看看如何使用接口实现多态 1.首先我们先要来了解了解什么是接口,它存在的意识 01.接口就是为了约束方法的格式(参数和返回值类型)而存在的 02 ...

  3. ahjesus mongodb指定到数据盘连接不上的解决方案

    关于配置路径指定到数据盘会出现连接不上的情况 我发现是因为数据盘权限不足引起的,目前没找到治本的方法 有个治标的方法就是设置数据盘的权限和用户 sudo chmod 777 * -R  /path/d ...

  4. C++ 面向对象的三个特点--多态性(二)

    运算符重载 运算符重载,就是对已有的运算符重新进行定义,赋予其另一种功能,以适应不同的数据类型. 类外部的运算符重载 首先,我们通过一个例子来说明为什么要有运算符重载. // Complex.h cl ...

  5. Stanford机器学习课程(Andrew Ng)

    斯坦福大学机器学习视频教程(附学习笔记,potplay播放器,PPT等资料),很具有学习价值. 链接:http://mooc.guokr.com/note/16274/

  6. Mybatis学习记录(八)----Mybatis整合Spring

    1.整合思路 需要spring通过单例方式管理SqlSessionFactory. spring和mybatis整合生成代理对象,使用SqlSessionFactory创建SqlSession.(sp ...

  7. php 安全过滤函数代码

    php 安全过滤函数代码,防止用户恶意输入内容. //安全过滤输入[jb] function check_str($string, $isurl = false) { $string = preg_r ...

  8. ubuntu下nagios配置

    参考文献: http://www.cnblogs.com/mchina/archive/2013/02/20/2883404.html http://my.oschina.net/duangr/blo ...

  9. CentOS6.5上编译OpenJDK7源码

    本文地址:http://www.cnblogs.com/wuyudong/p/build-openjdk7.html,转载请注明源地址. 采用开源的OpenJDK版本,获取其源码的方式有两种: 通Me ...

  10. WPF Caliburn.Micro ListView 批量删除 新方法.高效的

    上一片我做的批量删除,是更具ListView的选项改变事件,然后放到一个全局变量里面,缺点已经说了.这次又找到一个好的方法.和大家分享一下.这次我将删除按钮的click事件里面的参数绑定为ListVi ...