poj 1564 Sum It Up 搜索
题意:
给出一个数T,再给出n个数。若n个数中有几个数(可以是一个)的和是T,就输出相加的式子。不过不能输出相同的式子。
分析:
运用的是回溯法。比较特殊的一点就是不能输出相同的式子。这个可以通过map来实现:map<string,int>把字符串(可以是C语言的字符串)和整数联系起来了。我们可以把相加起来的几个数变成一个字符串(2+1+1,变成“211”),如果它出现过,就标记为1,初始值是0。出现过的就不再输出了。
剪枝:
1、所有的数加起来的和小于T,直接输出NONE。
2、搜索过程中,发现sum大于t,直接return ,不再继续搜索了。如果sum等于t,验证有无重复后决定输出与否,也不再继续搜索了。
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<map>
using namespace std; const int N=;
int t,n,flag,sum;
int p[N];
bool vis[N];
map<string,int> st;
char ch[];
void DFS(int k)
{
if(sum==t)
{
int j=;
for(int i=;i<n;i++)
{
if(!vis[i]) continue;
int x=p[i];
while(x)
{
int y=x%;
ch[j++]=y+;
x/=;
}
}
ch[j]=;
if(!st[ch])
{
//puts(ch);
flag=;
int f=;
for(int i=;i<n;i++)
{
if(vis[i]&&f) printf("+%d",p[i]);
if(vis[i]&&!f) {printf("%d",p[i]);f=;}
}
printf("\n");
st[ch]=;
return ;
} }
if(sum>t) return ;
for(int i=k;i<n;i++)
{
sum+=p[i];
vis[i]=;
DFS(i+);
sum-=p[i];
vis[i]=;
}
}
int main()
{
//freopen("test.txt","r",stdin);
while(scanf("%d%d",&t,&n)!=EOF&&t)
{
st.clear();
printf("Sums of %d:\n",t);
for(int i=;i<n;i++)
{
scanf("%d",&p[i]);
sum+=p[i];
}
if(sum<t) {printf("NONE\n");continue;}
memset(vis,,sizeof(vis));
flag=;
sum=;
DFS();
if(!flag) printf("NONE\n");
}
return ;
}
poj 1564 Sum It Up 搜索的更多相关文章
- poj 1564 Sum It Up
题目连接 http://poj.org/problem?id=1564 Sum It Up Description Given a specified total t and a list of n ...
- poj 1564 Sum It Up (DFS+ 去重+排序)
http://poj.org/problem?id=1564 该题运用DFS但是要注意去重,不能输出重复的答案 两种去重方式代码中有标出 第一种if(a[i]!=a[i-1])意思是如果这个数a[i] ...
- poj 1564 Sum It Up | zoj 1711 | hdu 1548 (dfs + 剪枝 or 判重)
Sum It Up Time Limit : 2000/1000ms (Java/Other) Memory Limit : 65536/32768K (Java/Other) Total Sub ...
- POJ 1564 Sum It Up (DFS+剪枝)
...
- poj 1564 Sum It Up【dfs+去重】
Sum It Up Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 6682 Accepted: 3475 Descrip ...
- POJ 1564 Sum It Up(DFS)
Sum It Up Time Limit:1000MS Memory Limit:32768KB 64bit IO Format:%I64d & %I64u Submit St ...
- poj 1564 Sum It Up(dfs)
Sum It Up Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 7191 Accepted: 3745 Descrip ...
- POJ 1564 经典dfs
1.POJ 1564 Sum It Up 2.总结: 题意:在n个数里输出所有相加为t的情况. #include<iostream> #include<cstring> #in ...
- ACM:POJ 2739 Sum of Consecutive Prime Numbers-素数打表-尺取法
POJ 2739 Sum of Consecutive Prime Numbers Time Limit:1000MS Memory Limit:65536KB 64bit IO Fo ...
随机推荐
- PostgreSQL使用总结
最近项目用到了PostgreSQL数据库,网上一堆教程,这里自己整理一下做个笔记: 1,下载安装,我这边安装在Windows7,在这里找到大象一样的标志: 2,双击打开,这里的话按流程直接走: 3,这 ...
- python 易忘操作整理
>>> l=[1,2,3,4,5] >>> s='$'.join(str(i) for i in l) >>> print(s,end=" ...
- 清北学堂模拟赛d3t2 b
分析:一道比较让人头疼的数学题. 先考虑怎么让分出来的三角形相似,先不考虑每个三角形的具体边长,设每个三角形的周长为li,则可知必然有一个数g = gcd{li},每一个三角形的周长都是g的倍数,这样 ...
- Grails,应该不错
就当学习英文,也慢慢看看啦..
- K - Count the string kmp_Next数组应用
It is well known that AekdyCoin is good at string problems as well as number theory problems. When g ...
- 51nod——1432 独木桥
https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1432 基准时间限制:1 秒 空间限制:131072 KB 分值: 10 ...
- 为什么视频流一般都用UDP
我们知道TCP 和 UDP的一个区别之一就是TCP要保证丢失的package会被再次重发,确保对方能够收到. 而在视频播放中,如果有一秒钟的信号确实,导致画面出现了一点瑕疵,那么最合适的办法是把这点瑕 ...
- c++ 双冒号的作用
转:原文:http://www.cnblogs.com/charley_yang/archive/2011/02/24/1964217.html #define FLY 10 #line 100 &q ...
- songtzu的创业产品的经历
我的产品是关于卡通头像的东东,也有点照片处理app的感觉.你可能会想到脸萌.或者足迹.可是.我自觉得,我比这两者想做的东西要好. 以下的两张是站点首页的效果图. 图片版权与肖像权全部,非授权不得使用. ...
- 移动互联网App推广的十大难题
常常有朋友来问."我做了一个App,请问怎么推广啊?"或者就是"我们公司开发了一个App.想短时间内获取巨大的量."还有的就是问"有没有什么好渠道三个 ...