HDU 4403 A very hard Aoshu problem(dfs爆搜)

http://acm.hdu.edu.cn/showproblem.php?pid=4403

题意:

给出一串数字,在里面添加一个等号和多个+号,使得等式成立,问有多少种不同的式子。

思路:

数据量比较小,直接dfs爆搜答案即可。

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<map>
 6 using namespace std;
 7
 8 char s[20];
 9 int a[2][100], tot[2], ans;
10 map<int,int> f;
11
12 void dfs(int cur, int pre, int sum, int ends, int id)
13 {
14     if(cur == ends)
15     {
16         if(pre)  return;
17         if(!id)  f[sum]++;
18         else if(f[sum])  ans+=f[sum];
19         return;
20     }
21     dfs(cur+1,0,sum+pre*10+s[cur]-‘0‘,ends,id);
22     dfs(cur+1,pre*10+s[cur]-‘0‘,sum,ends,id);
23 }
24
25 int main()
26 {
27     //freopen("in.txt","r",stdin);
28     while(~scanf("%s",s) && s[0]!=‘E‘)
29     {
30         ans = 0;
31         int n = strlen(s);
32         for(int i=1;i<n;i++)
33         {
34             f.clear();
35             dfs(0,0,0,i,0);
36             dfs(i,0,0,n,1);
37         }
38         printf("%d\n",ans);
39     }
40 }
时间: 2024-10-11 13:24:43

HDU 4403 A very hard Aoshu problem(dfs爆搜)的相关文章

[ACM] hdu 4403 A very hard Aoshu problem (DFS暴搜数字)

A very hard Aoshu problem Problem Description Aoshu is very popular among primary school students. It is mathematics, but much harder than ordinary mathematics for primary school students. Teacher Liu is an Aoshu teacher. He just comes out with a pro

HDU 4403 A very hard Aoshu problem(DFS)

A very hard Aoshu problem Problem Description Aoshu is very popular among primary school students. It is mathematics, but much harder than ordinary mathematics for primary school students. Teacher Liu is an Aoshu teacher. He just comes out with a pro

hdu 4403 A very hard Aoshu problem

hdu 4403 A very hard Aoshu problem 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4403 DFS 这几天集训,一天也就写个4题,被虐哭QAQ.回寝室后游少说解搜索就大胆搜,最后剪个枝就好了Orz,然后我就尝试解这题(剪枝要风骚).我先枚举等号的位置equ,然后搜索加号add的位置,然后主要的剪枝:如果等号左边运算后小于等号右边各个位上的数的和,那么后面的肯定不满足条件,右边同理.最后要小心爆int,这里吃了很多W

HDU 4403 A very hard Aoshu problem(DFS+暴力)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4403 Problem Description Aoshu is very popular among primary school students. It is mathematics, but much harder than ordinary mathematics for primary school students. Teacher Liu is an Aoshu teacher. He

hdu - 4403 - A very hard Aoshu problem(dp + dfs + map + 乘法原理)

题意:给出一串数字字符(长度在[2, 15]),现要在其中加一个 "=",不加或加一些 "+",问成立的等式有多少条? 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4403 -->>数据量较小,暴力吧.. 1.dp预处理出任意两个字符间的数值大小 2.枚举 = 的位置,分别对 = 的左边.右边各dfs一次,并记录各个数出现的次数 3.根据乘法原理求组合数 #include <cstdio> #

HDU 4403 A very hard Aoshu problem (DFS暴力)

题意:给你一个数字字符串.问在字符串中间加'='.'+'使得'='左右两边相等. 1212  : 1+2=1+2,   12=12. 12345666 : 12+3+45+6=66.  1+2+3+4+56=66: #include<stdio.h> #include<string.h> #include<string> #include<map> #include<stack> #include<math.h> #include&l

A very hard Aoshu problem(dfs或者数位)

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4403 A very hard Aoshu problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1080    Accepted Submission(s): 742 Problem Description Aoshu is ver

[HDU 5135] Little Zu Chongzhi&#39;s Triangles (dfs暴搜)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5135 题目大意:给你n条边,选出若干条边,组成若干个三角形,使得面积和最大.输出最大的面积和. 先将边从小到大排序,这样前面的两条边加起来如果不大于第三条边就可以跳出,这是一个存在性条件. dfs(int idx,int now,int cnt,int nowmax)代表我当前处理的是第idx条边,已经加入边集的有cnt条边,当前的边的长度和为now,组成的最大面积和为nowmax. 暴力枚举每个三

HDU4403 A very hard Aoshu problem DFS

A very hard Aoshu problem                           Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description Aoshu is very popular among primary school students. It is mathematics, but much harder than o