传送门:

http://acm.hdu.edu.cn/showproblem.php?pid=1258

Sum It Up

Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 7758    Accepted Submission(s): 4067

Problem Description
Given a specified total t and a list of n integers, find all distinct sums using numbers from the list that add up to t. For example, if t=4, n=6, and the list is [4,3,2,2,1,1], then there are four different sums that equal 4: 4,3+1,2+2, and 2+1+1.(A number can be used within a sum as many times as it appears in the list, and a single number counts as a sum.) Your job is to solve this problem in general.
 
Input
The input will contain one or more test cases, one per line. Each test case contains t, the total, followed by n, the number of integers in the list, followed by n integers x1,...,xn. If n=0 it signals the end of the input; otherwise, t will be a positive integer less than 1000, n will be an integer between 1 and 12(inclusive), and x1,...,xn will be positive integers less than 100. All numbers will be separated by exactly one space. The numbers in each list appear in nonincreasing order, and there may be repetitions.
 
Output
For each test case, first output a line containing 'Sums of', the total, and a colon. Then output each sum, one per line; if there are no sums, output the line 'NONE'. The numbers within each sum must appear in nonincreasing order. A number may be repeated in the sum as many times as it was repeated in the original list. The sums themselves must be sorted in decreasing order based on the numbers appearing in the sum. In other words, the sums must be sorted by their first number; sums with the same first number must be sorted by their second number; sums with the same first two numbers must be sorted by their third number; and so on. Within each test case, all sums must be distince; the same sum connot appear twice.
 
Sample Input
4 6 4 3 2 2 1 1
5 3 2 1 1
400 12 50 50 50 50 50 50 25 25 25 25 25 25
0 0
 
Sample Output
Sums of 4:
4
3+1
2+2
2+1+1
Sums of 5:
NONE
Sums of 400:
50+50+50+50+50+50+25+25+25+25
50+50+50+50+50+25+25+25+25+25+25
 
Source
 
Recommend
JGShining   |   We have carefully selected several similar problems for you:  1045 1195 1067 1426 1242 
 
题目意思:
给定一个非递减的序列,要求从这些序列中找出一系列的数相加等于要求的数。
主要就是我们需要对结果去重!
这里有一共很巧妙的办法
当你要搜的数字刚好前一步搜了,那么跳过你要搜的数字(直接看代码更好理解)
还有就是这个dfs的框架也很重要
关于到它到底是怎么搜的
我觉得很受启发
code:
#include<iostream>
#include<algorithm>
#include<stdio.h>
#include<queue>
#include<set>
#include<map>
#include<string>
#include<memory.h>
using namespace std;
#define max_v 25
int sum,n;
int a[max_v];
int ans[max_v];
int flag;
bool cmp(int x,int y)
{
return x>y;
}
void dfs(int now,int s,int f)//现在的和 开始位置 结束位置
{
if(now>sum)//没有搜到
return ;
if(now==sum)//搜到 输出
{
cout<<ans[];
for(int i=;i<f;i++)
{
cout<<"+"<<ans[i];
}
cout<<endl;
flag=;
}
for(int i=s;i<n;i++)
{
ans[f]=a[i];
dfs(now+a[i],i+,f+);
while(i+<n&&a[i]==a[i+])//搜完后,若下一个要搜的和搜完的一样 跳过
{
i++;
}
}
}
int main()
{
while(cin>>sum>>n)
{
if(sum==&&n==)
break;
int allsum=;
for(int i=;i<n;i++)
{
cin>>a[i];
allsum+=a[i];
}
cout<<"Sums of "<<sum<<":"<<endl;
if(allsum<sum)
{
cout<<"NONE"<<endl;
continue;
}
sort(a,a+n,cmp);
flag=;
dfs(,,);
if(flag==)
{
cout<<"NONE"<<endl;
}
}
return ;
}
 

HDU 1258 Sum It Up(dfs 巧妙去重)的更多相关文章

  1. HDOJ(HDU).1258 Sum It Up (DFS)

    HDOJ(HDU).1258 Sum It Up (DFS) [从零开始DFS(6)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双 ...

  2. (step4.3.4)hdu 1258(Sum It Up——DFS)

    题目大意:输入t,n,接下来有n个数组成的一个序列.输出总和为t的子序列 解题思路:DFS 代码如下(有详细的注释): #include <iostream> #include <a ...

  3. HDU 1258 Sum It Up(DFS)

    题目链接 Problem Description Given a specified total t and a list of n integers, find all distinct sums ...

  4. hdu 1258 Sum It Up (dfs+路径记录)

    pid=1258">Sum It Up Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (J ...

  5. hdu 1258 Sum It Up(dfs+去重)

    题目大意: 给你一个总和(total)和一列(list)整数,共n个整数,要求用这些整数相加,使相加的结果等于total,找出所有不相同的拼凑方法. 例如,total = 4,n = 6,list = ...

  6. HDU 1258 Sum It Up (DFS)

    Sum It Up Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  7. HDU 1258 Sum It Up

    Sum It Up Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total S ...

  8. HDOJ(HDU).1016 Prime Ring Problem (DFS)

    HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...

  9. HDU 1241 Oil Deposits --- 入门DFS

    HDU 1241 题目大意:给定一块油田,求其连通块的数目.上下左右斜对角相邻的@属于同一个连通块. 解题思路:对每一个@进行dfs遍历并标记访问状态,一次dfs可以访问一个连通块,最后统计数量. / ...

随机推荐

  1. 【shell】《shell学习指南》读书笔记

    一.使用shell脚本 优点:脚本语言能够轻易处理文件与目录之间的对象,如把文件从所有目录拷贝到另一个目录 缺点:效率不如编译型语言 二.简单的脚本 1.查看现在系统有谁登录 $who 2.算出行数 ...

  2. [转]微信小程序,开发大起底

    本文转自:http://blog.csdn.net/baiyuzhong2012/article/details/54378497 作者简介:张智超,北京微函工坊开发工程师,CSDN微信开发知识库特邀 ...

  3. ADO.NET教程(2)实现增删查改

    声明一个类,在类中实现增删查改的方法 public class AdoNet { //声明连接字符串 public string Sqlstr = "data source={0};data ...

  4. 在MyEclipse中使用javadoc导出API文档详解

    本篇文档介绍如何在MyEclipse中导出javadoc(API)帮助文档,并且使用htmlhelp.exe和jd2chm.exe生成chm文档. 具体步骤如下: 打开MyEclipse,选中想要制作 ...

  5. libtar 和 libz 的使用

    libtar 和 libz 的使用 用c代码生成 tar.gz 文件  实现的功能和 tar -zcf 命令一样 大概流程为 1.先用libtar相关函数对某个目录生成tar文件 2.然后对tar文件 ...

  6. Linux 连接 Xshell 及网络配置

    一.准备工具 在WMware上已经装有Linux系统:WMware安装CentOS7文章. xshell连接工具: 二.修改相关配置 切换到root用户下: 配置主机名(可选): #方法一:替换原主机 ...

  7. Java入门到精通——调错篇之Spring2.5利用aspect实现AOP时报错: error at ::0 can't find referenced pointcut XXX

    一.问题描述及原因. 利用Aspect注解实现AOP的时候出现了error at ::0 can't find referenced pointcut XXX.一看我以为注解写错了,结果通过查询相关资 ...

  8. Android内存泄漏排查利器LeakCanary

    开源地址:https://github.com/square/leakcanary 在 build.gralde 里加上依赖, 然后sync 一下, 添加内容如下 dependencies { ... ...

  9. selendroid之inspector

    http://selendroid.io/inspector.html 寻找元素.断点模式.

  10. Android学习——Fragment与Activity通信(一)

    学会了在Activity中加载Fragment的方法之后,接下来便需要学习Activity和Fragment之间的通信.这一节先学习如何把Activity中的信息传递给Fragment. 基本过程 在 ...