HDU4403

A - A very hard Aoshu problem
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u

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

昨天周赛的水搜索   然而………………

因为昨晚基地灯坏了,只能跟寝室里的煞笔一起打(当然他比我强,他10分钟A了   我越写越急

最后写了两个小时………………

不想多说什么了 先贴上源代码  再分析优化

#include <algorithm>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <queue>

using namespace std;
char str[20];
int flag[20];
int len, ans;

bool judge()
{
    int mid;
    for (mid = 0; mid < len; mid++)
        if (flag[mid] == 2)
            break;
    if (mid == len)
        return false;
    long long lans = str[0] - '0', rans = str[mid] - '0', i;
    for (i = 1; i < mid; i++)
        if (flag[i])
            break;
        else
            lans = lans * 10 + str[i] - '0';
    while (i < mid) {
        long long tmp = str[i] - '0';
        i++;
        while (flag[i] == 0) {
            tmp = tmp * 10 + str[i] - '0';
            i++;
        }
        lans += tmp;
    }
    for (i = mid + 1; i < len; i++)
        if (flag[i])
            break;
        else
            rans = rans * 10 + str[i] - '0';
    while (i < len) {
        long long tmp = str[i] - '0';
        i++;
        while (flag[i] == 0) {
            tmp = tmp * 10 + str[i] - '0';
            i++;
        }
        rans += tmp;
    }
    return lans == rans;
}

void dfs(int pos, bool has_equal)
{
    if (pos == len) {
        if (has_equal && judge())
            ans++;
        return;
    }
    if (has_equal) {
        flag[pos] = 1;
        dfs(pos + 1, true);
        flag[pos] = 0;
        dfs(pos + 1, true);
    } else {
        flag[pos] = 0;
        dfs(pos + 1, false);
        flag[pos] = 1;
        dfs(pos + 1, false);
        // if(pos==len-1) return;
        flag[pos] = 2;
        dfs(pos + 1, true);
    }
}

int main()
{
    while (scanf("%s", str) != EOF) {
        if (strcmp(str, "END") == 0)
            break;
        len = strlen(str), ans = 0;
        memset(flag, 0, sizeof flag);
        flag[len] = 1000;
        dfs(1, false);
        printf("%d\n", ans);
    }
    return 0;
}
我当时的思路很明显了  先用DFS放置所有 + = 的位置  到最后判断一些能不能使等式成立

因为前面想的少,搞得后面的bug处理特别多

尤其是判断那里 

对于优化建议   

1.完全可以预处理一下  比如定义一个二维数组  num[27][27]   用num[x][y]表示  x-y  的数字  然后只要判断加号位置就好了

2.另外的话可以以一个循环判断=的位置   再对左右按序DFS 即可

好吧  我已经理论优化了

最后想说的是     打ACM   心态很重要   心态爆炸了  这场基本也是完了














  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值