POJ-1416 Shredding Company 解题报告

POJ1416-Shredding Company 北大POJ1416-Shredding Company 解题报告+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 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


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

       解法类型:DFS

       解题思路:这题的数据量很小,因此可以用DFS直接模拟所有的可能结果,然后依次比较即可。具体就是用个标记数组C[],初始化为零,然后赋C[0]为1。接下来就是DFS了,分别取C[cur]为前一个数的值和前一个数的值加一搜索就可以了,这就模拟了当前这个数和前一个数组成一个数和当前这个数不和前一个数组成一个数的情况,即所有情况。

       算法实现:

//STATUS:C++_AC_0MS_164K
#include<stdio.h>
#include<memory.h>
#include<string.h>
const int MAXN=10;
int DFS(int cur);
int sum(int *c);
char tnum[MAXN];
int n,num[MAXN],spa[MAXN],C[MAXN],tar,max,tot,line,ma;
int main()
{
	freopen("in.txt","r",stdin);
	int i,k;
	while(scanf("%d%s",&tar,tnum)&&tar)
	{
		memset(C,0,sizeof(C)),C[0]=1;  //初始化,下面也是
		tot=0,line=strlen(tnum);  //tot=0,是为了考虑重复的情况
		max=0x80000000;
		for(i=0;i<line;i++)  //转化为整形
			num[i]=tnum[i]-'0';
		for(i=line-1,k=1,n=0;i>=0;i--,k*=10)
			n+=num[i]*k;

		if(tar==n)printf("%d %d\n",tar,tar);    //分四种情况输出
		else {
			DFS(1);
			if(tot>1)printf("rejected\n");
			else if(max==0x80000000)printf("error\n");
			else {
				printf("%d",max);
				for(i=ma;i>=0;i--)
					printf(" %d",spa[i]);
				putchar('\n');
			}
		}	
	}
	return 0;
}

int DFS(int cur)
{
	if(cur>line)return 1;  //搜索完成
	int s,i,j,t,k;

	s=sum(C);
	if(s==max)tot++;   //rejected
	if(s>max&&s<=tar){
     	tot=0;
    	max=s;
	for(i=line-1,j=0;i>=0;j++){
	for(t=C[i],k=1,spa[j]=0;C[i]==t;i--,k*=10)
     	spa[j]+=num[i]*k;
	}
	ma=j-1;  //spa[]的长度	
	}

	i=C[cur-1];  //和上一个数组成一个数
	C[cur]=i;
	DFS(cur+1);

	C[cur]=i+1;   //不和上一个数组成一个数
	DFS(cur+1);
	C[cur]=0;

	return 1;  //搜索完毕
}

int sum(int *c)  //计算可能的和
{
	int i,k,s1,s2=0,t;
	for(i=line-1;i>=0;){
		for(s1=0,t=c[i],k=1;c[i]==t;i--,k*=10)
			s1+=k*num[i];
		s2+=s1;
	}
	return s2;
}
       中间有部分代码还可以这样优化:
	//这个减少了不必要的重复运算,但POJ跑出来居然16MS,汗。。。so,0MS和16MS还是很看RP的。~
	if(C[line-1]){
		s=sum(C);
		if(s==max)tot++;   //rejected
		if(s>max&&s<=tar){
     		tot=1;   //这里就不需要考虑重复的情况了,注意,前面初始化的tot也要改为tot=1;
    		max=s;
		for(i=line-1,j=0;i>=0;j++){
		for(t=C[i],k=1,spa[j]=0;C[i]==t;i--,k*=10)
     		spa[j]+=num[i]*k;
		}
		ma=j-1;  //spa[]的长度	
		}
	}

【工控基础】UVW平台踩坑 1 每个轴按顺时针或逆时针走一个距离,就是旋转。 2 图中W动,U轴V轴不动,就是X方向的平移 3 图中U轴V轴同时动,X不动,就是Y方向的平移。 阅读详情

相关推荐

2025东方财富笔试考什么?cata能力测评攻略|答题技巧真题分享

4月10日-4月20日 专业笔试(行测+金融知识混合双打) 4月25日-5月10日 面试车轮战(AI面+半结构化+压力面) 5月15日起 Offer发放(谈薪必杀技:对标同花顺/雪球)这里不仅是金融极客的乐园,更是**“年薪30万管培生计划”**的主战场——你将用代码重构K线,用算法预判市场,成为资本市场的“数字先知”。东方财富,中国金融信息服务领域的“隐形冠军”,以**“金融数据+互联网”双擎驱动**,打造了覆盖证券、基金、期货的全生态交易平台。:• “我总坚持自己的方案”(HR:团队毒药!

Dgntsj的博客 3267

DFS学习笔记-POJ1416 Shredding Company

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

ex_voda的博客 727

C#读取CAD文件(dwg/dxf)并处理

项目要求识别CAD图纸(图纸内容与现实事物比例是1:1)中的内容,并提取出一些关键信息。这里的CAD图纸是指CAD文件,包含多种格式,dwg/dxf/dwf/stl/pdf等等。识别其中内容,主要指文件中的图元信息(也叫实体Entity),指线段、圆弧、矩形等图形元素。得到图元基础信息(比如长度、坐标、粗细)后,进一步做处理(如得到交点、得到线段间间隔、圆弧与线段切点等)。听起来有点难,因为作为软件工程师,对CAD相关事物并不了解,它是未知的;

三千幻想乡 1万+

poj1416的题解

#include<iostream> #include<cmath> #include <cstring> using namespace std; int getlen(int n) //得到n的位长度 { if (n < 10) return 1; else if (n < 100) retu...

xiaozhou163的博客 295

四阶龙格库塔法求解一次常微分方程组(python实现)

之前在博客发布了关于使用四阶龙格库塔方法求解一次常微分方程组的文章,由于代码缺少具体的验证,部分朋友可能存在疑问,因此这里打算再重新写一篇博客来验证一下程序的正确性,另外,这里是使用python语言来实现的。

HelloWorldTM的博客 8552

同花顺 python量化交易_开启量化第一步!同花顺iFinD数据接口免费版简易操作教程...

来源:雪球App,作者: 同花顺-iFinD,(https://xueqiu.com/6706494343/141951051)金融市场波动频繁,投资往往会夹杂非理性的情绪。而量化交易,旨在以先进的数学模型替代人为的主观判断,利用计算机技术从庞大的历史数据中海选能带来超额收益的多种“大概率”事件以制定策略,辅助投资者进行理性投资。不过计算机分析存在一定的技术门槛,有没有简单易学的量化交易方式,能够...

weixin_35911805的博客 2万+

noi.openjudge.cn 题目用到的头文件

#include #include #include #include #include #include #include #include #include #include #include #include #include #include #include using namespace std; int main() { return 0; }

sflsgfs的专栏 2350

SSE(Server-Sent Events)完整使用示例

本篇使用Node.js 作为服务端,在浏览器客户端演示SSE的完整实例和演示。实现了一个基于 Server-Sent Events (SSE) 的实时数据推送功能。

oscar999的专栏 755

poj1416

题意: 要求碎纸机能够把纸条上的数字切成一些片段,这些片段的和最接近而不超过所给的值tar,第一个样例,tar的值是50,而纸条上的数字是12346,应该把数字切成四部分,分别是1、2、34、6。因为这样所得到的和43 (= 1 + 2 + 34 + 6) 是所有可能中最接近而不超过50的。 三个要点: 1、如果tar的值等于纸条上的值,则不切。 2、如果没有办法把纸条上的数字切成小于

emmmmmm 2640

SEGGER Embedded Studio初体验

先楫HPM5E00EVK开发环境搭建

hywing's blog 1116

矢量网络分析仪校准:原理、方法与系统适配性

矢量网络分析仪(VNA)校准是射频测量的关键环节,通过误差建模(12项误差项)和标准件测量,可将系统不确定度从±5dB降至±0.1dB以下。主流校准方法包括SOLT(适用于同轴系统)和TRL(适用于波导系统),二者在标准件、误差重点及频率上限等方面存在显著差异。随着6G与量子通信的发展,动态实时校准将成为新趋势。工程师需根据传输结构、频率范围及精度需求选择合适方法,确保测量精度。

u014344410的博客 269

poj 1416 dfs 回溯

#include #include using namespace std; long int path,p,temp,aim; // path记录对应的一种划分,temp记录最大值个数 个人觉得temp的c long int sum,result; int he(char s[],int i) //前i位的和 { int sum1=0,j; for(j=0;j<i;j++)

leo_cuiyiming的专栏 379

7、RNA 二级结构预测:包含接吻发夹基序的新策略

本文提出三种用于预测RNA二级结构中接吻发夹基序的新策略,结合规范化规则与非歧义动态规划,分别在时间与空间复杂度之间进行权衡。策略A以O(n⁴)时间和O(n²)空间实现高效预测;策略B通过存储次优结果扩大搜索空间,空间复杂度升至O(n³);策略C全面探索组合可能,时间复杂度为O(n⁵),但保持O(n²)空间。实验表明不同策略适用于不同复杂度的序列,为RNA结构预测提供了灵活且可扩展的解决方案。

j7k8l的博客 136

POJ - 1416 Shredding Company解题报告(dfs)

题目大意: 每组测试数据就是给你两个数,s,n;让你给第2个数n分成几个数,问你怎么分可以让这些分得的数的和在不超过 s的情况下最接近s。s,n最多六位。

薛崇祥的博客 605

全球国家地区城市数据

MySQL全球国家地区城市数据报。。。。。。。。。。。。

迈瑞BS系列全自动生化分析仪LIS协议接口手册(内含HL7和ASTM)

迈瑞BS系列全自动生化分析仪LIS协议接口手册,内含HL7和ASTM)

POJ1416 Shredding Company ACM解题报告(DFS回溯+剪枝)

本渣渣明天C语言考试,今天有点亢奋,又来了一题,这题感觉比前一题难多了,不仅是字符串转化为数字,即使看了百度提醒的搜索树,还是参考了一些百度的代码。感觉道阻且长,我仍需努力。下面是题目翻译: 公司现在要发明一种新的碎纸机,要求新的碎纸机能够把纸条上的数字切成最接近而不超过target值。比如,target的值是50,而纸条上的数字是12346,应该把数字切成四部分,分别是1、2、34、6。因为这

Miracle_ma的专栏 1175

Outlook Express 6 (OE6) for Win7/Win8/Win10

英文版 Outlook Express6.0, 兼容Win7/Win10/WinXP,解压就可用。

POJ1416 Shredding Company

题目链接:http://poj.org/problem?id=1416 【分析】     水题不多说......直接暴力。 【代码】 #include #include #include using namespace std; int a[8],v[8][8]; int n,target,num; int d[8],fnd[8],fp; int ans,ansti

Zarxdy34 805

Probability and Statistics - 4th edition

Probability and Statistics - 4th edition

[POJ 1416]Shredding Company[DFS]

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

闲停 710

【枚举】POJ 1416 Shredding Company

POJ 1416 Shredding Company Shredding Company 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 unreadab

木又可可的博客 460

【POJ 1416】 Shredding Company

【POJ 1416】 Shredding Companydfs貌似不剪枝也能过 数据水水的 不过练练剪枝拓一下思路每组两个数t num 输入0 0结束 分割数字num为任意组 让这几组加和最接近t(且<=t) 无解输出error 多解输出rejected 否则输出加和还有分割情况做搜索剪枝有点小经验 搜索的时候逆向搜索 求最大就从大往小搜 求最小就从小往大搜 这样一出现不足(求最大时)或溢出(求最

ACMer' 1664

POJ 1416.Shredding Company

题目:http://poj.org/problem?id=1416 AC代码(C++): #include #include #include #include #include #include #include #include #include #include #define INF 0xfffffff #define MAXN 100105 usi

njyexiong的博客 207

poj1416 Shredding Company

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 shre

keshuqi的博客 608

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

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

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

【POJ1416】Shredding Company

本题传送门 本题知识点:深度优先搜索 + 回溯 本题题意很简单,就是有一条位数不超过6的数字纸条,问你怎么剪这纸条,使得得到的纸条的值的总和最接近目标值(总和不能超过目标值)。 比如第一个样例 50 12346 12346可以剪成 1 2 3 4 6(总和16);12 34 6(总和52)等,其中最接近且不大于目标值的就是剪成1 2 34 6,总和是43,所以输出 43 1 2 34 6...

LT4585的博客 314

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
上一篇: POJ-2531 Network Saboteur 解题报告
下一篇: POJ-1129 Channel Allocation 解题报告
angle555945
博客等级 码龄15年 66粉丝 74原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

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

抵扣说明:

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

余额充值