Watto and Mechanism Codeforces Round #291 (Div. 2)

本文介绍了一种基于暴力哈希的字符串匹配算法,该算法通过预先计算哈希值并使用特定的种子来快速查找是否在给定的字符串集合中存在与目标字符串仅有一个字符不同的匹配项。文章提供了完整的C++代码实现,并解释了如何处理大量字符串查询。
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\n");
45         else printf("NO\n");
46     }
47     return 0;
48 }
View Code

 

转载于:https://www.cnblogs.com/qldabiaoge/p/9174539.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值