hdu4403A very hard Aoshu problem 线段树

//给一个长度为大于2小于15的字符串
//在其中间加‘+’或‘=’使得其成为一个等式的方法的个数
//枚举等号位置,暴力搜索加号加的位置
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std ;
const int maxn = 20;
typedef __int64 ll ;
int ans;
int a[maxn] ,len_a;
int b[maxn] , len_b ;
char str[maxn] , len;
bool judge(int pos)
{
    ll ans_1 = 0 ,ans_2 = 0;
    int pre = 1 ;
    for(int i = 1;i <= len_a;i++)
    {
        ll temp = 0;
        for(int j = pre ; j <= (i == len_a ? pos :a[i]) ; j++)
        temp = temp*10 + str[j - 1] - '0' ;
        ans_1 += temp ;
        pre = a[i] + 1 ;
    }
    pre = pos + 1 ;
    for(int i = 1;i <= len_b ;i++)
    {
        ll temp = 0 ;
        for(int j = pre ; j <= (i == len_b?len :b[i]) ;j++)
        temp = temp*10 + str[j - 1] - '0' ;
        ans_2 += temp ;
        pre = b[i] + 1 ;
    }
    return (ans_1 == ans_2);
}
void dfs(int pos , int step)
{
    if(step == (len))
    {
        if(judge(pos))
        ans ++ ;
        return ;
    }
    if(step < pos)
    {
        a[len_a++] = step ;
        dfs(pos , step+1) ;
        len_a--;
    }
    if(step > pos)
    {
        b[len_b++] = step ;
        dfs(pos , step+1) ;
        len_b-- ;
    }
    dfs(pos , step+1) ;
}
int main()
{
    //freopen("in.txt" , "r" , stdin) ;
    while(scanf("%s" ,str))
    {
        if(str[0] == 'E')
        break;
        len = strlen(str) ;
        ans = 0 ;len_a = len_b = 1 ;
        for(int i = 1;i < len ;i++)
        dfs(i , 1) ;
        printf("%d\n" , ans) ;
    }
    return  0 ;
}

你可能感兴趣的:(暴力枚举)