zoukankan      html  css  js  c++  java
  • HDU 4403 dfs

    A very hard Aoshu problem

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


    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
     题意:
    最长是15的字符串,要求添加一个‘=’和若干个‘+’使等式成立的方案数
    代码:
    //字符串最长是15,纯暴力,两个dfs。
    #include<iostream>
    #include<cstdio>
    #include<cstring>
    using namespace std;
    char ch[20];
    int len,ans;
    void dfs2(int R,int k,int l,int pre,int m,int sum){
        if(l>R){
            if(m+pre==sum&&k==0) ans++;
            return;
        }
        pre=pre*10+ch[l]-'0';
        if(k>0&&pre!=0&&l!=R) dfs2(R,k-1,l+1,0,m+pre,sum);
        dfs2(R,k,l+1,pre,m,sum);
    }
    void solve(int l,int sum){
        for(int k=0;k<=len-1-l;k++){
            dfs2(len-1,k,l,0,0,sum);
        }
    }
    void dfs1(int R,int k,int l,int pre,int m){
        if(l>R){
            if(k==0) solve(R+1,m+pre);
            return;
        }
        pre=pre*10+ch[l]-'0';
        if(k>0&&pre!=0&&l!=R) dfs1(R,k-1,l+1,0,m+pre);
        dfs1(R,k,l+1,pre,m);
    }
    int main()
    {
        while(scanf("%s",ch)&&(strcmp(ch,"END")!=0)){
            len=strlen(ch);
            ans=0;
            for(int h=0;h<len-1;h++){
                for(int k=0;k<=h;k++){
                    dfs1(h,k,0,0,0);
                }
            }
            printf("%d
    ",ans);
        }
        return 0;
    }
  • 相关阅读:
    绕口令系列 1
    毕业论文排版
    使用matlab表示“段数不确定”的分段函数
    [转]C/C++关于全局变量和局部变量初始化与不初始化的区别
    [转]基于Protel DXP软件的PCB高级编辑技巧大全
    冒泡排序及其优化
    gcc编译器参数
    [转]跟我一起写Makefile系列
    实例说明optimize table在优化mysql时很重要
    log4php0.9的详细配备实例说明
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/6769796.html
Copyright © 2011-2022 走看看