-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathphonelist.cc
64 lines (47 loc) · 1.37 KB
/
phonelist.cc
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
#include <map>
#include <string>
#include <iostream>
struct Node {
std::map<char, Node*> trans;
bool end;
};
bool add_string(Node* node, std::string s, int i, bool followed=false) {
if (node->end || (followed && i == s.size())) {
//std::cout << "Found end" << std::endl;
return false;
}
if (i == s.size()) {
//std::cout << "set end" << std::endl;
node->end = true;
return true;
}
if (node->trans.count(s[i])) {
//std::cout << "following " << s[i] << std::endl;
return add_string(node->trans[s[i]], s, i+1, true);
} else {
//std::cout << "new " << s[i] << std::endl;
Node* n = new Node();
node->trans[s[i]] = n;
return add_string(n, s, i+1);
}
return true;
}
int main() {
int test_count; std::cin >> test_count;
std::string dummy;
while (test_count--) {
Node* root = new Node {{}, false};
int n; std::cin >> n;
bool result = true;
while (n--) {
std::string nr;
std::cin >> nr;
if (!add_string(root, nr, 0)) {
//std::cout << "set result to false" << std::endl;
result = false;
}
}
if (result) std::cout << "YES" << std::endl;
else std::cout << "NO" << std::endl;
}
}