hdu 4403 dfs

巨麻烦的模拟题orz。。。。

先确定等号的位置,然后两层dfs,分别算等号前面的和后面的,再比较

话说这题竟然不开long long都能水过

  1 #include <iostream>
  2 #include<cstring>
  3 using namespace std;
  4 int ANS,ansx,ansy,T,l;
  5 string s;
  6 int nx[1000],ny[1000],ax[1000],ay[1000],a[1000];
  7 int lx,ly;
  8 
  9 int calcx()
 10 {
 11     //ax[1..lx-1]
 12     //nx[1..lx]
 13     int tmpn=nx[1];
 14     int tmpa=0;
 15     for(int i=1;i<=lx-1;i++)
 16     {
 17         if(ax[i]==1)
 18         {
 19             tmpa+=tmpn;
 20             tmpn=nx[i+1];
 21         }
 22         else
 23         {
 24             tmpn=tmpn*10+nx[i+1];
 25         }
 26     }
 27     tmpa+=tmpn;
 28     return tmpa;
 29 }
 30 
 31 int calcy()
 32 {
 33     //ay[1..ly-1]
 34     //ny[1..ly]
 35     int tmpn=ny[1];
 36     int tmpa=0;
 37     for(int i=1;i<=ly-1;i++)
 38     {
 39         if(ay[i]==1)
 40         {
 41             tmpa+=tmpn;
 42             tmpn=ny[i+1];
 43         }
 44         else
 45         {
 46             tmpn=tmpn*10+ny[i+1];
 47         }
 48     }
 49     tmpa+=tmpn;
 50     return tmpa;
 51 }
 52 
 53 void dfsy(int y)
 54 {
 55     if(y==ly)
 56     {
 57         //cout<<nx[1]<<" ";
 58         //for(int i=1;i<=lx-1;i++)  cout<<char(ax[i]+'A')<<" "<<nx[i+1]<<" ";
 59         //cout<<"----";
 60         //cout<<ny[1]<<" ";
 61         //for(int i=1;i<=ly-1;i++)  cout<<char(ay[i]+'A')<<" "<<ny[i+1]<<" ";
 62 
 63         ansy=calcy();
 64         //cout<<"  "<<ansx<<"=="<<ansy<<" ";
 65         if(ansy==ansx)
 66         {
 67             ANS++;
 68         //    cout<<" OK! ";
 69         }
 70         //cout<<endl;
 71     }
 72     else
 73     {
 74         ay[y]=1;
 75         dfsy(y+1);
 76         ay[y]=0;
 77         dfsy(y+1);
 78     }
 79 }
 80 
 81 void dfsx(int x)
 82 {
 83     if(x==lx)
 84     {
 85         ansx=calcx();
 86         dfsy(1);
 87     }
 88     else
 89     {
 90         ax[x]=1;        //add a plus after ax[x]
 91         dfsx(x+1);
 92         ax[x]=0;
 93         dfsx(x+1);
 94     }
 95 }
 96 
 97 int main()
 98 {
 99     while(cin>>s)
100     {
101         if(s=="END")    break;
102         l=s.length();
103         ANS=0;  ansx=0;  ansy=0;
104         for(int i=1;i<=l;i++)
105             a[i]=s[i-1]-'0';
106         for(int eq=1;eq<=l-1;eq++)     //add equal after a[eq]
107         {
108             memset(nx,0,sizeof(nx));
109             memset(ny,0,sizeof(ny));
110             for(int i=1;i<=eq;i++)      nx[i]=a[i];
111             lx=eq;
112             for(int i=eq+1;i<=l;i++) ny[i-eq]=a[i];
113             ly=l-eq;
114             dfsx(1);
115         }
116         cout<<ANS<<endl;
117     }
118 
119     return 0;
120 }
View Code
原文地址:https://www.cnblogs.com/pdev/p/4517778.html