-
Notifications
You must be signed in to change notification settings - Fork 73
/
main.cpp
62 lines (54 loc) · 1.08 KB
/
main.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
#include <algorithm>
#include <cstdlib>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;
class Solution {
private:
vector<string> ans;
vector<char> stack;
int n;
string word;
void _generate(int p) {
if (p == n) {
ans.push_back(string(stack.begin(), stack.end()));
return;
}
stack.push_back(word[p]);
_generate(p + 1);
stack.pop_back();
if (stack.empty() ||
(stack.back() >= 'a' && stack.back() <= 'z')) {
for (int len = 1; len <= n - p; len++) {
stack.push_back((char)(len + '0'));
_generate(p + len);
stack.pop_back();
}
}
}
public:
vector<string> generalizedAbbr(string word) {
n = word.size();
this->word = word;
ans.clear();
stack.clear();
_generate(0);
return ans;
}
};
int main() {
Solution sol;
for (string s: sol.generalizedAbbr("word")) {
cout << s << endl;
}
return 0;
}