• Watto and Mechanism Codeforces Round #291 (Div. 2)


    C. Watto and Mechanism
    time limit per test
    3 seconds
    memory limit per test
    256 megabytes
    input
    standard input
    output
    standard output

    Watto, the owner of a spare parts store, has recently got an order for the mechanism that can process strings in a certain way. Initially the memory of the mechanism is filled with n strings. Then the mechanism should be able to process queries of the following type: "Given string s, determine if the memory of the mechanism contains string t that consists of the same number of characters as s and differs from s in exactly one position".

    Watto has already compiled the mechanism, all that's left is to write a program for it and check it on the data consisting of n initial lines and m queries. He decided to entrust this job to you.

    Input

    The first line contains two non-negative numbers n and m (0 ≤ n ≤ 3·105, 0 ≤ m ≤ 3·105) — the number of the initial strings and the number of queries, respectively.

    Next follow n non-empty strings that are uploaded to the memory of the mechanism.

    Next follow m non-empty strings that are the queries to the mechanism.

    The total length of lines in the input doesn't exceed 6·105. Each line consists only of letters 'a', 'b', 'c'.

    Output

    For each query print on a single line "YES" (without the quotes), if the memory of the mechanism contains the required string, otherwise print "NO" (without the quotes).

    Examples
    input
    Copy
    2 3
    aaaaa
    acacaca
    aabaa
    ccacacc
    caaac
    output
    Copy
    YES
    NO
    NO

     

    暴力哈希  卡种子  CF出题人  就是强  HASH各种卡种子

     

      

     1 #include <bits/stdc++.h>
     2 using namespace std;
     3 typedef long long LL;
     4 
     5 const LL seed = 257;
     6 const LL mod = 1e9 + 7;
     7 const int maxn = 6e5 + 10;
     8 int n,m;
     9 LL p[maxn];
    10 char str[maxn];
    11 set<LL>st;
    12 void init(){
    13     p[0]=1;
    14     for (int i=1 ;i<maxn ;i++)
    15         p[i]=p[i-1]*seed%mod;
    16 }
    17 LL Hash(char s[]){
    18     LL ret=0;
    19     for (int i=0 ; s[i] ;i++)
    20         ret=(ret*seed+s[i])%mod;
    21     return ret;
    22 }
    23 int check(char s[]){
    24     LL h=Hash(s);
    25     int len=strlen(s);
    26     for (int i=0 ;i<len ;i++){
    27         for (int j='a' ; j<='c' ;j++) {
    28             if (j==s[i]) continue;
    29             LL now=((((j-s[i])*p[len-1-i]%mod)+mod)+h)%mod;
    30             if (st.find(now)!=st.end()) return 1;
    31         }
    32     }
    33     return 0;
    34 }
    35 int main() {
    36     scanf("%d%d", &n, &m);
    37     init();
    38     for (int i = 0 ; i < n ; i++) {
    39         scanf("%s", str);
    40         st.insert(Hash(str));
    41     }
    42     for (int i = 0 ; i < m ; i++) {
    43         scanf("%s", str);
    44         if (check(str)) printf("YES
    ");
    45         else printf("NO
    ");
    46     }
    47     return 0;
    48 }
    View Code
  • 相关阅读:
    编码转换,基础补充,深浅拷贝,id is == ,代码块(了解),小数据池(了解)
    字典(dict),字典的嵌套,集合(set)
    列表,列表的增删改查,列表的嵌套,range
    整数,布尔值,字符串,字符串详解.
    [小明学Shader]4.自定义光照----RampTexture
    [小明学Shader]3.自定义光照,半拉姆伯特
    [小明学Shader]2.理解Shader和Material的关系
    [小明学Shader]1.Diffuse
    [UGUI]你说UnityEngine.UI.Button是怎么通过拖动来增加OnClick的监听器的呢?
    [小明也得懂架构]1.架构初探
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/9174539.html
Copyright © 2020-2023  润新知