• codeVS 4189 字典


    题目描述 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

    字典树查询是否存在的问题。

    代码:

    #include <iostream>
    #include <cstdio>
    #include <map>
    #include <algorithm>
    using namespace std;
    int trie[400001][26],pos;
    inline void Insert(char *s) {
        int i = 0,c = 0;
        while(s[i]) {
            int d = s[i] - 'a';
            if(!trie[c][d]) {
                trie[c][d] = ++ pos;
            }
            c = trie[c][d];
            i ++;
        }
    }
    inline bool Query(char *s) {
        int i = 0,c = 0;
        while(s[i]) {
            int d = s[i] - 'a';
            if(!trie[c][d]) {
                return false;
            }
            c = trie[c][d];
            i ++;
        }
        return true;
    }
    int main() {
        char s[12];
        int n,m;
        scanf("%d",&n);
        for(int i = 0;i < n;i ++) {
            scanf("%s",s);
            Insert(s);
        }
        scanf("%d",&m);
        for(int i = 0;i < m;i ++) {
            scanf("%s",s);
            puts(Query(s) ? "YES" : "NO");
        }
    }
  • 相关阅读:
    uGUI知识点剖析之RectTransform
    C#中的结构体要使用new来实例化吗?
    LOL数值分析
    【《Effective C#》提炼总结】提高Unity中C#代码质量的21条准则
    AnimationCurve
    Unity3D 自动添加Fbx Animation Event
    unity 代码添加AnimationEvent
    untiy AnimationEvent添加返回参数
    Windows CreateEvent,SetEvent,WaitForSingleObject的用法
    C++ Socket编程步骤
  • 原文地址:https://www.cnblogs.com/8023spz/p/9590392.html
Copyright © 2020-2023  润新知