-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathPHONELST(spoj).cpp
68 lines (63 loc) · 946 Bytes
/
PHONELST(spoj).cpp
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
#include<bits/stdc++.h>
using namespace std;
struct Trie
{
struct Trie *child[10];
bool end;
};
struct Trie *CreateNode()
{
struct Trie *t=new Trie;
t->end=false;
for(int i=0;i<10;i++)
t->child[i]=NULL;
return t;
}
bool Insert(struct Trie *root,string s)
{
struct Trie *temp=root;
int i,flag=0;
for(i=0;i<s.size();i++)
{
if(temp->child[s[i]-'0']==NULL)
temp->child[s[i]-'0']=CreateNode();
temp=temp->child[s[i]-'0'];
if(temp->end)
{
flag=1;
break;
}
}
temp->end=true;
if(flag)
return false;
else
return true;
}
int main()
{
int t,n,i;
cin>>t;
while(t--)
{
struct Trie *root=CreateNode();
cin>>n;
string s[n+1];
bool res=true;
for(i=0;i<n;i++)
{
cin>>s[i];
}
sort(s,s+n);
for(i=0;i<n;i++)
{
res = res and Insert(root,s[i]);
if(!res)
break;
}
if(res)
cout<<"YES\n";
else
cout<<"NO\n";
}
}