zoukankan      html  css  js  c++  java
  • 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
  • 相关阅读:
    PAT-乙级-1034. 有理数四则运算(20)
    PAT-乙级-1033. 旧键盘打字(20)
    PAT-乙级-1032. 挖掘机技术哪家强(20)
    PAT-乙级-1031. 查验身份证(15)
    PAT-乙级-1030. *完美数列(25)
    PAT-乙级-1029. 旧键盘(20)
    PAT-乙级-1028. 人口普查(20)
    PAT-乙级-1027. 打印沙漏(20)
    PAT-乙级-1026. 程序运行时间(15)
    PAT-乙级-1025. 反转链表 (25)
  • 原文地址:https://www.cnblogs.com/qldabiaoge/p/9174539.html
Copyright © 2011-2022 走看看