-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path5052.cpp
53 lines (46 loc) · 987 Bytes
/
5052.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
#include <iostream>
#include <string>
#include <memory.h>
using namespace std;
struct Node {
Node *nodes[10];
bool is_end;
Node() : is_end(false) {
memset(nodes, 0, sizeof(nodes));
}
~Node(){
for (int i = 0; i < 10; i++) if (nodes[i]) delete nodes[i];
}
Node* insert(int n) {
if (!nodes[n]) nodes[n] = new Node;
return nodes[n];
}
};
void solve(){
int N; cin >> N;
string arr[N]; for (int i = 0; i < N; i++) cin >> arr[i];
Node *head = new Node;
string result = "YES";
for (int i = 0; i < N; i++) {
string str = arr[i];
Node *temp = head;
for (int j = 0; j < str.size(); j++) {
temp = temp->insert(str[j] - '0');
if (temp->is_end) result = "NO";
}
for (int j = 0; j < 10; j++) {
if (temp->nodes[j]) result = "NO";
}
temp->is_end = true;
}
cout << result;
}
int main() {
cin.tie(0) -> sync_with_stdio(0);
int T; cin >> T;
while (T--){
solve();
cout <<'\n';
}
return 0;
}