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>
#include <cstring>
#include <map>

using std::map;

const int MAXN = 15 + 10;

char s[MAXN];
int len;
int num[MAXN][MAXN];
map<int, int> mpLeft;
map<int, int> mpRight;

void Init()
{
    len = strlen(s);
    for (int i = 0; i < len; ++i)
    {
        for (int j = 0; j < len; ++j)
        {
            num[i][j] = j == i ? s[i] - '0' : num[i][j - 1] * 10 + s[j] - '0';
        }
    }
}

void Dfs(int cur, int goal, int sum, map<int, int>& mp)
{
    if (cur == goal)
    {
        ++mp[sum];
        return;
    }
    for (int i = cur; i < goal; ++i)
    {
        Dfs(i + 1, goal, sum + num[cur][i], mp);
    }
}

void Solve()
{
    int ret = 0;

    for (int i = 1; i < len; ++i)
    {
        mpLeft.clear();
        mpRight.clear();
        Dfs(0, i, 0, mpLeft);
        Dfs(i, len, 0, mpRight);
        for (map<int, int>::const_iterator iter = mpLeft.begin(); iter != mpLeft.end(); ++iter)
        {
            if (mpRight.find(iter->first) != mpRight.end())
            {
                ret += iter->second * mpRight[iter->first];
            }
        }
    }

    printf("%d\n", ret);
}

int main()
{
    while (scanf("%s", s) == 1)
    {
        if (strcmp(s, "END") == 0) break;
        Init();
        Solve();
    }
    return 0;
}
时间: 2024-10-12 20:24:16

hdu - 4403 - A very hard Aoshu problem(dp + dfs + map + 乘法原理)的相关文章

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(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

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)

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

[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

【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

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

HDU 4971 A simple brute force problem.(dp)

HDU 4971 A simple brute force problem. 题目链接 官方题解写的正解是最大闭合权,但是比赛的时候用状态压缩的dp也过掉了- -,还跑得挺快 思路:先利用dfs预处理出每个项目要完成的技术集合,那么dp[i][j]表示第i个项目,已经完成了j集合的技术,由于j这维很大,所以利用map去开数组 代码: #include <cstdio> #include <cstring> #include <algorithm> #include &l