POJ 3630 Phone List

题目大意:给n个字符串,问是否有一个是另一个的前缀
思路:把n个字符串插到trie里,然后判断就好,注意一个长字符串覆盖另一个短字符串和短字符串匹配长字符串的区别

 1 #include<iostream>
 2 #include<cstring>
 3 #include<cstdio>
 4 #define maxn 100009
 5 using namespace std;
 6 int siz=0,trie[maxn][20],n,end[maxn][20];
 7 char ch[maxn];
 8 bool insert(const char * str)
 9 {
10     int len = strlen(str+1), u = 0,flag=1,en=0;
11     for(int i = 1; i <= len; u = trie[u][str[i++]-'0'])
12     {
13         if(!trie[u][str[i]-'0'])
14         {
15             trie[u][str[i]-'0']=++siz;
16             flag = 0;
17         }
18         else
19         {
20             if(end[u][str[i]-'0']==1)
21             {
22                 en=1;
23             }
24         }
25         if(i==len)
26         end[u][str[i]-'0'] = 1;
27     }
28     if(en)return en;
29     return flag;
30 }
31 int main()
32 {
33     int t;
34     scanf("%d",&t);
35     while(t--)
36     {
37         scanf("%d",&n);
38         memset(trie,0,sizeof(trie));
39         memset(end,0,sizeof(end));
40         siz=0;
41         int flag = 0;
42         while(n--)
43         {
44             scanf("%s",ch+1);
45             if(insert(ch))
46             {
47                 flag = 1;
48             }
49         }
50         if(flag)puts("NO");else puts("YES");
51     }
52     return 0;
53 }
原文地址:https://www.cnblogs.com/philippica/p/4638503.html