USACO Name That Number

Code

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
/*
ID: wcr19961
PROG: namenum
LANG: C++11
*/
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
using namespace std;

typedef long long LL;
const int maxn = 30;
char s[maxn], tmp[maxn];
vector<string> S, ans;

void dfs(int d, int n) {
if (d == n) {
tmp[d] = 0;
if (binary_search(S.begin(), S.end(), string(tmp))) {
ans.push_back(tmp);
}
return;
}
for (int i = 0; i < 3; ++i) {
tmp[d] = 'A' + (s[d] - '2') * 3 + i;
if (tmp[d] >= 'Q') {
++tmp[d];
}
dfs(d + 1, n);
}
}

int main() {
freopen("namenum.in", "r", stdin);
freopen("namenum.out", "w", stdout);
FILE* Dict = fopen("dict.txt", "r");
while (~fscanf(Dict, "%s", s)) {
S.push_back(s);
}
sort(S.begin(), S.end());
scanf("%s", s);
int n = int(strlen(s));
bool ok = true;
for (int i = 0; i < n; ++i) {
if (s[i] == '0' || s[i] == '1') {
ok = false;
break;
}
}
if (ok) {
dfs(0, n);
}
if (ok && ans.size()) {
sort(ans.begin(), ans.end());
for (int i = 0; i < ans.size(); ++i) {
puts(ans[i].c_str());
}
} else {
puts("NONE");
}
return 0;
}