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] ...
随机推荐
- C# 反射_基础
反射用于在程序运行过程中,获取类里面的信息或发现程序集并运行的一个过程.通过反射可以获得.dll和.exe后缀的程序集里面的信息.使用反射可以看到一个程序集内部的类,接口,字段,属性,方法,特性等信息 ...
- python实现词法分析
#请先安装Ply # -*- coding: utf-8 -*- #------------------------------------------------------------------ ...
- tr 替换删除字符
1.关于tr 通过使用 tr,您可以非常容易地实现 sed 的许多最基本功能.您可以将 tr 看作为 sed 的(极其)简化的变体:它可以用一个字符来替换另一个字符,或者可以完全除去一些字符.您 ...
- cf479D Long Jumps
D. Long Jumps time limit per test 1 second memory limit per test 256 megabytes input standard input ...
- 2013长沙网络赛H题Hypersphere (蛋疼的题目 神似邀请赛A题)
Hypersphere Time Limit: 1 Second Memory Limit: 32768 KB In the world of k-dimension, there's a ...
- hdu 2438 Turn the corner(几何+三分)
Problem Description Mr. West bought a new car! So he is travelling around the city. One day he comes ...
- IOS拷贝文件到沙盒
- (void)copyFileFromResourceTOSandbox { //文件类型 NSString * docPath = [[NSBundle mainBundle] pathForRe ...
- php下载服务器上的文件
$file_xls=$path; // 文件的保存路径 $example_name=basename($file_xls); //获取文件名 header('Content-Descrip ...
- 使用zeranoe的自动化脚本mingw-w64-build-3.6.4在ubutun14.04_64bit上交叉编译ffmpeg 2.5
编译期间,碰到的两个大问题,是环境变量设置问题. 先列出执行脚本是碰到的两个问题,后面给出解决方法并记录执行的命令步骤. 问题1: 编译binutils 2.5出错 ----------------- ...
- CSS-边框-效果
1.1边框 其中边框圆角.边框阴影属性,应用十分广泛,兼容性也相对较好,具有符合渐进增强原则的特性,我们需要重点掌握. 1.1.1边框圆角 border-radius 每个角可以设置两个值,x值,y值 ...