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

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

题目大意如下:给定你一个目标数字以及一张数字纸片,需要设计一种切割纸片的程序,使各个切片上数字之和加起来最接近目标数字且不超过它。额外地,有二个条件(话说那个相等条件似乎没用):其一,如果有多个得到最优解的切割方案,则输出"rejected";其二,若求不出合法解,那么输出"error"。

题解如下:由于原题描述用的是中式英语看不大懂,只好抄程序理解。主要思路是预处理求出任意区间代表的数字,从左到右一个一个地按顺序搜索区间,并记录路径,最后递归求解。详细的看我的程序注释吧。

15809294

  ksq2013 1416 Accepted 700K 16MS G++ 1510B 2016-07-25 21:11:48
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
bool more;
pair<int,int>pre[10][10],ans;
int lim,ln,d[10],num[10][10],minn;
void Init(char *p)
{
ln=strlen(p);
for(int i=0;i<ln;i++)
d[i]=p[i]-'0';
}
void Prep()//预处理程序;
{
memset(num,0,sizeof(num));
for(int i=0;i<ln;i++)
for(int j=i;j<ln;j++)
for(int k=i;k<=j;k++)
num[i][j]=num[i][j]*10+d[k];
}
bool dfs(int l,int r,int cnt)
{
if(r>=ln){
if(cnt>minn&&cnt<=lim){
more=false;
ans.first=l;
ans.second=r-1;//对应下方output函数的递归结束条件,非常巧妙的方法,避开了'0'这一特殊数字;
minn=cnt;
return true;
}
else if(cnt==minn)more=true;//多解了!;
return false;
}
bool adv=false;//advanced,记录该搜索子树的节点是否有解;
for(int i=1;i<=ln-r;i++){//看到i<=ln-r可知,i这里枚举的是准备切割区间的长度,例如(0,0)长度为1;
int now=r+i-1;
if(dfs(r,r+i,cnt+num[r][now])){
pre[r][now].first=l;
pre[r][now].second=r-1;
adv=true;
}
}
return adv;
}
void output(int l,int r)
{
if(l<0&&r<0)return;//对应上方dfs中的pre的存储方式,存的是r-1,所以最先前的区间的pre存的l和r都是-1,成为结束递归的条件;
output(pre[l][r].first,pre[l][r].second);
printf("%d ",num[l][r]);//直接按对应的l和r输出区间代表的数字;
}
int main()
{
char p[10];
while(~scanf("%d%s",&lim,p)){
if(!lim)break;
Init(p);
Prep();
more=false,minn=-1;
dfs(-1,0,0);
if(more){
puts("rejected");
continue;
}
if(minn==-1){
puts("error");
continue;
}
printf("%d ",minn);
output(ans.first,ans.second);//我试着用pair作为函数变量,但是不行,因为pre就是用l和r存的,所以只能代入first和second;
putchar('\n');
}
return 0;
}

poj1416 Shredding Company的更多相关文章

  1. POJ1416——Shredding Company(DFS)

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

  2. POJ1416 Shredding Company(dfs)

    题目链接. 分析: 这题从早上调到现在.也不算太麻烦,细节吧. 每个数字都只有两种状态,加入前一序列和不加入前一序列.DFS枚举. #include <iostream> #include ...

  3. 搜索+剪枝 POJ 1416 Shredding Company

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

  4. Shredding Company

    Shredding Company Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4653 Accepted: 2675 Des ...

  5. Shredding Company(dfs)

    Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 3519   Accepted: 2009 Description You h ...

  6. POJ 1416 Shredding Company【dfs入门】

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

  7. POJ 1416 Shredding Company 回溯搜索 DFS

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

  8. Shredding Company (hdu 1539 dfs)

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

  9. POJ 1416:Shredding Company

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

随机推荐

  1. arcgis批量处理mxd定义服务中的路径

     >>> from arcpy import env... env.workspace=r"c:\165mxd"... out = r"c:\166mx ...

  2. Web自动化测试 Selenium 2/3

    TesNG和Selenium集成使用 TestNG 是一个设计用来简化广泛的测试需求的测试框架,从单元测试(隔 离测试一个类)到集成测试(测试由有多个类多个包甚至多个外部框架组成的整 个系统,例如运用 ...

  3. 【转】C++标准库和标准模板库

    C++强大的功能来源于其丰富的类库及库函数资源.C++标准库的内容总共在50个标准头文件中定义.在C++开发中,要尽可能地利用标准库完成.这样做的直接好处包括:(1)成本:已经作为标准提供,何苦再花费 ...

  4. [Android]竖直滑动选择器WheelView的实现

    以下内容为原创,转载请注明: 来自天天博客:http://www.cnblogs.com/tiantianbyconan/p/3819304.html 公司项目中有这么一个需求,所以需要自己实现下.效 ...

  5. 操作系统开发系列—12.d.扩充内核 ●

    现在把esp.GDT等内容放进内核中,我们现在可以用C语言了,只要能用C,我们就避免用汇编. 下面看切换堆栈和GDT的关键代码: ; 导入函数 extern cstart ; 导入全局变量 exter ...

  6. swift 2.2 语法 (中)

    前言: 1.此文中的语法会根据Swift的升级变动而更新. 2.如果需要请移步 -> swift2.2 语法(上).swift 2.2语法(下) 函数 和C语言一样,swift也有函数,性质和我 ...

  7. android 浅谈Aidl 通讯机制

    服务端: 首先是编写一个aidl文件,注意AIDL只支持方法,不能定义静态成员,并且方法也不能有类似public等的修饰符:AIDL运行方法有任何类型的参数和返回值,在java的类型中,以下的类型使用 ...

  8. TCP连接状态与2MSL等待时间

    1 连接状态图 2 建立连接:三次握手,不使用DNS和使用DNS 3 关闭连接-四次握手 连接双方任何一方调用close()后,连接的两个传输方向都关闭,不能再发送数据了.如果一方调用shutdown ...

  9. js去掉字符串前后空格的五种方法

    转载 :http://www.2cto.com/kf/201204/125943.html 第一种:循环检查替换[javascript]//供使用者调用  function trim(s){  ret ...

  10. 【转】OpenStack和Docker、ServerLess能不能决定云计算胜负吗?

    还记得在十多年前,SaaS鼻祖SalesForce喊出的口号『No Software』吗?SalesForce在这个口号声中开创了SaaS行业,并成为当今市值460亿美元的SaaS之王.今天谈谈『No ...