Watto, the owner of a spare parts store, have recently got an order for the mechanism so can process strings in a certain The. Initially the memory of the mechanism is filled with n strings. Then the mechanism should is 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 a nd differs froms in exactly one position ".
Watto have already compiled the mechanism, all that's left was 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 the 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 is uploaded to the memory of the mechanism.
Next follow m non-empty strings that is 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 to 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
/* Obviously to use the Nlogn method, with the set of STL for each string has a specific hash value, change a value, is to see whether the hash has occurred. Set a good key to prevent conflicts *//************************************************author:p owatrcreated time:2015-8-5 18:28:51File N ame: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 | 1typedef long ll;const int MAX = 1e6 + 10;const int INF = 0x3f3f3f3f;const int MOD = 1e9 + 7;const int KEY = 257;//or Zint 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;}
codeforces517c--Hash string--watto and mechanism