码迷,mamicode.com
首页 > 其他好文 > 详细

4189 字典

时间:2016-06-13 08:49:24      阅读:136      评论:0      收藏:0      [点我收藏+]

标签:

4189 字典

 

 时间限制: 1 s
 空间限制: 256000 KB
 题目等级 : 大师 Master
 
 
题目描述 Description

最经,skyzhong得到了一本好厉害的字典,这个字典里整整有n个单词(1<=n<=200000)

现在skyzhong需要在字典里查询以某一段字母开头的单词

如:skyzhong想查询a

那么只要是a开头的单词就可以了

skyzhong只想知道里面有没有这一个单词(因为没有他就不查了)

若有,请输出YES。若没有,请输出NO

 

输入描述 Input Description

第一行一个数n

第二行到第n+1行,一行一个字符串

再下一行一个数m,表示skyzhong想要查询的次数

接着m行,一行一个字符串,表示skyzhong想要查的东西

输出描述 Output Description

共m行,若有这字串输出YES,否则输出NO

样例输入 Sample Input

3

asd

asfdghj

asfd

3

asd

asdghj

asf

样例输出 Sample Output

YES

NO

YES

数据范围及提示 Data Size & Hint

字符串只有小写字母,且长度≤8

分类标签 Tags 点此展开 

 
暂无标签
#include<cstdio>
#include<cstring>
#include<iostream>
using namespace std;
#define N 300010
struct node{
    int next[27],w;
}tire[N];
int n,m,cnt=0;
char s[10];
void build(){
    int now=0,len=strlen(s);
    for(int i=0;i<len;i++){
        int x=s[i]-a+1;
        if(tire[now].next[x])
            now=tire[now].next[x];
        else
            now=tire[now].next[x]=++cnt;
    }
    tire[now].w=1;    
}
int query(){
    int now=0,p=0,len=strlen(s);
    while(p<len){
        if(!tire[now].next[s[p]-a+1])
            return 0;
        now=tire[now].next[s[p++]-a+1];
    }
    return 1;
}
int main(){
    scanf("%d",&n);
    for(int i=1;i<=n;i++)
        scanf("%s",s),build();
    scanf("%d",&m);
    for(int i=1;i<=m;i++)
        scanf("%s",s),printf("%s\n",query()?"YES":"NO");
    return 0;
}

 

4189 字典

标签:

原文地址:http://www.cnblogs.com/shenben/p/5579397.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!