-
Notifications
You must be signed in to change notification settings - Fork 387
Expand file tree
/
Copy pathrecursive.cpp
More file actions
51 lines (51 loc) · 1.89 KB
/
recursive.cpp
File metadata and controls
51 lines (51 loc) · 1.89 KB
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
class Solution {
public:
vector<string> braceExpansionII(string s) {
stack<pair<bool, vector<string>>> ans; // bool -> isList
vector<string> init{""};
ans.push(make_pair(false, init));
for (int i = 0; i < s.size(); ++i) {
if (isalpha(s[i])) {
if (ans.top().first) {
vector<string> tmp{ string(1, s[i]) };
ans.push(make_pair(false, tmp));
} else {
for (auto &a : ans.top().second) a += s[i];
}
} else if (s[i] == '{') {
vector<string> tmp;
ans.push(make_pair(true, tmp));
} else { // , or }
auto top = ans.top();
ans.pop();
if (ans.top().first) {
for (auto &a : top.second) ans.top().second.push_back(a);
} else {
vector<string> tmp;
for (auto &a : ans.top().second)
for (auto &b : top.second)
tmp.push_back(a + b);
swap(tmp, ans.top().second);
}
if (s[i] == '}') {
auto top = ans.top();
ans.pop();
if (ans.top().first) {
for (auto &a : top.second) ans.top().second.push_back(a);
} else {
vector<string> tmp;
for (auto &a : ans.top().second)
for (auto &b : top.second)
tmp.push_back(a + b);
swap(tmp, ans.top().second);
}
}
}
cout << i << endl;
}
auto &v = ans.top().second;
sort(v.begin(), v.end());
v.erase(unique(v.begin(), v.end()), v.end());
return v;
}
};