zoukankan      html  css  js  c++  java
  • 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 " , ans) ; } return 0 ; }

  • 相关阅读:
    封装小程序http请求
    ES6为数组做的扩展
    练习题
    二叉树的遍历
    快速搭建vue项目
    收集的前端面试大全
    ios兼容webp格式的图片
    小程序开发API之获取启动参数
    使用HTML编写邮件
    深入理解javascript原型和闭包(9)——this
  • 原文地址:https://www.cnblogs.com/brucemengbm/p/7387639.html
Copyright © 2011-2022 走看看