• 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 }
  • 相关阅读:
    调用微信扫一扫接口
    Http错误代码解释
    php goto的用法
    Yii2.0-生成二维码实例
    飞鹅WiFi打印机配置,php调用接口
    HTML5 为什么只需要写 <!DOCTYPE HTML>?
    VueJs之 v-bind
    react 组件导出
    前端代码规范
    git与github的文件推送
  • 原文地址:https://www.cnblogs.com/qijinbiao/p/2698512.html
Copyright © 2020-2023  润新知