hdu1671 Phone List【字典树模板】

 1 #include<cstdio>
 2 #include<cstring>
 3 const int maxnode = 100005;
 4 
 5 char data[10005][15];
 6 struct Trie
 7 {
 8     int tree[maxnode][10],sz;
 9     int amount[maxnode];
10     void init(void)
11     {
12         sz=0;
13         memset(tree[0],0,sizeof(tree[0]));
14         memset(amount,0,sizeof(amount));
15     }
16     void insert(char *ch)
17     {
18         int p=0;
19         for(int i=0;ch[i];i++)
20         {
21             int x=ch[i]-'0';
22             if( !tree[p][x] )
23             {
24                 tree[p][x]=++sz;
25                 memset(tree[sz],0,sizeof(tree[sz]));
26             }
27             p=tree[p][x];
28             amount[p]++;
29         }
30     }
31     bool search(char *ch)
32     {
33         int p=0;
34         for(int i=0;ch[i];i++)
35         {
36             int x=ch[i]-'0';
37             if( !tree[p][x] )   return false;
38             p=tree[p][x];
39         }
40         return amount[p]-1;
41     }
42 }T;
43 
44 int main()
45 {
46     int t,n;
47     bool flag;
48 
49     scanf("%d",&t);
50     while( t-- )
51     {
52         flag=false;
53         T.init();
54         scanf("%d",&n);
55         for(int i=1;i<=n;i++)
56         {
57             scanf("%s",data[i]);
58             T.insert(data[i]);
59         }
60         for(int i=1;i<=n;i++)
61             if( T.search(data[i]) )
62             {
63                 flag=true;
64                 break;
65             }
66         puts(flag?"NO":"YES");
67     }
68     return 0;
69 }
70 /*
71     还可以对所有串排序,然后考察串i是否是串i+1的前缀
72 */

 

posted @ 2012-12-15 20:06  kiwi_bird  阅读(169)  评论(0编辑  收藏  举报