-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcode.cpp
More file actions
113 lines (85 loc) · 1.99 KB
/
Copy pathcode.cpp
File metadata and controls
113 lines (85 loc) · 1.99 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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
#include <bits/stdc++.h>
#define MAX 10010
#define PI acos(-1)
#define pb(x) push_back(x)
#define pii pair< int, int >
#define read(x) freopen("in.txt", "r", stdin)
#define write() freopen("out.txt", "w", stdout)
using namespace std;
typedef long long int lli;
struct node {
bool endmark;
node *next[26 + 1];
node() {
endmark = false;
for (int i = 0; i < 26; ++i) {
next[i] = NULL;
}
}
} * root;
vector< string > nums;
void insert(string str) {
node *curr = root;
for (int i = 0; i < str.size(); i++) {
int letter = str.at(i) - '0';
if (curr->next[letter] == NULL) {
curr->next[letter] = new node();
}
curr = curr->next[letter];
}
curr->endmark = true;
}
bool chceckConsistent(string str) {
node *curr = root;
for (int i = 0; i < str.size(); i++) {
int letter = str.at(i) - '0';
if (curr->endmark) {
// cout << "broke at (" << i << ") in " << str << endl;
return false;
}
curr = curr->next[letter];
}
return true;
}
void del(node *curr) {
for (int i = 0; i < 26; i++) {
if (curr->next[i]) {
del(curr->next[i]);
}
}
delete (curr);
}
int main() {
// read();
// write();
int tc;
cin >> tc;
while (tc--) {
root = new node();
int numWords = 0;
cin >> numWords;
bool consistent = true;
for (int i = 0; i < numWords; i++) {
string str;
cin >> str;
insert(str);
nums.pb(str);
}
for (int i = 0; i < numWords; i++) {
string x = nums.at(i);
if (!chceckConsistent(x)) {
consistent = false;
break;
}
}
if (consistent) {
cout << "YES" << endl;
}
else {
cout << "NO" << endl;
}
del(root);
nums.clear();
}
return 0;
}