forked from taraqr9/UVA-solutions
-
Notifications
You must be signed in to change notification settings - Fork 0
/
uva 11362.cpp
59 lines (58 loc) · 1.25 KB
/
uva 11362.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
#include<bits/stdc++.h>
using namespace std;
#define MAX_NODE 1000000
#define MAX_LEN 100
int node[MAX_NODE][100];
int root,nnode;
void initialization(){
root = 0;
nnode = 0;
for(int i=0;i<100;i++) node[root][i] = -1;
}
void insert(string S){
int len = S.size();
int now = root;
for(int i=0;i<len;i++){
if(node[now][S[i]]==-1){
node[now][S[i]] = ++nnode;
}
for(int j=0;j<100;j++) node[nnode][j] = -1;
now = node[now][S[i]];
}
}
bool search(string S){
int len = S.size();
int now = root; int chk=0;
for(int i=0;i<len;i++){
if(node[now][S[i]]==-1){
chk = 1;
}
now = node[now][S[i]];
}
if(chk==0) return 1;
return 0;
}
int main()
{
int t,n,i,j;
scanf("%d",&t);
while(t--){
string str;
cin>>n;
initialization();
vector<pair<int,string> >v;
while(n--){
cin>>str;
v.push_back(make_pair(str.size(),str));
}
int check=0;
sort(v.begin(),v.end());
for(i=v.size()-1;i>=0;i--){
if(search(v[i].second)==1) { check=1; break; }
insert(v[i].second);
}
if(check==1) printf("NO\n");
else printf("YES\n");
}
return 0;
}