-
Notifications
You must be signed in to change notification settings - Fork 288
/
Copy pathAC_simulation_n.cpp
55 lines (50 loc) · 1.17 KB
/
AC_simulation_n.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
/*
* Author: illuz <iilluzen[at]gmail.com>
* File: AC_simulation_n.cpp
* Create Date: 2015-01-03 11:18:53
* Descripton: recursive, simulation
*/
#include <bits/stdc++.h>
using namespace std;
const int N = 0;
class Solution {
private:
const string alpha[10] = {
" ",
"1", "abc", "def",
"ghi", "jkl", "mno",
"pqrs", "tuv", "wxyz"
};
void dfs(vector<string> &res, string &ab, string &digits, int cur) {
if (cur >= digits.length()) {
res.push_back(ab);
return;
}
for (auto &a : alpha[digits[cur] - '0']) {
ab.push_back(a);
dfs(res, ab, digits, cur + 1);
ab.pop_back();
}
}
public:
vector<string> letterCombinations(string digits) {
vector<string> res;
string alphas;
dfs(res, alphas, digits, 0);
return res;
}
};
int main() {
string d;
Solution s;
while (cin >> d) {
vector<string> res = s.letterCombinations(d);
for (auto &i : res) {
for (auto &j : i)
cout << j;
cout << ' ';
}
cout << endl;
}
return 0;
}