zoukankan      html  css  js  c++  java
  • CODE[VS]4189 字典 (字典树模板题)

    描述

    传送门:我是传送门

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

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

    如:skyzhong想查询a

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

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

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

    输入

    第一行一个数n

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

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

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

    输出

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

    样例

    输入

    3

    asd

    asfdghj

    asfd

    3

    asd

    asdghj

    asf

    输出

    YES

    NO

    YES

    Note

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

    思路

    字典树的模板题,恰好可以拿来练习字典树的使用

    代码

     1 /*
     2  * =========================================================================
     3  *
     4  *       Filename:  p4189.cpp
     5  *
     6  *           Link:  http://codevs.cn/problem/4189/
     7  *
     8  *        Version:  1.0
     9  *        Created:  2018/08/29 10时31分33秒
    10  *       Revision:  none
    11  *       Compiler:  g++
    12  *
    13  *         Author:  杜宁元 (https://duny31030.top/), duny31030@126.com
    14  *   Organization:  QLU_浪在ACM
    15  *
    16  * =========================================================================
    17  */
    18 #include <bits/stdc++.h>
    19 using namespace std;
    20 #define clr(a, x) memset(a, x, sizeof(a))
    21 #define rep(i,a,n) for(int i=a;i<=n;i++)
    22 #define pre(i,a,n) for(int i=a;i>=n;i--)
    23 #define ll long long
    24 #define max3(a,b,c) fmax(a,fmax(b,c))
    25 #define ios ios_base::sync_with_stdio(false);cin.tie(0);cout.tie(0);
    26 const double eps = 1e-6;
    27 const int INF = 0x3f3f3f3f;
    28 const int mod = 1e9 + 7;
    29 const int N = 1000100;
    30 
    31 struct node
    32 {
    33     int next[27];
    34 }trie[N];
    35 char a[10];
    36 int tot;
    37 
    38 void add()
    39 {
    40     int len = strlen(a);
    41     int now = 0;
    42     for(int i = 0;i < len;i++)
    43     {
    44         int tmp = a[i]-'a'+1;
    45         int next = trie[now].next[tmp];
    46         if(next)
    47         {
    48             now = next;
    49         }
    50         else 
    51         {
    52             trie[now].next[tmp] = ++tot;
    53             now = tot;
    54         }
    55     }
    56 }
    57 
    58 int query()
    59 {
    60     int len = strlen(a);
    61     int now = 0,p = 0;
    62     while(p < len)
    63     {
    64         int tmp = trie[now].next[a[p]-'a'+1];
    65         if(!tmp)
    66             return 0;
    67         else 
    68         {
    69             now = tmp;
    70             p++;
    71         }
    72     }
    73     return 1;
    74 }
    75 
    76 int main()
    77 {
    78     ios
    79     int n,m;
    80     cin >> n;
    81     rep(i,1,n)
    82     {
    83         cin >> a;
    84         add();
    85     }
    86     cin >> m;
    87     rep(i,1,m)
    88     {
    89         cin >> a;
    90         if(query())
    91             printf("YES
    ");
    92         else 
    93             printf("NO
    ");
    94 
    95     }
    96 
    97     return 0;
    98 }
  • 相关阅读:
    C++设计模式 ==> 装饰(者)模式
    基于路由器的VRRP技术--VRRP的应用
    基于路由器的VRRP技术--VRRP的应用
    C++设计模式 ==> 策略模式与简单工厂模式结合
    C++设计模式 ==> 简单工厂模式
    DVWA之Insecure Captcha
    DVWA之Insecure Captcha
    DVWA之File Inclusion
    DVWA之File Inclusion
    DVWA之XSS
  • 原文地址:https://www.cnblogs.com/duny31030/p/14304316.html
Copyright © 2011-2022 走看看