HDU 4403 A very hard Aoshu problem(DFS)

A very hard Aoshu problem

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

Answer

先预处理出第i位到第j位的数字是什么(sum数组),然后枚举插入等号的位置(两端不能插入等号),接下来dfs枚举等号左边的情况,每一种情况结束之后,继续dfs枚举等号右边的情况,同时将左边的情况(和)传递过去,等号右边每一种情况结束的时候,对比两个和,如果相等则答案加一。另外,枚举右边的情况的时候,可以加一个剪枝(不加也没事)。

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>
#include <queue>
#include <set>
#include <map>
#include <string>
#include <cmath>
#include <cstdlib>
#include <ctime>
#include <climits>
#define ms(a) memset(a,0,sizeof a)
using namespace std;
int sum[20][20];
vector<int> v;
string s;
int ans;
//处理等号右边
void dfs2(int lsu,int su,int l)//等号左边的和,和,起点
{
    if(lsu<su)return;
    if(l==(int)s.size()&&lsu==su)
    {
        ans++;
        return;
    }
    for(int i=l+1; i<=(int)s.size(); i++)
    {
        dfs2(lsu,su+sum[l][i-1],i);
    }
}
//处理等号左边
void dfs1(int su,int l,int r)//和,左,右界
{
    if(l==r)
    {
        dfs2(su,0,r);
        return;
    }
    for(int i=l+1; i<=r; i++)
    {
        dfs1(su+sum[l][i-1],i,r);
    }
}
int main()
{
    string::iterator it1,it2;
    while(cin>>s)
    {
        if(s=="END")break;
        //ms(sum);
        ans=0;
        if(s.size()==1)printf("0\n");
        else
        {
            //预处理,得到某个区间的数值
            for(it1=s.begin(); it1!=s.end(); it1++)
            {
                int t=*it1-‘0‘;
                sum[it1-s.begin()][it1-s.begin()]=t;
                for(it2=it1+1; it2!=s.end(); it2++)
                {
                    t=t*10+(*it2-‘0‘);
                    sum[it1-s.begin()][it2-s.begin()]=t;
                }
            }
            //1=234566=6,等号的位置
            for(it1=s.begin()+1; it1!=s.end(); it1++)
            {
                dfs1(0,0,it1-s.begin());
            }
            printf("%d\n",ans);
        }
    }
    return 0;
}

时间: 2024-08-05 09:43:02

HDU 4403 A very hard Aoshu problem(DFS)的相关文章

[ACM] hdu 4403 A very hard Aoshu problem (DFS暴搜数字)

A very hard Aoshu problem 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 pro

hdu 4403 A very hard Aoshu problem

hdu 4403 A very hard Aoshu problem 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4403 DFS 这几天集训,一天也就写个4题,被虐哭QAQ.回寝室后游少说解搜索就大胆搜,最后剪个枝就好了Orz,然后我就尝试解这题(剪枝要风骚).我先枚举等号的位置equ,然后搜索加号add的位置,然后主要的剪枝:如果等号左边运算后小于等号右边各个位上的数的和,那么后面的肯定不满足条件,右边同理.最后要小心爆int,这里吃了很多W

HDU 4403 A very hard Aoshu problem(DFS+暴力)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4403 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

hdu - 4403 - A very hard Aoshu problem(dp + dfs + map + 乘法原理)

题意:给出一串数字字符(长度在[2, 15]),现要在其中加一个 "=",不加或加一些 "+",问成立的等式有多少条? 题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4403 -->>数据量较小,暴力吧.. 1.dp预处理出任意两个字符间的数值大小 2.枚举 = 的位置,分别对 = 的左边.右边各dfs一次,并记录各个数出现的次数 3.根据乘法原理求组合数 #include <cstdio> #

HDU 4403 A very hard Aoshu problem(dfs爆搜)

http://acm.hdu.edu.cn/showproblem.php?pid=4403 题意: 给出一串数字,在里面添加一个等号和多个+号,使得等式成立,问有多少种不同的式子. 思路: 数据量比较小,直接dfs爆搜答案即可. 1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<map> 6 using nam

HDU 4403 A very hard Aoshu problem (DFS暴力)

题意:给你一个数字字符串.问在字符串中间加'='.'+'使得'='左右两边相等. 1212  : 1+2=1+2,   12=12. 12345666 : 12+3+45+6=66.  1+2+3+4+56=66: #include<stdio.h> #include<string.h> #include<string> #include<map> #include<stack> #include<math.h> #include&l

A very hard Aoshu problem(dfs或者数位)

题目连接:http://acm.hdu.edu.cn/showproblem.php?pid=4403 A very hard Aoshu problem Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 1080    Accepted Submission(s): 742 Problem Description Aoshu is ver

HDU4403 A very hard Aoshu problem DFS

A very hard Aoshu problem                           Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others) Problem Description Aoshu is very popular among primary school students. It is mathematics, but much harder than o

【HDOJ】4403 A very hard Aoshu problem

HASH+暴力. 1 /* 4403 */ 2 #include <iostream> 3 #include <cstdio> 4 #include <cstring> 5 #include <cstdlib> 6 #include <map> 7 using namespace std; 8 9 #define MAXN 55 10 11 map<int, int> tb[2]; 12 char s[MAXN]; 13 14 int