-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTrie - Implemented with Pointer.cpp
More file actions
96 lines (86 loc) · 1.89 KB
/
Trie - Implemented with Pointer.cpp
File metadata and controls
96 lines (86 loc) · 1.89 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
#include <bits/stdc++.h>
using namespace std;
class Trie
{
public:
struct node
{
bool endmark;
node* next[26];
node()
{
endmark = false;
for (int i = 0; i < 26; i++)
next[i] = NULL;
}
};
node* root = new node();
void insert(string str, int sz)
{
node* curr = root;
for (int i = 0; i < sz; i++)
{
int id = str[i] - 'a';
if (curr->next[id] == NULL)
curr->next[id] = new node();
curr = curr->next[id];
}
curr->endmark = true;
}
bool search(string str, int sz)
{
node* curr = root;
for (int i = 0; i < sz; i++)
{
int id = str[i] - 'a';
if (curr->next[id] == NULL)
return false;
curr = curr->next[id];
}
return curr->endmark;
}
void del(node* cur)
{
for(int i = 0; i < 26; i++)
if(cur->next[i] != NULL)
del(cur->next[i]);
delete cur;
}
};
int main()
{
int t, n;
cin >> t;
for(int tc = 0; tc < t; ++tc)
{
Trie boss;
string str[10005];
bool f = 0;
cin >> n;
for(int i = 0; i < n; ++i)
{
cin >> str[i];
boss.insert(str[i], str[i].size());
}
for(int i = 0; i < n; ++i)
{
for(int j = 0; j < str[i].size()-1; ++j)
{
string s = str[i].substr(0, j+1);
if(boss.search(s, j+1))
{
f = 1;
break;
}
}
if(f)
break;
}
if(f)
cout << "NO" << endl;
else
cout << "YES" << endl;
boss.del(boss.root);
}
return 0;
}