template
Trie dictionary tree can be roughly divided into two implementations, one is an array, the other is a pointer, then we have organized two templates:
Array implementation:
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <algorithm> #include <cmath> #include <map> using namespace std; const int N = 1000000; int ch [N] [26], val [N], tot; //If the array is implemented, the size of the ch array is always uncertain, and we are not sure how many nodes will be generated void Init() { memset(ch[0],0,sizeof(ch[0])); to = 1; memset(val,0,sizeof(val)); } void Insert(char *s,int x) { int u = 0,len = strlen(s); for(int i =0 ;i < len;i ++) { int v = s[i]-'a'; if(!ch[u][v]) { memset(ch[tot],0,sizeof(ch[tot])); val [tot] = 0; ch[u][v] = tot++; } u = ch[u][v]; } val[u] = x;//The end position is set to the weight, and the rest of the positions are set to 0 } int Find(char *s) { int u = 0,len = strlen(s); for(int i =0;i < len;i ++) { int v = s[i]-'a'; if(!ch[u][v]) return 0; u = ch[u][v]; } return val[u]; } void Del(char *s) { int u = 0,len = strlen(s); for(int i =0 ;i < len;i ++) { int v = s[i] - 'a'; if(!ch[u][v]) return ; u = ch[u][v]; } val[u] = 0;//When deleting, the weight of the last position is set to 0 }
pointer implementation
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <algorithm> #include <cmath> #include <map> using namespace std; //Pointer implementation is more convenient and easy to understand struct Trie { Trie *next[26]; int val; }; Trie *root; void Init() { root = (Trie *)malloc(sizeof(Trie)); for(int i = 0;i < 26;i ++) root->next[i] = NULL; root->val = 0; } void Insert(char *s) { Trie *p = root,*q; int len = strlen(s); for(int i = 0;i < len;i ++) { int v = s[i] - 'a'; if(p->next[v] == NULL) { q = (Trie *)malloc(sizeof(Trie)); q->val = 0; for(int j = 0;j < 26;j ++) q->next[j] = NULL; p->next[v] = q; } p = p->next[v]; } p->val = 1; } int Find(char *s) { int len = strlen(s); Trie *p = root; for(int i =0 ;i < len;i ++) { int v = s[i] - 'a'; p = p->next[v]; if(p == NULL) return -1; } return p->val; } void Del(Trie *s) { if(s == NULL) return ; for(int i =0;i < 26;i ++) if(s->next[i]) Del(s->next[i]); delete s; }
Chestnut
#include<iostream> #include<cstdio> #include<string> #include<cstring> #include<algorithm> #include<cmath> using namespace std; const int N = 1000000; int sz; int ch[N][26]; int val[N]; struct Trie { Trie(){ sz = 1;//The 0 node should be empty here as all the root nodes memset(ch[0],0,sizeof(ch[0])); memset(val,0,sizeof(val)); } int idx(char c) { return c-'a'; } void Insert(char *s,int v) { int u = 0,len = strlen(s); for(int i =0 ;i < len;i ++) { int c = idx(s[i]); if(!ch[u][c])//Indicates that this node is re-used as a new node, and there are still 26 child nodes below { memset(ch[sz],0,sizeof(ch[sz])); val [sz] = 0; ch[u][c] = sz++; } u = ch[u][c]; val[u] ++; } } int Read(char *s) { int u = 0,len = strlen(s); for(int i = 0;i < len;i ++) { int c = idx(s[i]); if(!ch[u][c]) return -1; else u = ch[u][c]; } if(val[u] == 0) return 1; else return 2; } int Get_num(char *s) { int u = 0,len = strlen(s); for(int i = 0;i < len;i ++) { int c = idx(s[i]); if(!ch[u][c]) return 0; else u = ch[u][c]; } return val[u]; } }; intmain() { Sort T; char s[15]; while(1) { gets(s); if(s[0] == '\0') break; T.Insert(s,1); } while(gets(s)) { printf("%d\n",T.Get_num(s)); } return 0; }
#include <iostream> #include <cstdio> #include <string> #include <cstring> #include <algorithm> #include <cmath> #include <map> using namespace std; struct Trie { Trie *next[26]; char * val; }; Trie *root; void Init() { root = (Trie *)malloc(sizeof(Trie)); for(int i = 0;i < 26;i ++) root->next[i] = NULL; root->val = NULL; } void Insert(char *s,char *str) { Trie *p = root,*q; int len = strlen(s); for(int i = 0;i < len;i ++) { int v = s[i] - 'a'; if(p->next[v] == NULL) { q = (Trie *)malloc(sizeof(Trie)); q->val = NULL; for(int j = 0;j < 26;j ++) q->next[j] = NULL; p->next[v] = q; } p = p->next[v]; } p->val = new char[11]; strcpy(p->val,str); } void Find(char *s) { int len = strlen(s); Trie *p = root; for(int i =0 ;i < len;i ++) { int v = s[i] - 'a'; p = p->next[v]; if(p == NULL) {printf("%s",s);return ;} } if(p->val) printf("%s",p->val); else printf("%s",s); } void Del(Trie *s) { if(s == NULL) return ; for(int i =0 ;i < 26;i ++) { if(s->next[i]) Del(s->next[i]); } delete s->val; delete s; } intmain() { char s[15],s1[15]; Init(); scanf("%s",s); while(scanf("%s %s\n",s,s1) && strcmp(s,"END")) { Insert(s1,s); } char str[3100],part[100]; while(gets(str) && strcmp(str,"END")) { int index = 0; int len = strlen (str); for(int i = 0;i < len;i ++) { if(islower(str[i])) part[index++] = str[i]; else { part[index] = '\0'; Find(part); index = 0; printf("%c",str[i]); } } puts(""); } }
Reference blog
https://blog.csdn.net/hcbbt/article/details/39499325