hdu4403A 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
 

题意:给定一个字符串,由1~9组成,问你能够找到多少种情况,使得添加‘+’和‘=’后,等号左右两边相等

思路:枚举每一个等号出现的位置,将等号左边左右可能出现的和,存在map里面,然后再看看等号右边有多少种和,判断每一个和是否在左边出现过,进行统计所有合法的结果,具体如何搜索看代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include <cmath>
#include <queue>
#include <stack>
#include <map>
#include <vector>
#include <algorithm>
#include <set>

using namespace std;

char s[16];
map<int,int>mp;
int len;
int num[16][16];
int cnt;

void getnum() {//获得每个位置到最后可能出现所有的数的大小
    for(int i = 0; i < len; i++) {
        int temp = 0;
        for(int j = i; j < len; j++) {
            temp += s[j]-'0';
            num[i][j] = temp;
            temp *= 10;
        }
    }
}

void dfs1(int tol,int x,int mid) {//搜索左边的所有可能出现的和,tol表示和,x表示当前搜在第几个数
    if(x == mid) {
        mp[tol]++;
        return;
    }
    for(int i = x; i < mid; i++) {
        dfs1(tol+num[x][i],i+1,mid);
    }
}
void dfs2(int tol,int x) {
    if(x == len) {
        if(mp[tol]) {
            cnt += mp[tol];
        }
        return;
    }
    for(int i = x; i < len; i++) {
        dfs2(tol+num[x][i],i+1);
    }
}

int main() {
    while(cin >> s) {
        if(strcmp(s,"END") == 0) {
            break;
        }
        len = strlen(s);
        getnum();
        cnt = 0;
        for(int i = 1; i < len; i++) {
            mp.clear();
            dfs1(0,0,i);
            dfs2(0,i);
        }
        cout << cnt << endl;
    }
    return 0;
}


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值