HDU4403

http://acm.hdu.edu.cn/showproblem.php?pid=4403

 

#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include 

using namespace std;
typedef long long LL;

map cnt;

void dfs(string text, int pos, LL num, LL sum) {
    if (pos >= text.length()) {
        cnt[sum + num]++;
        return;
    }
    dfs(text, pos + 1, num * 10 + (text[pos] - '0'), sum);
    dfs(text, pos + 1, text[pos] - '0', sum + num);
}

map gao(string text) {
    cnt.clear();
    dfs(text, 1, text[0] - '0', 0);
    return cnt;
}

LL calc(string text) {
    int n = text.length();
    if (n <= 1) {
        return 0;
    }
    LL sum = 0;
    for (int i = 1; i <= n - 1; i++) {
        map lf = gao(text.substr(0, i));
        map rt = gao(text.substr(i, n - i));
        for (map::iterator it = lf.begin(); it != lf.end(); it++) {
            sum += it->second * rt[it->first];
        }
    }
    return sum;
}


int main() {
    std::ios::sync_with_stdio(false);
    string text;
    while (cin >> text) {
        if ("END" == text) {
            break;
        }
        cout << calc(text) << endl;
    }
    return 0;
}

 

 

你可能感兴趣的:(C++足迹)