zoukankan      html  css  js  c++  java
  • hdu 4403 A very hard Aoshu problem (暴力枚举 2012 金华网络赛 )

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

    题意:

    给定 一个 数字字符串 ,在这些 字符间添加  一个 加号 和 多个 等号 求 有多少种 方法 得到 等式  使得左右相等 如 1212

    可以  分解为

    12=12" and "1+2=1+2".

    题解 :

      暴力枚举 ,因为i额 数据并不大 最多 15 位数 所以 枚举就可以 

      1 #include<cstdio>
      2  #include<cstring>
      3  #include<cmath>
      4  #include<iostream>
      5  #include<algorithm>
      6  #include<set>
      7  #include<map>
      8  #include<queue>
      9  #include<vector>
     10  #include<string>
     11  #define Min(a,b) a<b?a:b
     12  #define Max(a,b) a>b?a:b
     13  #define CL(a,num) memset(a,num,sizeof(a));
     14  #define eps  1e-12
     15  #define inf 100000000
     16  #define mx 1<<60
     17  #define ll   __int64
     18  const double pi  = acos(-1.0);
     19  const int maxn = 500000;
     20  using namespace std;
     21  int a[20] ;
     22  int p[maxn],q[maxn] ;
     23  int cnt1,cnt2;
     24  void dfs1(int x,int num,int sum,int pre)
     25  {
     26 
     27      if(num == 0)
     28      {
     29          if(pre == -1) pre = 0;
     30          p[cnt1++] = sum  + pre;
     31         
     32          return ;
     33      }
     34      if(pre == -1)// 表示 是 这个数 前面 插入了一个 +
     35      {
     36 
     37            dfs1(x + 1,num - 1,sum, a[x]);// 
     38 
     39 
     40      }
     41      else
     42      {
     43           dfs1(x + 1,num - 1,sum,pre*10 + a[x]);//前面不是  + 两种选择
     44 
     45          dfs1(x,num,sum + pre,-1) ;
     46      }
     47 
     48 
     49 
     50  }
     51  void dfs2(int x,int num,int sum,int pre)
     52  {
     53      if(num == 0)
     54      {
     55         if(pre == -1) pre = 0;
     56 
     57          q[cnt2++] = sum  + pre ;
     58          return ;
     59      }
     60       if(pre == -1)
     61      {
     62 
     63            dfs2(x + 1,num - 1,sum, a[x]);
     64 
     65 
     66      }
     67      else
     68      {
     69           dfs2(x + 1,num - 1,sum,pre*10 + a[x]);
     70 
     71           dfs2(x,num,sum + pre,-1) ;
     72      }
     73  }
     74  int main()
     75  {
     76      int i , j,k ;
     77      char c[20] ;
     78      while(scanf("%s",c)!=EOF)
     79      {
     80          if(strcmp(c,"END") == 0break ;
     81 
     82          int len = strlen(c);
     83          for(i = 0; i < len;i++)
     84          {
     85              a[i] = c[i] - '0' ;
     86          }
     87 
     88          int ans= 0 ;
     89          for(i = 1 ; i <= len - 1;i++)
     90          {   cnt1 = 0;
     91              cnt2 = 0 ;
     92              dfs1(0,i,0,-1);
     93 
     94              dfs2(i,len - i,0,-1) ;
     95              for(j = 0 ; j< cnt1;j++)
     96              {
     97                  for(k = 0;  k< cnt2;k++)
     98                  {
     99                      if(p[j] == q[k] )ans++;
    100                  }
    101              }
    102 
    103 
    104 
    105          }
    106 
    107 
    108 
    109 
    110          printf("%d\n",ans) ;
    111 
    112      }
    113  }


  • 相关阅读:
    xadmin修改list_export_fields不生效以及添加exclude_export_fields功能
    Python正则总结
    Ubuntu安装Gitlab简记
    pytest使用总结笔记
    unittest使用数据驱动ddt
    unittest使用总结
    HTTP知识点总结
    HTTPS加密流程理解
    Requests库使用总结
    leetcode 76:最小字符串
  • 原文地址:https://www.cnblogs.com/acSzz/p/2698757.html
Copyright © 2011-2022 走看看