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;
    }
  • 相关阅读:
    数据库被注入daxia123原因及解决办法
    Alipay数字证书管理员权限问题
    关闭数据库的xp_cmdshell命令以防止黑客攻击
    如何使用JavaScript来写ASP程序
    VBscript操作DOM
    如何做好性能压测丨压测环境设计和搭建
    10倍性能提升!DLA SQL推出基于Alluxio的数据湖分析加速功能
    高德地图驾车导航内存优化原理与实战
    「直播实录」中英数据库专家谈:数据库的过去、未来和现在
    Flink 助力美团数仓增量生产
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/6769796.html
Copyright © 2011-2022 走看看