poj 1416 Shredding Company (DFS)

POJ1416-Shredding Company 北大POJ1416-Shredding Company 解题报告+AC代码 立即下载

Shredding Company
Time Limit: 1000MS Memory Limit: 10000K
Total Submissions: 4595 Accepted: 2633

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


题目链接:http://poj.org/problem?id=1416


题目大意:两个不超过6位的整数n和m,把m拆分开,比如m为123,可以拆成三个数字1,2,3,也可以拆成数字12,3或1,23两个数字等,要求拆分出来的这些数字之和不大于n,求满足条件的最大数字和以及m拆分成哪些数字,如果不能得到答案,输出“error”,如果有多个答案,直接输出“rejeced”。


解题思路:枚举拆分成的小数字的长度,DFS递归过程中累加小数字的和,dfs1搜索满足条件的最大数字和,dfs2记录路径。个人觉得自己写得有些繁琐,用了两次dfs,实在想不出来怎样用一个dfs完成这两个步骤,因为递归过程中要寻找最优解,在不确定最优解的情况下,路径不方便记录。因此第一个dfs得到最优解,第二个dfs在搜索到最优解后原路返回记录路径。


代码如下:

#include <cstdio>
#include <cstring>
int ln,n,ans,cnt;     //ln是整数m的长度,这里用数组a[8]表示整数m
					  //ans是数字和的最优解,cnt是记录路径时用到的下标
int a[8],path[8],vis[1000000];   //path记录路径,vis用来判断是否存在多个最优解 
char s[8];
bool p;               //p标记是否搜索到满足条件的数字和
void dfs1(int st,int sum)   //搜索最优数字和
{
	if(st>=ln)              //此时整数m分割成的小数字已全部累加,得到一个数字和的解
	{
		p=true;
		vis[sum]++;         //标记是否存在多个该解值
		if(ans<sum)         //找最大解
			ans=sum;
		return ;
	}
	for(int i=0;i<ln;i++)    //枚举小数字的长度
	{
		if(st+i>=ln)
			break;
		int cur=0;
		for(int j=st;j<=st+i;j++)   //st枚举数组起始下标,把各位上的数构造成整数
			cur=(cur*10+a[j]);
		if(cur+sum>n)              //小数字和大于n,不满足条件,舍去。
			break;
		dfs1(st+i+1,cur+sum);      //起始下标跳过当前小数字,下一层构造后面一个小数字。
	}
}
void dfs2(int st,int sum)    //记录路径
{
	if(st>=ln)
	{
		if(sum==ans)         //找到目标值
			p=true;
		return;
	}
	for(int i=0;i<ln;i++)
	{
		if(st+i>=ln)
			break;
		int cur=0;
		for(int j=st;j<=st+i;j++)
			cur=(cur*10+a[j]);
		if(cur+sum>n)
			break;
		dfs2(st+i+1,sum+cur);
		if(p)
		{
			path[cnt++]=cur;   //由后往前记录路径
			return;
		}
	}
}
int main()
{
	while(scanf("%d",&n)!=EOF&&n)
	{
		memset(vis,0,sizeof(vis));
		p=false,ans=0,cnt=0;
		scanf("%s",s);
		ln=strlen(s);
		for(int i=0;i<ln;i++)
			a[i]=s[i]-'0';
		dfs1(0,0);
		if(!p)
			printf("error\n");
		else if(vis[ans]>1)
			printf("rejected\n");
		else
		{
			printf("%d",ans);
			p=false;
			dfs2(0,0);
			for(int i=cnt-1;i>=0;i--)
				printf(" %d",path[i] );
			printf("\n");
		}
	}
	return 0;
}


AIDD-人工智能药物设计-构建基于机器学习方法的在线预测平台高准确预测药物诱导骨毒性 在此基础上,提取多种药物分子特征,包括分子指纹(如MACCS、Morgan、RDKit、TopologicalTorsion、AtomPairs与Descriptors)及分子图谱,通过Transformer编码器与神经网络进行机器学习模型训练。最后,将训练好的模型部署为在线Web服务器(BoneToxPD),供用户快速预测药物的骨毒性。本研究不仅在骨毒性预测的准确性方面取得了突破,还在模型解释性方面提供了新的视角,通过注意力机制精准识别出影响骨毒性的关键化学基团,推动药物研发领域更安全、更高效的发展。 阅读详情

相关推荐

GEOPHYSICS 投稿避坑指南:从格式规范到双盲评审的实战解析

本文详细解析了GEOPHYSICS期刊投稿的全流程避坑指南,从格式规范、数据声明到双盲评审的实战策略。特别强调了数学公式排版、图表制作标准以及投稿系统的隐藏关卡,帮助作者避免常见错误,提升投稿成功率。

weixin_30606461的博客 239

POJ-1416 Shredding Company 解题报告

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 cont

岁月流风、、 1505

Photoshop实战技巧(9)——高效去除复杂水印的五种进阶方法

本文详细介绍了Photoshop中高效去除复杂水印的五种进阶方法,包括色阶通道爆破法、频率分离高阶应用、智能填充+手动修复组合拳等专业技巧。针对半透明水印、密集文字水印等复杂场景,提供分步操作指南和实战案例,帮助用户精准去除水印同时保留原始图像细节,大幅提升工作效率。

cake8的博客 966

Shredding Company (dfs)

  Problem 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...

a1046765624的博客 1563

2017–2024年中国10米/20米分辨率水稻种植分布数据集

摘要:本数据集提供2017-2024年中国23个省级行政区的水稻种植分布数据,包含单季稻和双季稻分类(GeoTIFF格式,WGS84坐标系)。5个省份分辨率为20米,其余为10米,基于Sentinel卫星数据和TWDTW方法生成。数据公开共享(7.99GB),使用需注明来源并引用相关文献(DOI:10.12199/nesdc.ecodb.rs.2025.012)。涉及学科包括遥感、生态学和地理学,研究成果需发送至国家生态科学数据中心备案。

国科大硕士在读,分享Python/GEE/Matlab/R专业代码实践,涵盖数据分析、算法开发与应用技巧等,以代码会友。 1394

冷门但实用:Python爬取微信公众号历史文章,突破微信反爬限制(2025有效)

2025年微信公众号爬取的核心是「冷门方法+指纹伪装」,而非常规接口破解,itchat-uos+Playwright组合是突破设备指纹的关键;登录态持久化、动态Token实时获取、频率严格控制是避免被封禁的三大核心;文章内容解析需解决2025新增的编码混淆问题,chardet+BeautifulSoup是冷门但有效的方案;合规是前提,爬取频率、数据用途需严格遵守平台/法律规则。

专注于Python爬虫开发,分享爬虫技巧、项目实战与反爬经验,使用Scrapy、BeautifulSoup等工具,解决数据抓取难题。 3799

POJ 1416 Shredding Company(简单DFS)

Shredding Company Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 4622   Accepted: 2654 Description You have just been put in charge of developing a new shre

叶孤心的专栏 856

手把手打通全流程:在华为昇腾310/Atlas 200DK上实现深度学习模型从移植到部署

通过以上步骤,你已成功在华为昇腾310 Atlas 200DK上完成深度学习模型的移植与推理。这一流程可复用到目标检测、语义分割等其他任务,快去尝试将自己的AI模型部署到昇腾设备上,打造属于你的边缘智能应用吧!上部署深度学习模型,这篇教程将带你走完从模型转换到推理运行的完整流程。无需担心技术壁垒,我们以通俗易懂的方式拆解每一步,让你轻松在昇腾嵌入式设备上实现AI应用。是专为边缘AI设计的高性能处理器,Atlas 200DK是其开发板形态,能高效运行图像分类、目标检测等深度学习任务。

FJN110的博客 185

poj1416

Shredding Company Time Limit:1000MS Memory Limit:10000K Total Submissions:2260 Accepted:1303 Description You have just been put in charge of developing a ne...

weixin_34177064的博客 208

【24E】SWT6652 wifi驱动加载失败问题分析及解决方案

修改驱动配置Kconfig。是由于内存分配失败导致。

wgl307293845的博客 223

MicroCap声学仿真介绍(一)

5-频率上下限选择,需要注意的是,第一个数字是上限,第二个数字是下限,如果填反了,会有报错提示;下三角可以选择“Auto”、“linear”、“log”和“list”,设置的是频率点之间的距离和频率点的数量。Component是用来选择部件的模块,如上图所示,声学仿真常用到的是“Electroacoustic Macros AC”、“Electroacoustic Macros TR”、“NXP Transducers”以及“NXP HD components”。3是声学部件,是声学仿真中常用的部件。

山无忧的博客 5139

[POJ 1416]Shredding Company[DFS]

题目链接:[POJ 1416]Shredding Company[DFS] 题意分析: 给出数字a和字符串b。问:字符串b能否切割后,使得每个数字相加,和最接近a但不超过a,如果有多组解,输出"rejected",无解输出"error",输出最接近的那个数和切割方案。 解题思路: 字符串长度最多6。那么就枚举当前位是否切割,用vector记录切割位置,p代表当前访问的位,sum代表当前方案

闲停 710

Android实战——白天黑夜模式设置

白天黑夜模式是一种动态的UI模式,根据当前时间或用户设置的偏好,在白天和黑夜之间进行切换。它通过调整应用程序的颜色、亮度和其他可视化元素来提供更加舒适和易读的用户界面。

小旭的专栏 5239

poj 1416 Shredding Company(DFS)

题目链接:http://poj.org/problem?id=1416 用DFS暴搜,枚举出所有状态,直至搜索完毕,计算最小值。 我是用map来标记出现次数,开数组太浪费空间了。 具体代码如下(变量有点多= =!): #include #include #include using namespace std; const int maxn=10; char tarNum[maxn],s

To be excellent! 307

win10 跳过U8环境检测的IE WebControls.rar

win10 安装u8的时候 提示IE WebControls 不通过,这个工具可以欺骗u8的检查。 跳过U8环境检测的IE WebControls。提示导入失败后,重新检测通过。

霍尼韦尔SoftMaster使用说明

霍尼韦尔SoftMaster使用说明

POJ.1416 Shredding Company (DFS)

POJ.1416 Shredding Company [从零开始DFS(8)]点我挑战题目题意分析给出一个目标数字n和一串数字序列s,要求切割序列s为若干数字段s1,s2,s3……sn,最后求出最大的 不超过目标数字n的 序列s切割方式,和这些字段的和。若对于最大的数字有多种切割方式,则输出rejected;若没有这样的切割方式,则输出error。直接对给出的数字串操作有些不方便,我们不妨想想挡板

Pengwill's Blog 502

OV9734 DATASHEET,完整规格书

OV9734完整规格书,包括电气参数,尺寸规格,寄存器配置表等

Poj 1416 Shredding Company 【dfs】

链接: http://poj.org/problem?id=1416 题意: 输入两个数t和num,问如何分割后的num的和小于t并且与t最接近 题解: 按着题目思路敲就好了 #include #include #include #include #include #include #include #include #include #include #in

t51645的博客 463

LinePhone3.2.7(android端最新版本) sip客户端

SIP客户端For android:LinePhone3.2.7(最新版本),官网地址:http://www.linphone.org/

poj1416 Shredding Company(Dfs)

DFS题目题目大意解题思路踩坑AC代码 题目 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 n

shuati2000的博客 286

深度优先搜索专题

这个专题是记录罗勇军《算法竞赛入门到进阶》DFS练习题的做题感悟。 poj 1416 "Shredding Company" 分岔路:以“12346”为例,可能的切割点:0号位置、1号位置、2号位置、3号位置、4号位置共计5个。因此,每次深度优先就以是否在i号位置进行切割为选择。用bool cut[ ]数组来记录是否切割。 终止:当5个位置的切割情况都已经确定后就终止。 注意点:50 12346这个测试数据会出现这两种切割: 0、1、3号切割其余位置不切割得 43 1 2 34 6; 0、1、3、

华师少女的梦的博客 210

DFS学习笔记-POJ1416 Shredding Company

关于初学者对DFS的一些浅要理解,以poj1416为例

ex_voda的博客 727

poj-1416 Shredding Company DFS

题意:有一个碎纸机,给你一个最多有6位数的纸片,需要让你将纸片切开,使得切开的纸片的数值和最接近或等于target值。输入target和纸片,输出最接近target的值和纸片的切法。特殊规则:如果纸片的值和target相等,那么不用切开。如果所有情况下都不存在小于target的切法,那么输出error。如果最终的答案有多种,那么输出rejected。         思路:DFS搜索所有切开纸片

心の コード 631

POJ - 1416 Shredding Company(DFS入门)

题目:https://vjudge.net/problem/POJ-1416 哇,对不起是我太菜了,DFS是得好好利用递归的算法,随说是暴力但也是很需要思维的…最近写题特别容易卡在各种小细节,无语了…比如这次 如果m<=t,我居然输出t,m!? 注意一下 101 100这类m<t的数据即可,其它数据只要能过样例就没问题。 思路来源:https://blog.csdn.net/u0127...

TK_wang_的博客 293

DFS/POJ 1416 Shredding Company

1 #include<cstdio> 2 #include<cstring> 3 using namespace std; 4 int mmax,sum; 5 char s[10]; 6 bool v[10],way[10]; 7 bool rejected; 8 void dfs(int dep,int value,int be...

diyoucang7967的博客 127

POJ-1416 Shredding Company(DFS)

题目链接:点击打开链接 大致题意: 公司现在要发明一种新的碎纸机,要求新的碎纸机能够把纸条上的数字切成最接近而不超过target值。比如,target的值是50,而纸条上的数字是12346,应该把数字切成四部分,分别是1、2、34、6。因为这样所得到的和43 (= 1 + 2 + 34 + 6) 是所有可能中最接近而不超过50的。(比如1, 23, 4, 和6 就不可以,因为它们的和不

JOKER_SAMA的博客 1543

POJ 1416 Shredding Company(DFS)

题意:将num切开然后求和,找到一个小于等于t且最接近t的值,如果得到这个值的方法是唯一的则输出切num的方法,否则输出rejected。如果没有一个方法的数是小于等于t则输出error。 题记:dfs去搜索所有切的方法。注意记录切的路径。 #include<iostream> #include<queue> #include<cstring> #include<string> #include<algorithm> using namespace

moyangxian 228

poj -1416-Shredding Company-DFS

题目翻译: 公司现在要发明一种新的碎纸机,要求新的碎纸机能够把纸条上的数字切成最接近而不超过target值。比如,target的值是50,而纸条上的数字是12346,应该把数字切成四部分,分别是1、2、34、6。因为这样所得到的和43 (= 1 + 2 + 34 + 6) 是所有可能中最接近而不超过50的。(比如1, 23, 4, 和6 就不可以,因为它们的和不如43接近50,而12, 34,

命运在指间 636

poj-1416 Shredding Company 按位DFS+入门剪枝

题目链接poj-1416 Shredding Company题目Shredding CompanyTime Limit: 1000MS Memory Limit: 10000KTotal Submissions: 6668 Accepted: 3608DescriptionYou have just been put in charge of developing a new shredder f...

iwts 521
上一篇: poj 2676 Sudoku (基础DFS)
下一篇: poj 1129 Channel Allocation (DFS 图的染色问题)
雨狮子
博客等级 码龄12年 24粉丝 185原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值