-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathcode2.cpp
More file actions
83 lines (60 loc) · 1.31 KB
/
Copy pathcode2.cpp
File metadata and controls
83 lines (60 loc) · 1.31 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
#include <bits/stdc++.h>
#define MAX 15
#define ALPHABET_SIZE 30
using namespace std;
struct node {
bool is_leaf;
int size;
char value;
node * children[ALPHABET_SIZE];
};
node * getNode() {
node * result = (node*)malloc(sizeof(node));
result->size = 0;
result->is_leaf = false;
result->value = '*';
for(int i = 0; i < ALPHABET_SIZE; i++) result->children[i] = NULL;
return result;
}
node * trie;
void ins(char *key) {
int length = strlen(key);
int index;
node * crawl = trie;
for (int level = 0; level < length; level++) {
crawl->size = crawl->size + 1;
index = key[level] - '0';
if (!crawl->children[index]) crawl->children[index] = getNode();
crawl->children[index]->value = key[level];
crawl = crawl->children[index];
}
crawl->is_leaf = true;
}
char aux[MAX];
int n;
bool ans;
//DFS
void dfs(node *root) {
if(root->is_leaf && root->size > 0) ans = false;
else {
for(int i = 0; i < ALPHABET_SIZE; i++)
if(root->children[i])
dfs(root->children[i]);
}
}
int main(){
int t;
scanf("%d", &t);
while(t--) {
ans = true;
trie = getNode();
scanf("%d", &n);
for(int i = 0; i < n; i++) {
scanf("\n%s", aux);
ins(aux);
}
dfs(trie);
printf("%s\n", ans ? "YES" : "NO");
}
return 0;
}