zoukankan      html  css  js  c++  java
  • hdu 2266 dfs+1258

    How Many Equations Can You Find

    Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
    Total Submission(s): 985    Accepted Submission(s): 659


    Problem Description
    Now give you an string which only contains 0, 1 ,2 ,3 ,4 ,5 ,6 ,7 ,8 ,9.You are asked to add the sign ‘+’ or ’-’ between the characters. Just like give you a string “12345”, you can work out a string “123+4-5”. Now give you an integer N, please tell me how many ways can you find to make the result of the string equal to N .You can only choose at most one sign between two adjacent characters.
     
    Input
    Each case contains a string s and a number N . You may be sure the length of the string will not exceed 12 and the absolute value of N will not exceed 999999999999.
     
    Output
    The output contains one line for each data set : the number of ways you can find to make the equation.
     
    Sample Input
    123456789 3 21 1
     
    Sample Output
    18 1
     大意:
    给出一个字符串,往任意二个字符间添加‘+’‘-‘,使得其值等于给定的aim,求方案个数,字符串前后均无符号;
    自己第一次使用dfs保存一个加工过的字符串,再用一个pd函数判定此字符串的和;
    其实可以直接暴力dfs水果,更省时:                      //ps:55555555~~~感觉自己好傻

    #include<bits/stdc++.h>
    using namespace std;
    char num[15],b[105];
    int aim,ans,n;
    void dfs(int cur,int sum)
    {
    if(cur==n){
    if(sum==aim) ans++;
    return;
    }
    int t=0;
    for(int i=cur;i<n;i++){
    t=t*10+num[i]-'0';                         //其实就是暴力枚举出所有的+-情况
    dfs(i+1,sum+t);
    if(cur>0) dfs(i+1,sum-t);
    }
    }


    int main()
    {
    while(cin>>num>>aim/*scanf("%s%d",num,&aim)!=EOF*/){
    n=strlen(num);
    ans=0;
    dfs(0,0);
    printf("%d ",ans);
    }
    return 0;
    }

    例如 1234
    也是关于深搜的一些看法吧:
    此类问题类似“决策”,就是每一次/每一个位数对应着几个状态,取/不取或/////
    想到之前的全排列问题也是如此,只不过那个问题所有的位置都要有数字只不过要求顺序不同和去重:
    这道题与1258有点类似吧
    1258是每一位的数字加/不加,且要去重:由于每次考虑两个状态:加/不加,如果相连的几个数字都是同一个数字的话会造成重复解,
    每次两个状态,展开后就是一颗二叉树:假设有4个数abcd(*表示不加)
                                      a                                                     *
                            b                *                                       b                  *
                         c     *           c     *                              c      *           c      *
                     d   *   d   *    d   *   d   *                     d    *  d   *    d   *    d   *
     
    每个字母下面对应的两个状态如此,如果假设a==b则:
                                      a                                                     *
                            a                *                                       a                 *
                         c     *           c     *                              c      *           c      *
                     d   *   d   *    d   *   d   *                     d    *  d   *    d   *    d   *
    会发现左边的右树和右边的左树是重复的,只要出现重复得数就会出现此情况,另一颗树的一颗子树必然会与另一颗的一颗子树相同结果,因为只是前两个位置调换后面出现的所有情况是一样的;
    这道题就是在任意相邻数字间加+-号;
  • 相关阅读:
    python爬虫
    RMQ算法
    组合数
    水池数目
    jQuery 拼接事件
    ORACLE
    day 75
    day74 vue框架
    day73 vue框架
    day 72 vue框架
  • 原文地址:https://www.cnblogs.com/zzqc/p/6523527.html
Copyright © 2011-2022 走看看