POJ 1416 Shredding Company 回溯搜索 DFS
| Time Limit: 1000MS | Memory Limit: 10000K | |
| Total Submissions: 6173 | Accepted: 3361 |
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
Source
#include<iostream>
#include<cstdio>
#include<cstring>
#include<sstream>
#include<algorithm>
#include<queue>
#include<vector>
#include<cmath>
#include<map>
#include<stack>
#include<set>
#include<fstream>
#include<memory>
#include<list>
#include<string>
using namespace std;
typedef long long LL;
typedef unsigned long long ULL;
#define MAXN 7
#define INF 1000000009
/*
给一个数字和一个数组,把数组分成多段,输出生成结果最接近目标数字的解
回溯法搜索,两种情况:分割该点作为新的起点 和 将该点接在前一段后面
*/
int aim,n,k,cntsame;
char s[MAXN];
vector<vector<char> > ans;
void solve(int x,int cnt,vector<vector<char> > tmp)//在当前数组中遍历的位置,取的段数,临时数组
{
if (x == n)
{
int sum = ;
for (int i = ; i < tmp.size(); i++)
{
int tt = ;
for (int j = ; j < tmp[i].size(); j++)
{
tt = tt * + (tmp[i][j] - '');
if (tt > aim)
return;
}
sum += tt;
if (sum > aim)
return;
}
if (aim - sum < k)
{
k = aim - sum;
cntsame = ;
ans = tmp;
}
else if (aim - sum == k)
{
cntsame++;
}
return;
}
tmp[cnt].push_back(s[x]);
solve(x + ,cnt,tmp);
if (x)
{
tmp[cnt].pop_back();
tmp[++cnt].push_back(s[x]);
solve(x + , cnt, tmp);
}
}
void print()
{
if (cntsame > )
printf("rejected\n");
else if (cntsame == )
printf("error\n");
else
{
printf("%d", aim-k);
for (int i = ; i < ans.size(); i++)
{
printf(" ");
for (int j = ; j < ans[i].size(); j++)
{
printf("%c", ans[i][j]);
}
}
printf("\n");
}
}
int main()
{
while (scanf("%d%s", &aim, s), s[] != ''&&aim != )
{
int j = atoi(s);
if (j == aim)
{
printf("%d %d\n", aim, aim);
continue;
} cntsame = ;
k = INF;
n = strlen(s);
vector<vector<char> > a;
a.resize();
solve(, , a);
print();
}
return ;
}
POJ 1416 Shredding Company 回溯搜索 DFS的更多相关文章
- 搜索+剪枝 POJ 1416 Shredding Company
POJ 1416 Shredding Company Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 5231 Accep ...
- 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 )
我的dfs真的好虚啊……,又是看的别人的博客做的 题目== 题目:http://poj.org/problem?id=1416 题意:给你两个数n,m;n表示最大数,m则是需要切割的数. 切割m,使得 ...
- 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
题目: http://poj.org/problem?id=1416 又16ms 1A了,这人品... #include <stdio.h> #include <string.h&g ...
- Shredding Company (hdu 1539 dfs)
Shredding Company Time Limit: 5000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others ...
- POJ - 1321 棋盘问题 简单搜索 dfs 格子
点这里去看题 思路:本题的难点在k<n的情况,所以我们可以另dfs中的两个参数分别代表起始行和待放棋子个数(待放棋子只能放在起始行后面的行),然后用一个c[8]来表示每一列放旗子的情况来判断列不 ...
- POJ 1416:Shredding Company
Shredding Company Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 4713 Accepted: 2714 ...
- POJ1416——Shredding Company(DFS)
Shredding Company DescriptionYou have just been put in charge of developing a new shredder for the S ...
随机推荐
- 使用display:flex;实现垂直水平居中
body,div{margin:0px;padding:0px;} .flex-container{display:flex;height:300px;background-color:#ddd;ju ...
- selenium3 + python - 异常处理截图 screenshot
一.截图方法 1.get_screenshot_as_file(self, filename) --这个方法是获取当前window的截图,出现IOError时候返回False,截图成功返回True. ...
- redis过期策略和内存淘汰机制
目录 常见的删除策略 redis使用的过期策略:定期删除+惰性删除 定期删除 惰性删除 为什么要采用定期删除+惰性删除2种策略呢? redis内存淘汰机制 常见的删除策略 1.定时删除:在设置键的过期 ...
- hihocode 编程练习赛17
1. f1 score 首先了解f1 score的计算方法, 我记得是学信息检索知道的, 然后简单处理就行. 由于我写的比较麻烦, 中间处理过程引入了一些除数为0的情况,导致错了很多次.其实是很简单的 ...
- [转]Linux下chkconfig命令详解
转自:http://www.cnblogs.com/panjun-Donet/archive/2010/08/10/1796873.html chkconfig命令主要用来更新(启动或停止)和查询系统 ...
- 为什么现在改用int.TryParse了
以前一直用 int.Parse(x)或者 Convert.ToInt64(x),后来项目中发现如果x变量的值为null是,就报错了,哪怕我这样写 int.Parse(x=x??"0" ...
- 02--Tomcat总体结构分析一
注:此文章大部分参考大神文档,并且结合自身理解,补充了其他相关知识,谢绝转载. 大神原文地址链接:http://www.ibm.com/developerworks/cn/java/j-lo ...
- ORA-02068,ORA-03135错误解决方法
今天查看了下ERP DB服务器 alter_<SID>.log日志,发现有个错误 Sat Sep 14 14:49:42 CST 2013 Error 2068 trapped in 2P ...
- echarts交叉关系图一
想要做一个公司-人员关系图,官网echarts图graph webkit dep 稍微改了一下, 也是有点恶心自己,调了一个数据最多的去改,如果正好有人需要就不用去改了 说明:此图没有坐标,可以设置图 ...
- html5——动画
基本介绍 /*执行函数gun,执行时间,重复执行,反向执行,匀速执行,延迟执行时间*/ animation: gun 4s infinite alternate linear 5s; 动画序列 1.g ...