码迷,mamicode.com
首页 > 其他好文 > 详细

字典树模板+HDU 1671 ( Phone List )(字典树)

时间:2018-03-17 12:17:33      阅读:171      评论:0      收藏:0      [点我收藏+]

标签:使用   hid   open   sed   log   space   arc   main   style   

字典树指针模板(数组模板暂时还没写):

技术分享图片
 1 #include<cstdio>
 2 #include<string.h>
 3 #include<algorithm>
 4 using namespace std;
 5 const int MAX=26;
 6 const int maxn=1e4+100;
 7 int N;
 8 
 9 struct Trie
10 {
11     Trie *next[MAX];
12     int v;///v要灵活使用,具体情况具体分析
13 };
14 Trie *root;
15 int Get_ID(char c){
16     return c-a;
17 }///得到字符的id
18 
19 void init()///初始化根节点
20 {
21     root=(Trie *)malloc(sizeof(Trie));
22     for(int i=0;i<MAX;i++)
23         root->next[i]=NULL;
24     root->v=0;
25 }
26 int deal(Trie *p)///删除指针,释放空间,有些题要释放空间不让会MLE
27 {
28     if(p==NULL)
29         return 0;
30     for(int i=0;i<MAX;i++)
31     {
32         if(p->next[i]!=NULL)
33             deal(p->next[i])
34     }
35     free(p);
36     return 0;
37 }
38 void Insert(char s[])///字典树的建立
39 {
40     Trie *p=root;
41     int len_s=strlen(s);
42     for(Int i=0;i<len_s;i++)
43     {
44         int id-Get_ID(s[i]);
45         if(p->next[id]==NULL)
46         {
47             Trie *q=(Trie *)malloc(sizeof(Trie));
48             for(int j=0;j<MAX;j++)
49                 q->next[j]=NULL;
50             q->v=0;
51             p->next[id]=q;
52         }
53         p=p->next[id];
54         
55     }
56     p->v++;
57 }
58 int Search(char s[])///查找,看是否有条件满足
59 {
60     Trie *p=root;
61     int len_s=strlen(s);
62     for(int i=0;i<len_s;i++)
63     {
64         int id=Get_ID(s[i]);
65         if(p->next[id]==NULL)
66             return 0;///没有找到以s为前缀的字符串
67         p=p->next[id];
68     }
69     if(p->v)
70         return 1;///找到了以s串味前缀的字符串
71     return 0;
72 }
73 int main()
74 {
75     deal(root);
76     init();
77     
78     return 0;
79 }
字典树指针模板

http://acm.hdu.edu.cn/showproblem.php?pid=1671

题意:就是给你一些列的字符串,问你这些字符串中是否存在某一个字符串是其他字符串(可以是一个也可以是多个)的前缀,如果是输出NO,否则输出YES。

字典树的例题,不过要注意的是必须要释放内存,不然会MLE;同时这道题也是对v值得灵活运用。

 1 #include<cstdio>
 2 #include<string.h>
 3 #include<algorithm>
 4 using namespace std;
 5 const int MAX=10;
 6 const int maxn=1e4+100;
 7 int N;
 8 char s[maxn][30];
 9 struct Trie
10 {
11     Trie *next[MAX];
12     int v;
13 };
14 Trie *root;
15 int Get_ID(char c){
16     return c-0;
17 }
18 void init()
19 {
20     root=(Trie *)malloc(sizeof(Trie));
21     for(int i=0;i<MAX;i++)
22         root->next[i]=NULL;
23     root->v=0;
24 }
25 void Insert(char s[])
26 {
27     int len_s=strlen(s);
28     Trie *p=root;
29     for(int i=0;i<len_s;i++)
30     {
31         int id=Get_ID(s[i]);
32         if(p->next[id]==NULL)
33         {
34             Trie *q=(Trie *)malloc(sizeof(Trie));
35             for(int j=0;j<MAX;j++)
36                 q->next[j]=NULL;
37             q->v=0;
38             p->next[id]=q;
39         }
40         p=p->next[id];
41         p->v++;
42     }
43 }
44 
45 int Search(char s[])
46 {
47     Trie *p=root;
48     int len_s=strlen(s);
49     for(int i=0;i<len_s;i++)
50     {
51         int id=Get_ID(s[i]);
52         if(p->next[id]==NULL)
53             return 0;
54         p=p->next[id];
55     }
56         return p->v;
57 }
58 int deal(Trie *p)
59 {
60     if(p==NULL)
61         return 0;
62     for(int i=0;i<MAX;i++)
63     {
64         if(p->next[i]!=NULL)
65             deal(p->next[i]);
66     }
67     free(p);
68     return 0;
69 }
70 int main()
71 {
72     int t;
73     scanf("%d",&t);
74     while(t--)
75     {
76         deal(root);
77         init();
78         scanf("%d",&N);
79         for(int i=0;i<N;i++)
80         {
81             scanf("%s",s[i]);
82             Insert(s[i]);
83         }
84         int flag=0;
85         for(int i=0;i<N;i++)
86         {
87             if(Search(s[i])>1)
88             {
89                 flag=1;
90                 break;
91             }
92         }
93         if(flag)
94             printf("NO\n");
95         else
96             printf("YES\n");
97     }
98     return 0;
99 }

在做这道题的时候刚开始没有释放内存MLE了,之后MAX又取成了9,取小了点,在测试9这个字符的时候又出错了几次。

<.....>

记得要小心啊!~!~!~!~

字典树模板+HDU 1671 ( Phone List )(字典树)

标签:使用   hid   open   sed   log   space   arc   main   style   

原文地址:https://www.cnblogs.com/Y-Meng/p/8587898.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!