题目链接:http://acm.hdu.edu.cn/status.php?user=NYNU_WMH&pid=1251&status=5
Trie树的基本实现
字母树的插入(Insert)、删除( Delete)和查找(Find)都非常简单,用一个一重循环即可,即第i 次循环找到前i 个字母所对应的子树,然后进行相应的操作。实现这棵字母树,我们用最常见的数组保存(静态开辟内存)即可,当然也可以开动态的指针类型(动态开辟内存)。至于结点对儿子的指向,一般有三种方法:
1、对每个结点开一个字母集大小的数组,对应的下标是儿子所表示的字母,内容则是这个儿子对应在大数组上的位置,即标号;
2、对每个结点挂一个链表,按一定顺序记录每个儿子是谁;
3、使用左儿子右兄弟表示法记录这棵树。
三种方法,各有特点。第一种易实现,但实际的空间要求较大;第二种,较易实现,空间要求相对较小,但比较费时;第三种,空间要求最小,但相对费时且不易写。
表示用G++提交一直内存超限.....Orz 而C++直接秒过.......srO
malloc 较为费时 124MS
1 #include <stdio.h> 2 #include <string.h> 3 #include <math.h> 4 #include <algorithm> 5 #include <iostream> 6 #include <ctype.h> 7 #include <iomanip> 8 #include <queue> 9 #include <stdlib.h> 10 using namespace std; 11 12 struct node 13 { 14 int count; 15 struct node *next[26]; 16 }; 17 18 struct node *root; 19 struct node *newset() // 20 { 21 struct node *current; 22 current=(struct node *)malloc(sizeof(struct node)); 23 for(int i=0 ;i < 26; i++){ 24 current->next[i]=NULL; 25 } 26 current->count=1; 27 return current; 28 } 29 30 void insert(char *s) // 31 { 32 struct node *current; 33 int len=strlen(s); 34 if(len==0) 35 return ; 36 current= root; 37 for(int i=0 ;i < len; i++){ 38 if(current->next[s[i]-‘a‘]!=NULL){ 39 current = current->next[s[i]-‘a‘]; 40 current->count = current->count+1; 41 } 42 else{ 43 current->next[s[i]-‘a‘] = newset(); 44 current = current->next[s[i]-‘a‘]; 45 } 46 } 47 } 48 49 int find(char *s) 50 { 51 struct node *current; 52 int len=strlen(s); 53 if(len==0) 54 return 0; 55 current=root; 56 for(int i=0 ;i < len; i++){ 57 if(current->next[s[i]-‘a‘]!=NULL){ 58 current = current->next[s[i]-‘a‘]; 59 } 60 else{ 61 return 0; 62 } 63 } 64 return current->count; 65 } 66 67 int main() 68 { 69 char str[101]; 70 int i,ans; 71 root=newset(); 72 while(gets(str)&&str[0]!=‘\0‘){ 73 insert(str); 74 } 75 while(~scanf("%s",str)){ 76 ans=find(str); 77 printf("%d\n",ans); 78 } 79 return 0; 80 }
先分配好内存 109MS
1 #include <stdio.h> 2 #include <string.h> 3 #include <math.h> 4 #include <algorithm> 5 #include <iostream> 6 #include <ctype.h> 7 #include <iomanip> 8 #include <queue> 9 #include <stdlib.h> 10 using namespace std; 11 12 typedef struct node 13 { 14 int count; 15 struct node *next[26]; 16 }Trienode; 17 18 Trienode *root; 19 Trienode memory[1000000]; 20 int p=0; 21 22 struct node *newset() // 23 { 24 Trienode *current = &memory[p++]; 25 for(int i=0 ;i < 26; i++){ 26 current->next[i]=NULL; 27 } 28 current->count=1; 29 return current; 30 } 31 32 void insert(char *s) // 33 { 34 struct node *current; 35 int len=strlen(s); 36 if(len==0) 37 return ; 38 current= root; 39 for(int i=0 ;i < len; i++){ 40 if(current->next[s[i]-‘a‘]!=NULL){ 41 current = current->next[s[i]-‘a‘]; 42 current->count = current->count+1; 43 } 44 else{ 45 current->next[s[i]-‘a‘] = newset(); 46 current = current->next[s[i]-‘a‘]; 47 } 48 } 49 } 50 51 int find(char *s) 52 { 53 struct node *current; 54 int len=strlen(s); 55 if(len==0) 56 return 0; 57 current=root; 58 for(int i=0 ;i < len; i++){ 59 if(current->next[s[i]-‘a‘]!=NULL){ 60 current = current->next[s[i]-‘a‘]; 61 } 62 else{ 63 return 0; 64 } 65 } 66 return current->count; 67 } 68 69 int main() 70 { 71 char str[101]; 72 int i,ans; 73 root=newset(); 74 while(gets(str)&&str[0]!=‘\0‘){ 75 insert(str); 76 } 77 while(~scanf("%s",str)){ 78 ans=find(str); 79 printf("%d\n",ans); 80 } 81 return 0; 82 }
原文:http://www.cnblogs.com/wangmengmeng/p/5008394.html