cf_514C(字符串哈希)

时间:2022-12-30 13:21:53
Watto and Mechanism
Time Limit:3000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u
Submit  Status

Description

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·1050 ≤ 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 Input

Input
2 3
aaaaa
acacaca
aabaa
ccacacc
caaac
Output
YES
NO
NO

题意:

给出n个字符串,再做m次询问,每次询问给你一条字符串,问你之前的n个字符串里是否存在和待查询串,有差别且仅差一个字符的字符串。有输出YES,没有NO

思路:

先算出N个字符串的哈希值,存进set。

每次查询时,算出待查询串的哈希值,遍历串上每一个位置,改变位置上字母,以产生差且仅差一个字符的新字符串,直接生成新字符串的哈希值。

注意生成新哈希值是可以根据原哈希值用O(1)方法算出,对于一个HASH值来说,你可以对它做除法以外的任何运算,不会影响HASH值的正确性,我之前对字符串哈希的理解是有偏颇的。HASH值被取模之后虽然失去了数学意义,但仍保持数学正确性,它的每一位并不再有意义,但是整体来讲,对其运算仍然是正确的。这题就很好的说明了这一点。

然后*说,MOD值要取得很大,WA了就去把MOD值改大!!注意改大的话用long long

 

 

字符串的hash主要是通过模拟字符串每一位对应到相应的数然后用相应进制去模拟加法即可

下面是代码:

 1 #include <iostream>  
 2 #include <cstdio>  
 3 #include <cmath>  
 4 #include <cstring>  
 5 #include <set>  
 6 using namespace std;  
 7 typedef long long ll;  
 8 const int maxn = 6*1e5 + 50;  
 9 const ll MOD = 1e11 + 7;  
10 const int base = 3;  
11   
12 char str[maxn];  
13 set<ll> mySet;  
14 ll key;  
15 ll p[maxn];  
16   
17 ll getHash()  
18 {  
19     ll num=0;  
20     int len = strlen(str);  
21     for(int i=len-1;i>=0;i--){  
22         num = (num*base + str[i]-'a'+1) % MOD;  
23     }  
24     return num;  
25 }  
26   
27 ll changeKey(ll key,int pos,int type)  
28 {  
29     ll tkey = ((key - (str[pos]-'a'+1)*p[pos] ) % MOD + MOD + (type+1)*p[pos] ) % MOD;  
30     return tkey;  
31 }  
32   
33 int main()  
34 {  
35     int i,j;  
36     p[0] = 1;  
37     for(i=1;i<maxn;i++){  
38         p[i] = p[i-1]*base % MOD;  
39     }  
40     int n,m;  
41     cin>>n>>m;  
42     while(n--){  
43         scanf("%s",str);  
44         key = getHash();  
45         mySet.insert(key);  
46         //cout<<key<<endl;  
47     }  
48     while(m--){  
49         scanf("%s",str);  
50         key = getHash();  
51         int len = strlen(str);  
52         int found = 0;  
53         for(i=0;i<len;i++){  
54             if(found) break;  
55             for(j=0;j<3;j++){  
56                 if(str[i]-'a' == j) continue;  
57                 ll tkey = changeKey(key,i,j);  
58                 //cout<<tkey<<endl;  
59                 if(mySet.find(tkey) != mySet.end()){  
60                     cout<<"YES"<<endl;  
61                     found = 1; break;  
62   
63                 }  
64             }  
65         }  
66         if(!found) cout<<"NO"<<endl;  
67     }  
68     return 0;  
69 }