Message Flood

http://acm.sdut.edu.cn/sdutoj/showproblem.php?pid=1500&cid=1147

View Code
 1 #include<stdio.h>
 2 #include<string.h>
 3 #include<malloc.h>
 4 int num ;
 5 struct node
 6 {
 7     int flag;
 8     struct node *next[26];
 9 };
10 struct node *creat()
11 {
12      int i;
13      struct node *p;
14      p=(struct node*)malloc(sizeof(struct node));
15      p->flag=0;
16      for(i=0;i<26;i++)
17          p->next[i]=NULL;
18      return p;
19 }
20 void insert(struct node *root, char *s)
21 {
22      int i;
23      struct node *p = root ;
24      int len=strlen(s);
25      for(i=0;i<len;i++)
26      {
27          if(s[i]>='A'&&s[i]<='Z')
28             s[i]+=32;
29          int t = s[i] - 'a' ;
30          if(p->next[t]==NULL)
31          {
32              p->next[t]= creat();
33          }
34          p=p->next[t];
35      }
36      p->flag=1;
37 }
38 void search(struct node *root, char *s)
39 {
40      int i,len;
41      len=strlen(s);
42      struct node *p = root ;
43      for(i=0;i<len;i++)
44      {
45          if(s[i]>='A'&&s[i]<='Z')
46             s[i]+=32;
47          int t = s[i] - 'a' ;
48          if(p->next[t]!=NULL)
49           p=p->next[t];
50      }
51     if(p->flag)
52     {
53         p->flag=0;
54         num++ ;
55     }
56 
57 }
58 void Delete(struct node *root)
59 {
60     int i ;
61     struct node *p = root ;
62     if(p)
63     {
64         for(i=0; i<26; i++)
65         if(p->next[i]!=NULL)
66         Delete(p->next[i]) ;
67     }
68     free(p) ;
69     p = NULL ;
70 }
71 int main()
72 {
73      int n, m, tt;
74      char str[26];
75      while( scanf("%d",&n),n!=0)
76      {
77         struct node *root = creat() ;
78         tt=n;
79         num = 0 ;
80         scanf("%d",&m);
81         while(n--)
82         {
83            scanf("%s",str);
84            insert(root, str);
85         }
86         while(m--)
87         {
88            scanf("%s",str);
89            search(root, str);
90         }
91           printf("%d\n",tt-num);
92           Delete(root) ;
93      }
94      return 0;
95 }
原文地址:https://www.cnblogs.com/yelan/p/2918490.html