hdu1258Sum It Up (DFS)
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
知识点:DFS
题意:给个数t,再给一组数列,降序组合出题目给的数t,要求按降序排列输出.
难点:判断重复的组合。
#include<cstdlib>
#include<cstdio>
#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
int vis[];
int a[];
int b[];
int t,n,flag;
void dfs(int p,int num,int cnt)
{
if(num==t)
{
flag=;
for(int j=;j<cnt;j++)
printf(j==cnt-?"%d\n":"%d+",b[j]);
return;
}
if(num>t)
return;
if(cnt>=n)
return;
int temp=-;
for(int i=p;i<n;i++)
{
if(!vis[i]&&temp!=a[i])//判断重复:手动模拟一遍便知。
{
vis[i]=;
b[cnt]=a[i];
temp=a[i];
dfs(i,num+a[i],cnt+);
vis[i]=;
}
}
}
int main()
{
while(~scanf("%d%d",&t,&n))
{
if(t==&&n==)
break;
memset(a,,sizeof(a));
for(int i=;i<n;i++)
scanf("%d",&a[i]);
sort(a,a+n);
reverse(a,a+n);//reverse(a,a+n)代表对a数组中所有元素反序存储
printf("Sums of %d:\n",t);
memset(vis,,sizeof(vis));
flag=;
dfs(,,);
if(flag==)
printf("NONE\n");
}
return ;
}
hdu1258Sum It Up (DFS)的更多相关文章
- hdu--1258--Sum It Up(Map水过)
Sum It Up Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- BZOJ 3083: 遥远的国度 [树链剖分 DFS序 LCA]
3083: 遥远的国度 Time Limit: 10 Sec Memory Limit: 1280 MBSubmit: 3127 Solved: 795[Submit][Status][Discu ...
- BZOJ 1103: [POI2007]大都市meg [DFS序 树状数组]
1103: [POI2007]大都市meg Time Limit: 10 Sec Memory Limit: 162 MBSubmit: 2221 Solved: 1179[Submit][Sta ...
- BZOJ 4196: [Noi2015]软件包管理器 [树链剖分 DFS序]
4196: [Noi2015]软件包管理器 Time Limit: 10 Sec Memory Limit: 512 MBSubmit: 1352 Solved: 780[Submit][Stat ...
- 图的遍历(搜索)算法(深度优先算法DFS和广度优先算法BFS)
图的遍历的定义: 从图的某个顶点出发访问遍图中所有顶点,且每个顶点仅被访问一次.(连通图与非连通图) 深度优先遍历(DFS): 1.访问指定的起始顶点: 2.若当前访问的顶点的邻接顶点有未被访问的,则 ...
- BZOJ 2434: [Noi2011]阿狸的打字机 [AC自动机 Fail树 树状数组 DFS序]
2434: [Noi2011]阿狸的打字机 Time Limit: 10 Sec Memory Limit: 256 MBSubmit: 2545 Solved: 1419[Submit][Sta ...
- POJ_2386 Lake Counting (dfs 错了一个负号找了一上午)
来之不易的2017第一发ac http://poj.org/problem?id=2386 Lake Counting Time Limit: 1000MS Memory Limit: 65536 ...
- 深度优先搜索(DFS)
[算法入门] 郭志伟@SYSU:raphealguo(at)qq.com 2012/05/12 1.前言 深度优先搜索(缩写DFS)有点类似广度优先搜索,也是对一个连通图进行遍历的算法.它的思想是从一 ...
- 【BZOJ-3779】重组病毒 LinkCutTree + 线段树 + DFS序
3779: 重组病毒 Time Limit: 20 Sec Memory Limit: 512 MBSubmit: 224 Solved: 95[Submit][Status][Discuss] ...
随机推荐
- 【剑指offer】面试题20:顺时针打印矩阵
题目描述 输入一个矩阵,按照从外向里以顺时针的顺序依次打印出每一个数字,例如,如果输入如下矩阵: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...
- 喜欢的女生快被别人抢走了,我敢怎么抢? - V2EX
喜欢的女生快被别人抢走了,我敢怎么抢? - V2EX 三大定律镇楼: 第一定律:永远不要以为女生对你有好感.第二定律:告白等于见光死.第三定律:秀恩爱分得快.
- linux 下apache安装、启动和配置
linux 下 apache安装 1:系统安装,这里就不说了,网上有很多,也很简单.顺便说下,我用的是redhat 9: 2:在图形界面下下载apache 安装包,我下的是 httpd-2.2.9.t ...
- IOS 计算密码强度
+ (BOOL) judgeRange:(NSArray*)conditionArr Password:(NSString*)password { NSRange range; BOOL result ...
- LINQ 之四 (SkipWhile和TakeWhile)
看到书上的例子很正常,于是,我写了个不正常一点的. 书上的是这样的 class Program { static void Main(string[] args) { ,,,,,,,,}; ); fo ...
- C# winform平台下使用spread控件导出excel表格
//首先要引入两个控件:1.根据自己的office 版本在项目->添加引用->microsoft office object 12.0 library (2010版) //2.在.net中 ...
- win10下安装通过Hyper-v安装Ubuntu
一直也来在做C#的开发,Winform及Web都有所涉及,想在闲暇之余学习下Python,拓展一下自己的知识.既然决定学习Python那么就直接在Linux下进行吧,由于Ubuntu最近很火而且也有方 ...
- 关于box-sizing的理解
---恢复内容开始--- box-sizing 属性允许您以特定的方式定义匹配某个区域的特定元素. 例如,假如您需要并排放置两个带边框的框,可通过将 box-sizing 设置为 "bord ...
- 配置Apache2 管理 SVN
软件环境:CentOS-7-x86_64 1.安装 mod_dav_svn 模块 2.在Apache2下增加管理配置,如: cd /etc/httpd/conf.d vim subversion. ...
- (转+原)VC编译错误:uafxcw.lib(afxmem.obj) : error LNK2005: "void * __cdecl operator new(unsigned int)" (??2@YAPAXI@Z) 已经在 LIBCMT.lib(new.obj) 中定义
参考网址:http://zhanyonhu.blog.163.com/blog/static/16186044201023094754832/ 1>uafxcw.lib(afxmem.obj) ...