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 ordinary mathematics for primary school students. Teacher Liu is an Aoshu teacher. He just comes out with a problem to test his students:

Given a serial of digits, you must put a '=' and none or some '+' between these digits and make an equation. Please find out how many equations you can get. For example, if the digits serial is "1212", you can get 2 equations, they are "12=12" and "1+2=1+2". Please note that the digits only include 1 to 9, and every '+' must have a digit on its left side and right side. For example, "+12=12", and "1++1=2" are illegal. Please note that "1+11=12" and "11+1=12" are different equations.
 

 

Input
There are several test cases. Each test case is a digit serial in a line. The length of a serial is at least 2 and no more than 15. The input ends with a line of "END".
 

 

Output
For each test case , output a integer in a line, indicating the number of equations you can get.
 

 

Sample Input
1212 12345666 1235 END
 

 

Sample Output
2 2 0
 

 

Source
题解:枚举等号
  等号两边暴力dfs
//1085422276
#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#include<algorithm>
#include<queue>
#include<cmath>
#include<map>
#include<bitset>
#include<set>
#include<vector>
using namespace std ;
typedef long long ll;
#define mem(a) memset(a,0,sizeof(a))
#define meminf(a) memset(a,127,sizeof(a));
#define memfy(a) memset(a,-1,sizeof(a))
#define TS printf("111111\n");
#define FOR(i,a,b) for( int i=a;i<=b;i++)
#define FORJ(i,a,b) for(int i=a;i>=b;i--)
#define READ(a,b,c) scanf("%d%d%d",&a,&b,&c)
#define mod 1000000007
#define maxn 106
inline ll read()
{
    ll x=0,f=1;
    char ch=getchar();
    while(ch<'0'||ch>'9')
    {
        if(ch=='-')f=-1;
        ch=getchar();
    }
    while(ch>='0'&&ch<='9')
    {
        x=x*10+ch-'0';
        ch=getchar();
    }
    return x*f;
}
//****************************************
int n,m,ans,fsum;
vector<int >G[5];
char a[41];
void dfs1(int x,int right,int sum,int last)
{
    if(x==right)
    {
        if(!last)
        G[1].push_back(sum+last);
        return ;
    }
    dfs1(x+1,right,sum+last*10+a[x],0);///f
    dfs1(x+1,right,sum,last*10+a[x]);
}
void dfs2(int x,int right,int sum,int last)
{
    if(x==right)
    {if(!last)
       G[2].push_back(sum+last);
        return ;
    }
    dfs2(x+1,right,sum+last*10+a[x],0);///f
    dfs2(x+1,right,sum,last*10+a[x]);
}
int main()
{

    while(scanf("%s",a)!=EOF)
    {
        ans=0;
        if(strcmp(a,"END")==0)
            break;
        n=strlen(a);
        FOR(i,0,n-1)a[i]=a[i]-'0';
        FOR(i,1,n-1)
        {
            G[1].clear();G[2].clear();
            if(i==1) G[1].push_back(a[0]);
            else dfs1(0,i,0,0);
            if(i==n-1) G[2].push_back(a[i]);
           else dfs2(i,n,0,0);
           for(int j=0;j<G[1].size();j++)
            for(int k=0;k<G[2].size();k++)
           {
               if(G[1][j]==G[2][k])ans++;
           }
        }
       cout<<ans<<endl;
        ///getchar();
    }
    return 0;
}
代码

 

posted @ 2015-09-14 22:02  meekyan  阅读(259)  评论(0编辑  收藏  举报