-
Notifications
You must be signed in to change notification settings - Fork 0
/
zero.cpp
78 lines (72 loc) · 2.15 KB
/
zero.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
#include <functional>
#include <sstream>
using namespace std;
int strToInt(string str) {
int res = 0;
int base = 1;
for (int i = str.size() - 1; i >= 0; i--) {
res += base * (str[i] - '0');
base *= 10;
}
return res;
}
void recursive(int t, string s, vector<string> &result, vector<int> bits) {
if (t == s.size()) {
int curIdx = 0, crawl, res = 0;
while(curIdx < t) {
crawl = curIdx + 1;
if (s[curIdx] != '0') {
while(crawl < t && bits[crawl] == 0) crawl++;
int tmp = strToInt(s.substr(curIdx, crawl - curIdx));
if (bits[curIdx] == 1) res += tmp;
else if (bits[curIdx] == 2) res -= tmp;
}
curIdx = crawl;
}
if (res == 0) {
curIdx = 0;
string final = "";
while(curIdx < t) {
crawl = curIdx + 1;
if (s[curIdx] != '0') {
while(crawl < t && bits[crawl] == 0) crawl++;
string str = s.substr(curIdx, crawl - curIdx);
if (bits[curIdx] == 1 && curIdx != 0) final += '+';
else if (bits[curIdx] == 2) final += '-';
final += str;
} else {
if (bits[curIdx] == 1 && curIdx != 0) final += '+';
else if (bits[curIdx] == 2) final += '-';
final += '0';
}
curIdx = crawl;
}
result.push_back(final);
}
return;
}
for (int i = 0; i < 3; i++) {
if (t == 0 && i == 0) continue;
if (s[t] == '0' && i == 0) continue;
bits[t] = i;
recursive(t + 1, s, result, bits);
}
}
vector<string> zero(string s) {
vector<string> result;
vector<int> bits(s.size(), 0);
recursive(0, s, result, bits);
sort(result.begin(), result.end());
return result;
}
int main() {
vector<string> res = zero("102003");
for (string x : res) {
cout << x << endl;
}
return 0;
}