您的位置:首页 > 其它

SDUT 1500 Message Flood trie树

2012-08-13 20:44 316 查看
题目连接:http://acm.sdut.edu.cn/sdutoj/problem.php?action=showproblem&problemid=1500

大意:输入几个字符串,然后再输入几个字符串,看第一次输入的字符串有多少没有在后面的字符串中出现(后输入的字符串不一定出现在之前的字符串中)

代码:

View Code

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
struct node
{
int flag;
struct node *next[27];
};
struct node *new_node()
{
struct node *p;
p = (struct node *)malloc(sizeof(struct node));
int i;
for(i = 0;i < 27;i++)
{
p->next[i] = NULL;
}
p->flag = 0;
return p;
}
void insert(struct node *rt,char *s)
{
struct node *p;
p = rt;
int t,i;
for(i = 0;s[i] != '\0';i++)
{
if(s[i] >= 'A' && s[i] <= 'Z')
s[i] =s[i]-'A'+'a';
t = s[i]-'a';
if(p->next[t] == NULL)
p->next[t] = new_node();
p = p->next[t];
}
p->flag = 1;

}

int search(struct node *rt,char s[])
{
int i;
struct node *p;
p = rt;
for(i = 0; s[i] != 0;i++)
{
if(s[i] >= 'A' && s[i] <= 'Z')
s[i]+=32;
int t;
t = s[i]- 'a';
if(p->next[t] == NULL)
return 0;
p = p->next[t];
}
if(p->flag)
{
p->flag = 0;
return 1;
}
return 0;
}
void del(struct node * p)
{
int i;
if(p)
{
for(i=0;i<26;i++)
if(p->next[i])
del(p->next[i]);
}
free(p);
p=NULL;
}
int main()
{
char s[105];
int n,m,i,j,leap;
while(~scanf("%d",&n)&&n)
{
scanf("%d",&m);
struct node *rt;
rt = new_node();
leap = 0;
int temp =  n;
while(n--)
{
scanf("%s",s);
insert(rt,s);
}
leap = 0;
while(m--)
{
scanf("%s",s);
if(search(rt,s))
leap++;

}
del(rt);
printf("%d\n",temp-leap);
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: