-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtrie_ref.cpp
More file actions
58 lines (58 loc) · 1.23 KB
/
trie_ref.cpp
File metadata and controls
58 lines (58 loc) · 1.23 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
#include <cstdio>
#include <algorithm>
#include <cstring>
#define MAX_N 10000
using namespace std;
struct Trie{
Trie* next[10]; //전화번호의 최대 길이
bool term;
Trie() : term(false){
memset(next,0,sizeof(next));
}
~Trie(){
for(int i=0;i<10;i++){
if(next[i])
delete next[i];
}
}
void insert(const char* key){
if(*key=='\0')
term=true;
else{
int curr = *key-'0';
if(next[curr]==NULL)
next[curr]=new Trie();
next[curr]->insert(key+1);
}
}
bool find(const char* key){
if(*key=='\0')
return 0;
if(term)
return 1;
int curr = *key-'0';
return next[curr]->find(key+1);
}
};
int t,n,r;
char a[MAX_N][11];
int main(){
scanf("%d",&t);
while(t--){
scanf("%d",&n);
getchar();
for(int i=0;i<n;i++)
scanf("%s",&a[i]);
Trie *root=new Trie;
r=0;
for(int i=0;i<n;i++)
root->insert(a[i]);
for(int i=0;i<n;i++){
if(root->find(a[i])){
r=1;
}
}
printf("%s\n",r?"NO":"YES");
}
return 0;
}