搜索+剪枝 POJ 1416 Shredding Company
POJ 1416 Shredding Company
Time Limit: 1000MS | Memory Limit: 10000K | |
Total Submissions: 5231 | Accepted: 2964 |
Description
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
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
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的更多相关文章
- POJ 1416 Shredding Company【dfs入门】
题目传送门:http://poj.org/problem?id=1416 Shredding Company Time Limit: 1000MS Memory Limit: 10000K Tot ...
- POJ 1416 Shredding Company 回溯搜索 DFS
Shredding Company Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6173 Accepted: 3361 ...
- OpenJudge 2803 碎纸机 / Poj 1416 Shredding Company
1.链接地址: http://poj.org/problem?id=1416 http://bailian.openjudge.cn/practice/2803 2.题目: 总时间限制: 1000ms ...
- poj 1416 Shredding Company( dfs )
我的dfs真的好虚啊……,又是看的别人的博客做的 题目== 题目:http://poj.org/problem?id=1416 题意:给你两个数n,m;n表示最大数,m则是需要切割的数. 切割m,使得 ...
- POJ 1416 Shredding Company
题目: http://poj.org/problem?id=1416 又16ms 1A了,这人品... #include <stdio.h> #include <string.h&g ...
- 搜索+剪枝——POJ 1011 Sticks
搜索+剪枝--POJ 1011 Sticks 博客分类: 算法 非常经典的搜索题目,第一次做还是暑假集训的时候,前天又把它翻了出来 本来是想找点手感的,不想在原先思路的基础上,竟把它做出来了而且还是0 ...
- 搜索 + 剪枝 --- POJ 1101 : Sticks
Sticks Problem's Link: http://poj.org/problem?id=1011 Mean: http://poj.org/problem?id=1011&lan ...
- POJ 1416:Shredding Company
Shredding Company Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4713 Accepted: 2714 ...
- poj1416 Shredding Company
Shredding Company Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5379 Accepted: 3023 ...
随机推荐
- KM算法
链接: http://blog.csdn.net/lvshubao1314/article/details/41702291
- Angularjs,WebAPI 搭建一个简易权限管理系统 —— Angularjs名词与概念(一)
目录 前言 Angularjs名词与概念 Angularjs 基本功能演示 系统业务与实现 WebAPI项目主体结构 Angularjs 前端主体结构 2. 前言 Angularjs开发CRUD类型的 ...
- windows临界区
临界区: 临界区是一种轻量级机制,在某一时间内只允许一个线程执行某个给定代码段.通常在多线程修改全局数据时会使用临界区.事件.信号量也用于多线程同步,但临界区与它们不同,并不总是执行向内核模式的切换, ...
- Android5.0新特性——新增的Widget(Widget)
新增的Widget RecyclerView RecyclerView是ListView的升级版,它具备了更好的性能,且更容易使用.和ListView一样,RecyclerView是用来显示大量数据的 ...
- 使用openssl创建自签名证书及部署到IIS教程
概要 本文讲解三个部分:1. 创建自签名证书2. 创建自己的证书颁发机构3. 以及如何配置IIS 创建自签名证书 首先,创建一个私钥文件: openssl genrsa -out myselfsign ...
- React 初学整理
1,通过createElement创建元素 HELLO Word ps:切记组建名称首字母大写 2,虚拟DOM 在虚拟DOM上操作 通过render来渲染真是DOM 3,JSX JSX 是对JS的语法 ...
- Unable to start activity ComponentInfo{com.first/com.first.Game}
原因一: xxx的错误,若为R.layout.main 那么应该是main.xml文件中的标签 使用错误,最常见的而且编译器不会提示的错误就是 android:name 和android:id 两者 ...
- Xcode 编译运行报错: CpResource /user/xxxx/ xxx Directory not empty
之前遇到过相同的问题,总是记吃不记打,踩过的坑后面还会踩进去... 仅以次标记加深一下印象 错误特征RT 确认该类型错误是library或frameWork的search路径问题 首先找到编译错误的路 ...
- 全球最低功耗蓝牙单芯片DA14580的硬件架构和低功耗
号称全球最低功耗蓝牙单芯片DA14580在可穿戴市场.健康医疗.ibeacon定位等市场得到广泛的应用,但是因为其较为封闭的技术/资料支持导致开发人员有较高的技术门槛,网络上也极少看到有关DA1458 ...
- IPv4中IP地址分类
分三个部分进行讲述: 1 五类IP 2 特殊用途的IP 3 私有IP 1 五类IP IPV4简单粗暴地把IP地址分为五类.分类方法如下图所示: 也就是说,分为以下几类: A: 0.0.0.0-127. ...