搜索+剪枝 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 ...
随机推荐
- Titanium开发环境搭建第三个坑
Nodejs版本的问题 首先,如下引用:http://docs.appcelerator.com/titanium/latest/#!/guide/Installing_Node To run all ...
- R 网页数据爬虫1
For collecting and analyzing data. [启示]本处所分享的内容均是笔者从一些专业书籍中学习所得,也许会有一些自己使用过程中的技巧.心得.小经验一类的,但远比不上书中所讲 ...
- [WF] Quickstart Sample
[WF] Quickstart Sample 前言 Workflow Foundation(WF),总是给人一种很有用.可是却不知道怎么用的印象.这主要是因为前置的功课太多.要整合很多底层知识,才能完 ...
- 高性能文件缓存key-value存储—Memcached
1.高性能文件缓存key-value存储—Redis 2.ASP.NET HttpRuntime.Cache缓存类使用总结 备注:三篇博文结合阅读,简单理解并且使用,如果想深入学习,请多参考文章中给出 ...
- 最全的前端开发面试题及答案(js,css等等)
点击链接 https://github.com/HerbertKarajan/Fe-Interview-questions 我会不断的更新...... 若想自己留着,可以fork一下. 如果觉得不错, ...
- ssh无法登录linux服务器的解决办法
最近之前使用的一台linux服务器被长官重装系统了,导致ssh登录的时候出现如下错误: @@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@ ...
- 【读书笔记】iOS-Objective-C对C的扩展基础知识
一,Xcode的.m扩展名表示文件含有Objective-C代码,应由Objective-C编译器处理.C编译器处理名称以.c结尾的文件,而C++编译器处理.cpp文件.在Xcode中,所有这些编译工 ...
- 【读书笔记】iOS-本地文件和数据安全注意事项
一,程序文件的安全. 可通过将JavaScript源码时行混淆和加密,防止黑客轻易地阅读和篡改相关的逻辑,也可以防止自己的Web端与Native端的通讯协议泄露. 二,本地数据安全. 对于本地的重要数 ...
- Swift开发第一篇——异常处理及断言
本篇分两部分: 1.错误和异常处理 2.Swift 中的断言 1.错误和异常处理 在 OC 开发中,我们通常会将 error 置为 nil NSError *error; BOOL success = ...
- 初识 TextKit
iOS 7 的发布给开发者的案头带来了很多新工具.其中一个就是 TextKit.TextKit 由许多新的 UIKit 类组成,顾名思义,这些类就是用来处理文本的.在这里,我们将介绍 TextKit ...