ZOJ Problem Set - 1711 解题报告

/*
	25/10/13 16:57

	ZOJ 1711 Sum It Up 解题报告 
	题意 :给出一个值sum和一系列降序正整数,从降序数字中取出若干数字之后求和可能等于sum ,找出所有不重复的可能性,并按字典序降序排列输出 
	思路 :dfs+剪枝。之前也想过要去重,但是太麻烦且运行低效,就放弃了 
*/
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int list[1005],vis[1005];
int result,N;
bool flag;

void dfs(int i,int sum);
void output();

int main(){
	int i,sum;
	while(scanf("%d%d",&result,&N),result+N)
	{
		memset(vis,0,sizeof(vis));
		for(i=0;i<N;i++)
			scanf("%d",&list[i]);
		flag=1;
		printf("Sums of %d:\n",result);
		dfs(0,0);
		if(flag)puts("NONE");
	}
	return 0;
}
void dfs(int i,int sum){
	if(i>=N||sum>result)
		return;
	vis[i]=1;	              //先取当前的数字
	if(sum+list[i]==result){  //这里的顺序很重要,如果 vis[i]=1;放到后面的话,输出会出现连续的重复,要改动list[]再加上一个判定函数才能搞定 
		flag=0;
		output();
	}	
	dfs(i+1,sum+list[i]);
	
	vis[i]=0;                 //重要:若不取当前数字的话,那么后面相同的数字都不取 否则会挂掉 找到一个很好的例子8 8 4 4 2 2 2 2 1 1
	while(list[i+1]==list[i])
		i++;
	dfs(i+1,sum);		
}
void output(){
	int i,k=0;
	for(i=0;i<N;i++){
		if(vis[i]){
			if(k)
				printf("+%d",list[i]);
			else 
				printf("%d",list[i]);
			k++;
		}
	}
	puts("");
}

就这样啦。。。玩玩Dota改改代码,早上搞到下午才搞定尴尬

邮箱wangliaofeng@gmail.com欢迎交流

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Antique Comedians of Malidinesia would like to play a new discovered comedy of Aristofanes. Putting it on a stage should be a big surprise for the audience so all the preparations must be kept absolutely secret. The ACM director suspects one of his competitors of reading his correspondece. To prevent other companies from revealing his secret, he decided to use a substitution cipher in all the letters mentioning the new play. Substitution cipher is defined by a substitution table assigning each character of the substitution alphabet another character of the same alphabet. The assignment is a bijection (to each character exactly one character is assigned -- not neccessary different). The director is afraid of disclosing the substitution table and therefore he changes it frequently. After each change he chooses a few words from a dictionary by random, encrypts them and sends them together with an encrypted message. The plain (i.e. non-encrypted) words are sent by a secure channel, not by mail. The recipient of the message can then compare plain and encrypted words and create a new substitution table. Unfortunately, one of the ACM cipher specialists have found that this system is sometimes insecure. Some messages can be decrypted by the rival company even without knowing the plain words. The reason is that when the director chooses the words from the dictionary and encrypts them, he never changes their order (the words in the dictionary are lexicographically sorted). String a1a2 ... ap is lexicografically smaller than b1b2 ... bq if there exists an integer i, i <= p, i <= q, such that aj=bj for each j, 1 <= j < i and ai < bi. The director is interested in which of his messages could be read by the rival company. You are to write a program to determine that. Input Output Sample Input 2 5 6 cebdbac cac ecd dca aba bac cedab 4 4 cca cad aac bca bdac Sample Output abcde Message cannot be decrypted.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值