hdu 1258 Sum It Up (dfs+路径记录)
pid=1258">Sum It Up
Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)
Total Submission(s): 3953 Accepted Submission(s): 2032
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.
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.
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.
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
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
记录答案: 用一个数组跟着搜索路线进行下去,顺便就把答案记录了。
防止答案反复:在一次遍历数组时。记录上一次搜索的值。当前值不和该值相等就好了!
(真是学无止境,继续AC)
#include"stdio.h"
#include"string.h"
#include"math.h"
#include"algorithm"
using namespace std;
#define N 20
int n,t,a[N];
int ans[N],flag;
void dfs(int x,int s,int cnt)
{
int i,tmp;
if(s>t)
return ;
if(s==t)
{
for(i=0;i<cnt;i++)
{
if(i==cnt-1)
printf("%d\n",ans[i]);
else
printf("%d+",ans[i]);
}
flag=1;
}
else
{
tmp=-1;
for(i=x;i<n;i++)
{
if(tmp!=a[i]) //保留当前的数,能避免反复
{
tmp=ans[cnt++]=a[i];
dfs(i+1,s+a[i],cnt);
cnt--;
}
}
}
}
int main()
{
int i;
while(scanf("%d%d",&t,&n),n||t)
{
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
printf("Sums of %d:\n",t);
flag=0;
dfs(0,0,0);
if(flag==0)
printf("NONE\n");
}
return 0;
}
hdu 1258 Sum It Up (dfs+路径记录)的更多相关文章
- HDOJ(HDU).1258 Sum It Up (DFS)
HDOJ(HDU).1258 Sum It Up (DFS) [从零开始DFS(6)] 点我挑战题目 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架/双 ...
- (step4.3.4)hdu 1258(Sum It Up——DFS)
题目大意:输入t,n,接下来有n个数组成的一个序列.输出总和为t的子序列 解题思路:DFS 代码如下(有详细的注释): #include <iostream> #include <a ...
- HDU 1258 Sum It Up(DFS)
题目链接 Problem Description Given a specified total t and a list of n integers, find all distinct sums ...
- HDU 1258 Sum It Up(dfs 巧妙去重)
传送门: http://acm.hdu.edu.cn/showproblem.php?pid=1258 Sum It Up Time Limit: 2000/1000 MS (Java/Others) ...
- hdu 1258 Sum It Up(dfs+去重)
题目大意: 给你一个总和(total)和一列(list)整数,共n个整数,要求用这些整数相加,使相加的结果等于total,找出所有不相同的拼凑方法. 例如,total = 4,n = 6,list = ...
- 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 ...
- HDU 1258 Sum It Up
Sum It Up Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total S ...
- HDOJ(HDU).1016 Prime Ring Problem (DFS)
HDOJ(HDU).1016 Prime Ring Problem (DFS) [从零开始DFS(3)] 从零开始DFS HDOJ.1342 Lotto [从零开始DFS(0)] - DFS思想与框架 ...
- 剑指 Offer 34. 二叉树中和为某一值的路径 + 记录所有路径
剑指 Offer 34. 二叉树中和为某一值的路径 Offer_34 题目详情 题解分析 本题是二叉树相关的题目,但是又和路径记录相关. 在记录路径时,可以使用一个栈来存储一条符合的路径,在回溯时将进 ...
随机推荐
- 谈谈如何来查看GC日志
一.首先来看一下JVM中的GC有哪几种类型? 1.-XX:UseSerialGC 虚拟机运行在Client模式的默认值,打开此开关参数后,使用Serial+Serial Old收集器组合进行垃圾收集. ...
- slover层解读
void Solver<Dtype>::UpdateSmoothedLoss(Dtype loss, int start_iter, int average_loss) { if (los ...
- jq进度条
<!doctype html><html><head><meta charset="utf-8"><title>JQue ...
- 51nod 1013 3的幂的和 - 快速幂&除法取模
题目地址:http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1013 Konwledge Point: 快速幂:https:/ ...
- BZOJ 4464 旅行时的困惑 最小流
题面: Waldives 有 N 个小岛.目前的交通系统中包含 N-1 条快艇专线,每条快艇 专线连接两个岛.这 N-1条快艇专线恰好形成了一棵树. 由于特殊的原因,所有N-1条快艇专线都是单向的.这 ...
- nginx搭建静态资源服务器
nginx配置访问前端工程 1.前端工程目录 crm-view |-- view |-- user.html 2.工程位置 C:\Users\Administrator\Desktop\CRM系统\c ...
- PHPExcel读取表格内容
PHPExcel读取表格 先引入类IOFactory.php require_once '../PHPExcel/IOFactory.php'; $filePath = "test.xlsx ...
- Jbox 弹出窗口 子页面操作完成后关闭 父页面刷新
父页面js //父页面js <script> var isFreshFlag = '1'; //添加会议活动 function addMeetingAct(){ var attendVip ...
- Jquery validate自定义验证
http://www.runoob.com/jquery/jquery-plugin-validate.html addMethod(name,method,message)方法 参数 name 是添 ...
- 洛谷 3106 [USACO14OPEN]GPS的决斗Dueling GPS's 3720 [AHOI2017初中组]guide
[题解] 这两道题是完全一样的. 思路其实很简单,对于两种边权分别建反向图跑dijkstra. 如果某条边在某一种边权的图中不是最短路上的边,就把它的cnt加上1.(这样每条边的cnt是0或1或2,代 ...