Codeforces517C——哈希字符串——Watto and Mechanism

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 froms 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).

Sample test(s)
input
2 3
aaaaa
acacaca
aabaa
ccacacc
caaac
output
YES
NO
NO
/*
显然要用nlogn的方法,用set的STL
对于每一个串都有一个特定的哈希值,改变某个值,就是看这个哈希值是否已经出现过。
设一个好的KEY防止冲突
*/
/************************************************
Author        :powatr
Created Time  :2015-8-5 18:28:51
File Name     :c.cpp
************************************************/

#include <cstdio>
#include <algorithm>
#include <iostream>
#include <sstream>
#include <cstring>
#include <cmath>
#include <string>
#include <vector>
#include <queue>
#include <deque>
#include <stack>
#include <list>
#include <map>
#include <set>
#include <bitset>
#include <cstdlib>
#include <ctime>
using namespace std;

#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
typedef long long ll;
const int MAX = 1e6 + 10;
const int INF = 0x3f3f3f3f;
const int MOD = 1e9 + 7;
const int KEY = 257;//ORZ
int n, m;
set <ll> s;
char s1[MAX];
char s2[MAX];
ll f[MAX];

void inti()
{
    f[0] = 1;
    for(int i = 1; i <= MAX; i++){
        f[i] = f[i-1] * KEY % MOD;
    }
}

ll Hash(char *s1)
{
    int len = strlen(s1);
    ll tmp = 0;
    for(int i = 0 ; i < len; i++){
        tmp = (tmp * KEY + s1[i]) % MOD;
    }
    return tmp;
}

bool check(char *s2)
{
    int len = strlen(s2);
    ll h = Hash(s2);
    for(int i = 0 ; i < len; i++){
        for(ll ch = 'a'; ch <= 'c'; ch++){
            if(ch == s2[i]) continue;
            if(s.find((((ch - s2[i]) * f[len - i - 1] + h)%MOD + MOD)%MOD) != s.end())
                return true;
        }
    }
    return false;
}
int main()
{
    inti();
    while(~scanf("%d%d", &n, &m)){
        s.clear();
        for(int i = 1; i <= n; i++){
            scanf("%s", s1);
            s.insert(Hash(s1));
        }
     
        for(int i = 1; i <= m; i++){
            scanf("%s", s2);
            puts(check(s2) ? "YES" : "NO");
        }
    }
    return 0;
}