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.
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'.
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).
2 3
aaaaa
acacaca
aabaa
ccacacc
caaac
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 }