-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPHONELST.cpp
More file actions
64 lines (58 loc) · 979 Bytes
/
PHONELST.cpp
File metadata and controls
64 lines (58 loc) · 979 Bytes
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
#include <bits/stdc++.h>
using namespace std;
int flag = 1, flag2 = 1;
class node {
public:
int word;
node *edge[10];
};
node* initialize() {
node *temp = new node();
temp->word = 0;
for (int i = 0; i < 10; ++i)
{
temp->edge[i] = NULL;
}
return temp;
}
void add_word(node *vertex, string word) {
if(word.length() == 0) {
vertex->word = vertex->word + 1;
for (int i = 0; i < 10; ++i)
{
if(vertex->edge[i] != NULL)
flag = 0;
}
}
else {
char k = word[0];
if(vertex->word != 0) {
flag2 = 0;
}
if(vertex->edge[k-'0'] == NULL) {
vertex->edge[k-'0'] = initialize();
}
word.erase(word.begin());
add_word(vertex->edge[k-'0'], word);
}
}
int main() {
int t;
scanf("%d",&t);
while(t--) {
flag = 1, flag2 = 1;
int n;
node *root = initialize();
scanf("%d",&n);
for (int i = 0; i < n; ++i)
{
string s;
cin>>s;
add_word(root, s);
}
if(flag2 == 0 || flag == 0)
printf("NO\n");
else
printf("YES\n");
}
}