3 2 aab aa ad ac ad 0 0
No Yes
///字典树是用来查找单词的,是一种哈兮树的变种
#include<stdio.h>
#include<string.h>
struct node
{
int next[26];
int mark;
}st[500000];
int top,n,m;
char a[20],b[20];
int creat()
{
memset(st[top].next,-1,sizeof(st[top].next));
st[top].mark=0;
return top++;
}
int xiab(char c)
{
return c-'a';
}
void insert(int root,char *s)
{
int i;
int len=strlen(s);
for(i=0;i<=len-1;i++)
{
if(st[root].next[xiab(s[i])]==-1)
st[root].next[xiab(s[i])]=creat();
root=st[root].next[xiab(s[i])];
}
st[root].mark=1;
}
int search(int root,char *s)
{
int i;
for(i=0;s[i]!='\0';i++)
{
if(st[root].next[xiab(s[i])]==-1)
return 0;
root=st[root].next[xiab(s[i])];
}
return st[root].mark;
}
int main()
{
int i,root;
while(~scanf("%d%d",&n,&m))
{
if(n==0 && m==0)
{
break;
}
else
{
top=0;
root=creat();
for(i=0;i<=n-1;i++)
{
scanf("%s",a);
insert(root,a);
}
for(i=0;i<=m-1;i++)
{
scanf("%s",b);
if(search(root,b))
printf("Yes\n");
else
printf("No\n");
}
}
}
return 0;
}版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/sh_tomorrow/article/details/47725591