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 ; }

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值