Phone List(电话表) POJ - 3630

题目链接:https://vjudge.net/problem/POJ-3630#author=Xiaomostream

 1 #include<cstdio>
 2 #include<cstring>
 3 #include<cmath>
 4 #include<algorithm>
 5 using namespace std;
 6 const int maxn=1e5+10;//数据规模开足
 7 int t, n;
 8 
 9 int trie[maxn][10], ed[maxn], tot;
10 void inif(){
11     memset(trie, 0, sizeof(trie));
12     memset(ed, 0, sizeof(ed));
13     tot=1;
14 }
15 bool trie_build(char s[]){//一边插入一边判断前缀问题 
16     int p=1;
17     int len=strlen(s);
18     for(int i=0; i<len; i++){
19         int ch=s[i]-'0';
20         if(!trie[p][ch])
21             trie[p][ch]=++tot;
22         else if(i==len-1)//说明当前插入串是其他串的子串,当前串已经处理完了 
23             return true;
24         p=trie[p][ch];
25         if(ed[p])//说明已插入串是要插入串的子串,因为要插入串还没有插入 
26             return true;
27     }
28     ed[p]=1;
29     return false;//新插入且没有匹配串 
30 }
31 int main()
32 {
33     scanf("%d", &t);
34     while(t--){
35         scanf("%d", &n);
36         char s[25];
37         bool f=0;//假定没有包含前缀
38         inif();
39         for(int i=1; i<=n; i++){
40             char s[15];
41             scanf("%s", s);
42             if(f)//有包含子串后面的没必要插入到字典树 
43                 continue;
44             if(trie_build(s))//有包含前缀 
45                 f=1;
46         }
47         if(!f)
48             printf("YES\n");
49         else
50             printf("NO\n");    
51     }
52     return 0;
53 }

 

posted @ 2022-05-01 18:02  TFLSNOI  阅读(47)  评论(0编辑  收藏  举报