HDU4403 A very hard Aoshu problem

搜索提,等号只有一个可以剪枝

 1 #include <iostream>
 2 #include <queue>
 3 #include <cstring>
 4 #include <cstdio>
 5 using namespace std;
 6 char seq[50];
 7 char str[50];
 8 int n,ans,eq;
 9 bool ok(int len)
10 {
11     long long a,sum=0;
12     int i,flg=1;
13     queue<char> q;
14     q.push('+');
15     seq[len]='\0';
16     for(i=0; i<len; i++)
17     {
18         a=0;
19         while(i<=len&&seq[i]>='0'&&seq[i]<='9') a=a*10+seq[i]-'0',i++;
20         q.push(seq[i]);
21         if(flg&&q.front()=='+')
22             sum+=a;
23         else
24             flg=0,sum-=a;
25         q.pop();
26     }
27     if(sum==0) return 1;
28     else return 0;
29 }
30 void dfs(int cur,int q)
31 {
32     if(eq>1) return;
33     if(q>n)
34     {
35         if(ok(cur)) ans++;
36         return;
37     }
38     seq[cur]=str[q];
39     dfs(cur+1,q+1);
40     seq[cur]='+';
41     seq[cur+1]=str[q];
42     dfs(cur+2,q+1);
43     seq[cur]='=';
44     seq[cur+1]=str[q];
45     eq++;
46     dfs(cur+2,q+1);
47     eq--;
48 }
49 int main()
50 {
51     while(cin>>str,str[0]!='E')
52     {
53         ans=0;eq=0;
54         seq[0]=str[0];
55         n=strlen(str)-1;
56         dfs(1,1);
57         cout<<ans<<endl;
58     }
59     return 0;
60 }

 

 

 

posted @ 2012-09-22 22:28  qijinbiao1  阅读(791)  评论(0编辑  收藏  举报