您的位置:首页 > 其它

SDUT 1500 Message Flood

2012-08-15 21:08 274 查看
字典树模板题。

题目链接http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1500

View Code

#include<stdio.h>
#include<string.h>
#include<stdlib.h>
#define N 26
struct node
{
int num;
struct node *next
;
}tree[21000];
int t=0;
struct node *creat()
{
int i;
struct node *p;
//p=&tree[t++];     为什么这里不能用静态分配内存。用的话会RE。 改成动态就可以了?
p=(struct node*)malloc(sizeof(struct node));
for(i=0;i<N;i++)
p->next[i]=NULL;
p->num=0;
return p;
}
void Insert(struct node *root,char *s)
{
int i,len,ans;
len=strlen(s);
struct node *p=root;
for(i=0;i<len;i++)
{
if(s[i]>='A' && s[i]<='Z') ans=s[i]-'A';
else ans=s[i]-'a';
if(p->next[ans]==NULL) p->next[ans]=creat();
p=p->next[ans];
}
p->num=1;
}
int search(struct node *root,char *s)
{
struct node *p=root;
int i,len,ans;
len=strlen(s);
for(i=0;i<len;i++)
{
if(s[i]>='A' && s[i]<='Z') ans=s[i]-'A';
else ans=s[i]-'a';
if(p->next[ans]==NULL) return 0;
p=p->next[ans];
}
if(p->num==1) {p->num=0;return 1;}
return 0;
}
void Delete(struct node *p)
{
int i;
for(i=0;i<N;i++)
if(p->next[i]!=NULL) Delete(p->next[i]);
delete p;
p=NULL;
}
int main()
{
int i,n,m,count;
char str[12];
struct node *p;
while(scanf("%d",&n)!=EOF && n)
{
scanf("%d",&m);
p=creat();
for(i=0;i<n;i++)
{
scanf("%s",str);
Insert(p,str);
}
count=0;
while(m--)
{
scanf("%s",str);
count+=search(p,str);
}
printf("%d\n",n-count);
Delete(p);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: