sdut1500Message Flood(trie树)
http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1500
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 #include <iostream> 2 #include<cstdio> 3 #include<string.h> 4 #include<stdlib.h> 5 using namespace std; 6 struct node 7 { 8 int flag; 9 node *next[27]; 10 node() 11 { 12 flag = 0; 13 memset(next,NULL,sizeof(next)); 14 } 15 }; 16 int num; 17 void trie(node *head,char *c) 18 { 19 int i,k = strlen(c),d; 20 node *p = head; 21 for(i = 0 ; i < k ;i++) 22 { 23 if(c[i]>='A'&&c[i]<='Z') 24 c[i] = c[i]+32; 25 d = c[i]-'a'; 26 if(p->next[d]==NULL) 27 p->next[d] = new node; 28 p=p->next[d]; 29 } 30 p->flag = 1; 31 } 32 void search(node *head,char *c) 33 { 34 int i,j,k=strlen(c),d; 35 node *p = head; 36 for(i= 0 ; i <k ; i++) 37 { 38 if(c[i]>='A'&&c[i]<='Z') 39 c[i] = c[i]+32; 40 d = c[i]-'a'; 41 if(p->next[d]==NULL) 42 return ; 43 p = p->next[d]; 44 } 45 if(p->flag==1) 46 { 47 num++; 48 p->flag = -1; 49 } 50 } 51 void deal(node *head) 52 { 53 int i; 54 if(head) 55 { 56 for(i = 0 ;i < 26 ; i++) 57 if(head->next[i]) 58 deal(head->next[i]); 59 } 60 free(head); 61 head=NULL; 62 } 63 int main() 64 { 65 int i,j,k,n,m,x; 66 char c[30]; 67 while(scanf("%d",&n)&&n) 68 { 69 x = n; 70 scanf("%d%*c",&m); 71 node *head = new node; 72 num = 0; 73 while(n--) 74 { 75 gets(c); 76 trie(head,c); 77 } 78 while(m--) 79 { 80 gets(c); 81 search(head,c); 82 } 83 printf("%d\n",x-num); 84 deal(head); 85 } 86 return 0; 87 }