1 typedef struct Node 2 { 3 Node *next[26]; 4 int cut; 5 }Node; 6 Node *root; 7 void inser(char *s) 8 { 9 Node *p=root; 10 for (int i=0;s[i];i++) 11 { 12 int x=s[i]-'a'; 13 if (p->next[x]==NULL) 14 { 15 p->next[x]=(Node *)malloc(sizeof(Node)); 16 p->next[x]->cut=0; 17 for (int i=0;i<26;i++) p->next[x]->next[i]=NULL; 18 } 19 p=p->next[x]; 20 p->cut++; 21 } 22 } 23 int Find(char *s) 24 { 25 Node *p=root; 26 for (int i=0;s[i];i++) 27 { 28 int x=s[i]-'a'; 29 if (p->next[x]==NULL) return 0; 30 p=p->next[x]; 31 } 32 return p->cut; 33 }
1 ///字典树模板链表版 2 struct Node 3 { 4 int next[26]; 5 int cut; 6 void Init() 7 { 8 for (int i=0;i<26;i++) next[i]=-1; 9 cut=0; 10 } 11 }; 12 Node tree[1000005]; 13 int cut=0; 14 void inser(char *s) 15 { 16 int p=0; 17 for (int i=0;s[i];i++) 18 { 19 int x=s[i]-'a'; 20 if (tree[p].next[x]==-1) 21 { 22 tree[p].next[x]=++cut; 23 tree[cut].Init(); 24 } 25 p=tree[p].next[x]; 26 tree[p].cut++; 27 } 28 } 29 int Find(char *s) 30 { 31 int p=0; 32 for (int i=0;s[i];i++) 33 { 34 int x=s[i]-'a'; 35 if (tree[p].next[x]==-1) return 0; 36 p=tree[p].next[x]; 37 } 38 return tree[p].cut; 39 }