字典树 sdut acm 1500 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 <stdlib.h>
 4 
 5 struct node
 6 {
 7     int flag;
 8     struct node * next[26];
 9 };
10 
11 struct node * newnode()
12 {
13     int i;
14     struct node * 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 
21 void insert(struct node * root, char *s)
22 {
23     struct node * p = root;
24     int i, len = strlen(s), t;
25     for(i = 0; i < len; i++)
26     {
27         if(s[i]>='A' && s[i] <= 'Z') t = s[i] -'A';
28         else t = s[i] - 'a';
29         if(p->next[t] == NULL)
30             p->next[t] = newnode();
31         p = p->next[t];
32     }
33     p->flag = 1;
34 }
35 
36 int search(struct node * root, char *s)
37 {
38     struct node * p = root;
39     int i, t, len = strlen(s);
40     for(i = 0; i < len; i++)
41     {
42         if(s[i]>='A' && s[i] <= 'Z') t = s[i] -'A';
43         else t = s[i] - 'a';
44         if(p->next[t] == NULL)
45             return 0;
46         p = p->next[t];
47     }
48     if(p->flag)
49         {p->flag = 0; return 1;}
50     return 0;
51 }
52 
53 void ReleaseTrie(struct node *T){
54     int i;
55     for(i=0; i<26; i++){
56         if(T->next[i]) ReleaseTrie(T->next[i]);
57         T->next[i] = NULL;
58     }
59     free(T);
60 }
61 
62 int main()
63 {
64     int n,m,t,i;
65     char s[20];
66     while(scanf("%d",&n)!=EOF&&n!=0)
67     {
68         struct node *root=NULL;
69         root=newnode();
70         scanf("%d",&m);
71         for(i=1;i<=n;i++)
72         {
73             scanf("%s",s);
74             insert(root,s);
75         }
76         while(m--)
77         {
78             scanf("%s",s);
79             t=search(root,s);
80             if(t==1)
81             n--;
82         }
83         printf("%d\n",n);
84         ReleaseTrie(root);
85     }
86     return 0;
87 }

 

 

 

 

转载于:https://www.cnblogs.com/bfshm/archive/2013/04/07/3006154.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值