传送门:

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. 读书笔记-NIO的工作方式

    读书笔记-NIO的工作方式 1.BIO是阻塞IO,一旦阻塞线程将失去对CPU的使用权,当前的网络IO有一些解决办法:1)一个客户端对应一个处理线程:2)采用线程池.但也会出问题. 2.NIO的关键类C ...

  2. AI从入门到放弃:CNN的导火索,用MLP做图像分类识别?

    欢迎大家前往腾讯云+社区,获取更多腾讯海量技术实践干货哦~ 作者:郑善友 腾讯MIG后台开发工程师 导语:在没有CNN以及更先进的神经网络的时代,朴素的想法是用多层感知机(MLP)做图片分类的识别:但 ...

  3. ul+js模拟select

    html   css .select_box{ float: left; } .select_box input{ width: 160px; height: 30px; text-align: ce ...

  4. MVC3.0与MVC2.0的区别

    昨天面试时第一回用MVC2.0做了一个简单的增删改查功能的测试.想一下用了一年多的MVC3.0,对这两个版本不同之处做以下几点总结: 最明显的是MVC3.0较MVC2.0而言,多了Razor视图: 1 ...

  5. Sql server 操作笔记

    (1)更改字段类型 (2)添加字段 alter table class add InKinDate intEXECUTE sp_addextendedproperty N'MS_Description ...

  6. 【学习笔记】JDBC数据库连接技术(Java Database Connectivity)

    一.JDBC简介 Java是通过JDBC技术实现对各种数据库的访问的,JDBC是Java数据库连接技术的简称.它可以把数据持久保存,是一种持久化机制. 1.持久化 持久化就是将程序中的数据在瞬时状态和 ...

  7. OLEDB不使用SQL语句直接打开数据表

    一般来说获取数据库表的方法是采用类似 select * from table_name 这样的sql语句.SQL语句必然伴随着数据库的解释执行,一般来说效率比较低下,而且使用SQL语句时需要数据库支持 ...

  8. 51nod 1597 有限背包计数问题 (背包 分块)

    题意 题目链接 Sol 不会做啊AAA.. 暴力上肯定是不行的,考虑根号分组 设\(m = \sqrt{n}\) 对于前\(m\)个直接暴力,利用单调队列优化多重背包的思想,按\(\% i\)分组一下 ...

  9. DrawerLayout侧滑

    DrawerLayout是Support Library包中实现了侧滑菜单效果的控件,可以说DrawerLayout是因为第三方控件如SlidingMenu等出现之后,google借鉴而出现的产物.D ...

  10. Android DiskLruCache完全解析,硬盘缓存的最佳方案 --转载

    概述 记得在很早之前,我有写过一篇文章 Android高效加载大图.多图解决方案,有效避免程序OOM,这篇文章是翻译自Android Doc的,其中防止多图OOM的核心解决思路就是使用LruCache ...