A very hard Aoshu problem

D - A very hard Aoshu problem
Time Limit:1000MS     Memory Limit:32768KB     64bit IO Format:%I64d & %I64u
Submit  Status  Practice  HDU 4403

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

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <map>

using namespace std;
#define N 20

char s[N];
int a[N], len;
map<int, int>ml, mr;

void dfs(int pos, int x, int v, int res, map<int, int> &m)
{
    if(pos == x)
    {
        int val = res + v * 10 + a[pos];
        m[val]++;
        return ;
    }
    dfs(pos + 1, x, 0, res + v * 10 + a[pos], m);
    dfs(pos + 1, x, v * 10 + a[pos], res, m);
}

int solve(int x)
{
    int res = 0;
    ml.clear();
    mr.clear();
    dfs(0, x, 0, 0, ml);
    dfs(x + 1, len - 1, 0, 0, mr);
    for(map<int, int>::iterator it = ml.begin(); it != ml.end(); it++)
        res += (it->second) * mr[(it->first)];

    return res;
}

int main()
{
    while(~scanf("%s", s) && s[0] != 'E')
    {
        int ans = 0;
        len = strlen(s);
        for(int i = 0; i < len; i++) a[i] = s[i] - '0';
        for(int i = 0; i < len - 1; i++)
            ans += solve(i);
        printf("%d\n", ans);
    }
    return 0;
}


你可能感兴趣的:(ACM)