唔...好久没写博客了,上大学久了,人也变懒了...不行,以后还是要坚持写。
翻了翻OJ上提交了但是没有过的题目,对于我这种有强迫症的人来说,每多一道unsolved都会让我感觉很不舒服,然后随便找了道切了下
做这题做之前还是要想清楚,因为这题做起来注意的细节稍微有点多,之前敲的dfs,感觉有点漏洞,然后想了想又换成了二进制枚举。
这题数据范围小,毫无疑问是枚举;感觉比较好的做法是枚举等号的位置,然后分别求出等号两边分别能组成什么数,那么这里就可以用到dfs或者二进制枚举加号的位置,需要注意的细节是不要算重复了。
#include <map>
#include <cstdio>
#include <cstring>
#include <algorithm>
#define se second
#define fi first
using namespace std;
typedef __int64 LL;
char str[22];
LL getNumber(int mask, int len, int i) {
int t = 1 << (len - 2);
LL sum = str[i++] - '0', res = 0;
while(t) {
if(mask & t) {
res += sum;
sum = str[i] - '0';
} else {
sum = sum * 10 + str[i] - '0';
}
i++, t >>= 1;
}
return res + sum;
}
void solve() {
int len = strlen(str);
int ans = 0;
for(int i = 0; i < len - 1; i++) {
int end = 1 << i;
map<LL, int> A, B;
if(i == 0) {
A[str[0] - '0']++;
end = 0;
}
for(int j = 0; j < end; j++) {
LL number = getNumber(j, i + 1, 0);
A[number]++;
}
end = 1 << (len - i - 2);
if(len - i - 1 == 1) {
B[str[len - 1] - '0']++;
end = 0;
}
for(int j = 0; j < end; j++) {
LL number = getNumber(j, len - i - 1, i + 1);
B[number]++;
}
for(map<LL, int>::iterator it = A.begin(); it != A.end(); it++) {
ans += it->se * B[it->fi];
}
}
printf("%d\n", ans);
}
int main() {
while(scanf("%s", str)) {
if(str[0] == 'E') break;
solve();
}
return 0;
}