[trie]字典树模板

 

 

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 typedef long long ll;
 4 struct trie{
 5     int count;
 6     trie *next[26];
 7 }*root;
 8 trie *newtrie(){
 9     trie *t=(trie *)malloc(sizeof(struct trie));
10     memset(t,0,sizeof(struct trie));
11     return t;
12 }
13 void insert1(char *ch){
14     trie *s=root;
15     for(int i=0;ch[i];i++){
16         if(s->next[ch[i]-'a']){
17             s=s->next[ch[i]-'a'];
18             s->count++;
19         }else{
20             trie *t=newtrie();
21             s->next[ch[i]-'a']=t;
22             s=t;
23             s->count=1; 
24         }
25     }
26 }
27 int search1(char *ch){
28     trie *s=root;
29     int i;
30     for(i=0;ch[i];i++){
31         if(s->next[ch[i]-'a']&&s->next[ch[i]-'a']->count) s=s->next[ch[i]-'a'];
32         else break;
33     }
34     if(ch[i]) return 0;
35     else return s->count;//以此字符串为前缀的字符串个数 
36 }
37 void delete1(char *ch){
38     trie *s=root;
39     for(int i=0;ch[i];i++){
40         s=s->next[ch[i]-'a'];
41         s->count--;
42     }
43 }
44 int main(){
45     return 0;
46 }

 

posted @ 2017-05-16 01:21  Elpsywk  阅读(356)  评论(0编辑  收藏  举报