-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsol.cpp
More file actions
executable file
·72 lines (56 loc) · 1.12 KB
/
Copy pathsol.cpp
File metadata and controls
executable file
·72 lines (56 loc) · 1.12 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
#include <bits/stdc++.h>
using namespace std;
struct trie
{
unordered_map<char, trie *> children = {};
bool is_end_of_word = false;
};
void insert(trie *root, string key)
{
for (char c : key)
{
if (root->children.find(c) == root->children.end())
root->children[c] = new trie;
root = root->children[c];
}
root->is_end_of_word = true;
}
bool isPrefixFree(trie *root)
{
queue<trie *> Q;
Q.push(root);
while (!Q.empty())
{
trie *current = Q.front();
Q.pop();
if (current->is_end_of_word && current->children.size() > 0)
return false;
for (auto each : current->children)
{
Q.push(each.second);
}
}
return true;
}
int main()
{
int t;
cin >> t;
while (t--)
{
int n;
cin >> n;
trie *root = new trie;
while (n--)
{
string s;
cin >> s;
insert(root, s);
}
if (isPrefixFree(root))
cout << "YES" << endl;
else
cout << "NO" << endl;
}
return 0;
}