• 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;
    }
  • 相关阅读:
    Stm32高级定时器(一)
    AES算法简介
    vsim仿真VHDL输出fsdb格式文件
    ncsim仿真VHDL
    云贵高原骑行
    触发器(笔记)
    几种常见的十进制代码(笔记)
    时序电路分类
    组合逻辑电路和时序逻辑电路比较
    数字电路基础(网络整理)
  • 原文地址:https://www.cnblogs.com/--ZHIYUAN/p/6769796.html
Copyright © 2020-2023  润新知