字典树
Time Limit: 1000MS
Memory Limit: 65536KB
Problem Description//静态字典树,模拟字典树的动态内存分配
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
struct node
{
int data;
int next[26];
}st[1000000];
int top;
int Creat()
{
memset(st[top].next,-1,sizeof(st[top].next));
st[top].data=0;
top++;
return top-1;
}
void Insert(int root,char *s)
{
int i;
int len=strlen(s);
for(i=0;i<=len-1;i++)
{
int t=s[i]-'a';
if(st[root].next[t]==-1)
{
st[root].next[t]=Creat();
}
root=st[root].next[t];
}
st[root].data++;//相同字母的数量
}
int Search(int root,char *s)
{
int i;
int len=strlen(s);
for(i=0;i<=len-1;i++)
{
int t=s[i]-'a';
if(st[root].next[t]==-1)
return 0;
root=st[root].next[t];
}
return st[root].data;
}
int main()
{
int n,m;
char s[12];
int root;
while(~scanf("%d%d",&n,&m))
{
if(m==0&&n==0) break;
top=0;
root=Creat();
while(n--)
{
scanf("%s",s);
Insert(root,s);
}
while(m--)
{
scanf("%s",s);
if(Search(root,s))
printf("Yes\n");
else printf("No\n");
}
}
return 0;
}
//静态字典树,模拟字典树的动态内存分配
#include<iostream>
#include<cstdio>
#include<cstring>
using namespace std;
struct node
{
int data;
int next[26];
}st[1000000];
int top;
int Creat()
{
memset(st[top].next,-1,sizeof(st[top].next));
st[top].data=0;
top++;
return top-1;
}
void Insert(int root,char *s)
{
int i;
int len=strlen(s);
for(i=0;i<=len-1;i++)
{
int t=s[i]-'a';
if(st[root].next[t]==-1)
{
st[root].next[t]=Creat();
}
root=st[root].next[t];
}
st[root].data++;//相同字母的数量
}
int Search(int root,char *s)
{
int i;
int len=strlen(s);
for(i=0;i<=len-1;i++)
{
int t=s[i]-'a';
if(st[root].next[t]==-1)
return 0;
root=st[root].next[t];
}
return st[root].data;
}
int main()
{
int n,m;
char s[12];
int root;
while(~scanf("%d%d",&n,&m))
{
if(m==0&&n==0) break;
top=0;
root=Creat();
while(n--)
{
scanf("%s",s);
Insert(root,s);
}
while(m--)
{
scanf("%s",s);
if(Search(root,s))
printf("Yes\n");
else printf("No\n");
}
}
return 0;
}
遇到单词不认识怎么办? 查字典啊,已知字典中有n个单词,假设单词都是由小写字母组成。现有m个不认识的单词,询问这m个单词是否出现在字典中。
Input
含有多组测试用例。
第一行输入n,m (n>=0&&n<=100000&&m>=0&&m<=100000)分别是字典中存在的n个单词和要查询的m个单词.
紧跟着n行,代表字典中存在的单词。
然后m行,要查询的m个单词
n=0&&m=0 程序结束
数据保证所有的单词都是有小写字母组成,并且长度不超过10
Output
若存在则输出Yes,不存在输出No .
Example Input
3 2 aab aa ad ac ad 0 0
Example Output
No Yes
Hint
Author
gyx
542

被折叠的 条评论
为什么被折叠?



