poj 3630 Phone List

同poj 1059,判断是否为前缀。

 1 #include <stdio.h>
 2 #include <string.h>
 3 const int N = 100000;
 4 int next[N][10],cnt=0;
 5 bool f=1,end[N],vis[N];
 6 void inst(char *s)
 7 {
 8     int u=0,p;
 9     for(;*s;s++)
10     {
11         p = *s - '0';
12         if(end[u])
13         {f = 0; return ;}
14         if(!next[u][p])
15         {
16             cnt++;
17             memset(next[cnt],0,sizeof next[0]);
18             next[u][p] = cnt;
19             vis[u] = 1;
20         }
21         u = next[u][p];
22     }
23     end[u] = 1;
24     if(vis[u]) f = 0;
25 }
26 int main()
27 {
28     char t[12];
29     int n,m;
30     scanf("%d",&n);
31     while(n--)
32     {
33         scanf("%d",&m);
34         while(m--)
35         {
36             scanf("%s",t);
37             if(f) inst(t);
38         }
39         printf("%s\n",f ?"YES" :"NO");
40         f = 1; cnt = 0;
41         memset(next[0],0,sizeof next[0]);
42         memset(end,0,sizeof end);
43         memset(vis,0,sizeof end);
44     }
45     return 0;
46 }
原文地址:https://www.cnblogs.com/lzxskjo/p/2661102.html