hdu 1258 Sum It Up(dfs)
来源:互联网 发布:ubuntu更新软件的命令 编辑:程序博客网 时间:2024/06/10 14:44
小记:起初以为是一道非常水的dfs暴搜题,然后写完测试一看,蒙了。 相同的答案太多,也就是说,我将相同的数字都看出是不一样的了,实际上都是相同的,
也就是说我没判重。于是就必须加个判重,但是这样又不好做了,标记判重不可能。
思路:对每个数值记录出现了多少次,然后对原数组从大到小排序,并去重。 然后对剩下的数组进行dfs搜索。
依据是每次每种数组用多少个,从第一个开始往后,记录每一个数值使用的个数,最后依照这个使用个数的数组进行输出即可。
代码:
#include <iostream>#include <stdio.h>#include <string.h>#include <math.h>#include <stdlib.h>#include <map>#include <set>#include <vector>#include <stack>#include <queue>#include <algorithm>using namespace std;#define mst(a,b) memset(a,b,sizeof(a))#define REP(a,b,c) for(int a = b; a < c; ++a)#define eps 10e-8const int MAX_ = 10010;const int N = 100010;const int INF = 0x7fffffff;bool vis[MAX_];int h[MAX_];int num[MAX_];int a[MAX_];int dir[2] = {1,-1};bool flag;int n, m;void dfs(int cur, int sum) { if(sum == n) { bool first = 0; REP(i, 0, m) { REP(j, 0, h[i]) { if(!first) { printf("%d", a[i]); first = 1; } else { printf("+%d", a[i]); } } } printf("\n"); flag = 1; return ; } if(cur == m) { return ; } for(int i = num[a[cur]]; i > -1; -- i) { if(i * a[cur] + sum > n)continue; h[cur] = i; dfs(cur+1,sum + i*a[cur]); }}bool cmp(const int a, const int b) { return a>b;}int main() { int T; while(scanf("%d%d", & n, & m), n||m) { flag = 0; mst(num, 0); REP(i, 0, m) { scanf("%d", &a[i]); num[a[i]] ++; } sort(a,a+m,cmp); m = unique(a,a+m) - a; printf("Sums of %d:\n", n); dfs(0, 0); if(!flag) { printf("NONE\n"); } } return 0;}
0 0
- hdu 1258 Sum It Up(DFS)
- hdu 1258 Sum It Up(dfs)
- HDU 1258 Sum It Up (DFS)
- HDU 1258 Sum It Up(DFS)
- HDU 1258 Sum It Up(DFS)
- hdu-1258-Sum It Up(DFS)
- hdu 1258 Sum It Up(dfs)
- hdu 1258 Sum It Up dfs
- HDU 1258 Sum It Up DFS
- HDU 1258 Sum it up 搜索DFS
- HDU--1258:Sum It Up (DFS)
- hdu 1258 Sum It Up (dfs)
- hdu 1258 Sum It Up(dfs)
- HDU 1258 Sum It Up (还是DFS)
- hdu 1258 Sum it up dfs+剪枝
- HDOJ(HDU).1258 Sum It Up (DFS)
- hdu 1258 Sum It Up dfs暴搜
- HDU 1258 (DFS) Sum It Up
- centos6.5 源代码方式安装python2.7.8
- 全方位阐述HDFS
- Android开源介绍-UI组件
- 什么用户的什么需求
- Android动画分析之3D翻转效果
- hdu 1258 Sum It Up(dfs)
- HDU 1072 Nightmare
- 在springMVC+mybatis+Ajax+JSON+jQuery easyui的项目中,自定义全局异常处理器
- javascript实现简单的走马灯
- oracle中的raw类型
- load data file使用详解
- C++ primer(第五版) 练习 3.30 个人 见解
- Linux下如何将数据库脚本文件从sh格式变为sql格式
- Android应用如何实现换肤功能