0%

SDTBU2021暑假集训II G题(字典树)

题目链接

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
#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
using namespace std;

const int N = 10010;

int T, n;

char s[N][20];
int t[100010][26], cnt;
int pre[100010];

void insert(char s[]){
int x = 0;
for(int i=0; s[i]; i++){
if(!t[x][s[i]-'a']) t[x][s[i]-'a'] = ++cnt;
x = t[x][s[i]-'a'];
pre[x]++;
}
}

int Find(char s[]){
int x = 0;
for(int i=0; s[i]; i++){
if(t[x][s[i]-'a'] == 0) return 0;
x = t[x][s[i]-'a'];
}
return pre[x];
}

int main() {
cin >> T;
while(T--) {
memset(s, 0, sizeof(s));
memset(pre, 0, sizeof(pre));
memset(t, 0, sizeof(t));
cnt = 0;
scanf("%d", &n);
for(int i = 1; i <= n; i++) {
scanf("%s", s[i]);
insert(s[i]);
//cout << s[i] << "****" << Find(s[i]) << endl;
}
int flag = 0;
for(int i = 1; i <= n; i++) {
//cout << s[i] << "## "<< Find(s[i]) << endl;
if(Find(s[i]) > 1) {
flag = 1;
break;
}
}

if(flag) printf("NO\n");
else printf("YES\n");
}

return 0;
}
求大佬赏个饭