hdu4403- A very hard Aoshu problem(搜索)

枚举等号的位置,然后暴力搜索一波 这个题本身不难,但它是我第一次使用对拍程序来查找错误,值得纪念。

 1 #include<cstdio>
 2
 3 #include<string.h>
 4
 5 #include<map>
 6
 7 #include<algorithm>
 8
 9 #define inf 0x3f3f3f3f
10
11 const int maxn=100;
12
13 using namespace std;
14
15 typedef pair<int,int> P;
16
17 int len,d,b,c,ans;
18
19 char a[maxn+10];
20
21 map<P,int> m1,m2;
22
23 int cal(int l,int r){
24    if(l>r) return 0;
25    int res=0;
26    for(int i=l;i<=r;i++){
27         res=res*10+a[i]-‘0‘;
28    }
29    return res;
30 }
31
32 void dfs(int l,int r,int sum,int f,int eq){
33     if(!f){
34         if(l>r){
35            m1[P(sum,eq)]++;
36            return ;
37         }
38         for(int i=l;i<=r;i++){
39             dfs(i+1,r,sum+cal(l,i),0,eq);
40         }
41     } else {
42         if(l>r){
43            m2[P(sum,eq)]++;
44            return ;
45         }
46         for(int i=l;i<=r;i++){
47                 dfs(i+1,r,sum+cal(l,i),1,eq);
48         }
49
50     }
51 }
52
53 int main()
54 {
55    // freopen("e://duipai//data.txt","r",stdin);
56    // freopen("e://duipai//out1.txt","w",stdout);
57     while(scanf("%s",a)!=EOF){
58         if(a[0]==‘E‘)
59         break;
60         len=strlen(a);
61         ans=0;
62         for(int i=1;i<len;i++){
63            m1.clear();
64            dfs(0,i-1,0,0,i);
65            m2.clear();
66            dfs(i,len-1,0,1,i);
67            map<P,int>::iterator it=m1.begin();
68                 for(;it!=m1.end();++it){
69                 int a=m1[P((*it).first.first,(*it).first.second)];
70                 int b=m2[P((*it).first.first,(*it).first.second)];
71                 if(a&&b) {
72                 ans+=(a*b);
73                 }
74            }
75         }
76         printf("%d\n",ans);
77     }
78     return 0;
79 }
时间: 2024-10-24 18:53:41

hdu4403- A very hard Aoshu problem(搜索)的相关文章

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

HDU 3699 A hard Aoshu Problem (暴力搜索)

题意:题意:给你3个字符串s1,s2,s3;要求对三个字符串中的字符赋值(相同的字符串进行相同的数字替换), 替换后的三个数进行四则运算要满足左边等于右边,求有几种解法. Sample Input 2 A A A BCD BCD B Sample Output 5 72 eg:ABBDE   ABCCC   BDBDE :令 A = 1, B = 2, C = 0, D = 4, E = 5 12245 + 12000 = 24245: 注意没有前导零!! #include<stdio.h>

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

[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

hdu3699 A hard Aoshu Problem 暴搜

http://acm.hdu.edu.cn/showproblem.php?pid=3699 A hard Aoshu Problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 62768/32768 K (Java/Others) Total Submission(s): 968    Accepted Submission(s): 490 Problem Description Math Olympiad is calle

hdu 3699 10 福州 现场 J - A hard Aoshu Problem

Description Math Olympiad is called “Aoshu” in China. Aoshu is very popular in elementary schools. Nowadays, Aoshu is getting more and more difficult. Here is a classic Aoshu problem: ABBDE __ ABCCC = BDBDE In the equation above, a letter stands for

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 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