RoNgDaZhOnG

  博客园  :: 首页  :: 新随笔  :: 联系 :: 订阅 订阅  :: 管理

逼格最高的程序。。但是由于是动态开辟,POJ上超时了,HDU过了,不知道为什么。
trie树很简单,没什么说的。
附标程:

 1 #include<cstdio>
 2 #include<cstdlib>
 3 #include<cstring>
 4 #include<algorithm>
 5 #include<vector>
 6 using namespace std;
 7 char num[100];
 8 struct sontype{int sonnum,sonplace;};
 9 struct node{
10        vector <sontype> son;
11        int freq;
12        bool endflag;
13 };
14 vector <sontype> mynull;
15 vector <node> E;
16 int root=0;
17 bool f;
18 
19 bool trie_insert(char *a){
20                  int now=root,l=0;
21                  while(l<strlen(a)){
22                                     int tmp=(a[l]-'0');
23                                     bool flag=false;
24                                     for(int i=0;i<E[now].son.size();i++){
25                                             sontype t1=E[now].son[i];
26                                             if(t1.sonnum==tmp){
27                                                                E[t1.sonplace].freq++;
28                                                                now=t1.sonplace;
29                                                                flag=true;
30                                                                break;
31                                             }
32                                     }
33                                     if(!flag){
34                                               E[now].son.push_back((sontype){tmp,E.size()});
35                                               now=E.size();
36                                               E.push_back((node){mynull,1,0});
37                                     }
38                                     l++;
39                                     if(l==strlen(a)){
40                                                      E[now].endflag=1;
41                                                      if(E[now].freq>1) return false;
42                                     }
43                                     else{if(E[now].endflag) return false;}
44                  }
45                  return true;
46 }
47 void trie_clear(){
48      f=false;root=0;
49      E.clear();
50      E.push_back((node){mynull,0,0});      
51 }                                       
52                                               
53                                                                     
54                  
55 int main(){
56     int t,n;
57     scanf("%d",&t);
58     while(t--){
59                trie_clear();
60                scanf("%d",&n);
61                for(int i=0;i<n;i++){
62                        scanf("%s",num);
63                        if(!trie_insert(num)) f=true;
64                }
65                printf(f?"NO\n":"YES\n");
66     }
67     
68     return 0;
69 }

 

posted on 2017-03-06 19:31  学无止境-1980  阅读(107)  评论(0编辑  收藏  举报