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

Posted qldabiaoge

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Watto and Mechanism Codeforces Round #291 (Div. 2)相关的知识,希望对你有一定的参考价值。

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
");
45         else printf("NO
");
46     }
47     return 0;
48 }
View Code

 








以上是关于Watto and Mechanism Codeforces Round #291 (Div. 2)的主要内容,如果未能解决你的问题,请参考以下文章

CodeForces 518C - Watto and Mechanism(模拟)

Codeforces 514C. Watto and Mechanism解题报告(字典树)

文献总结RDAA:Role Discovery-Guided Network Embedding Based on Autoencoder and Attention Mechanism

文献总结RDAA:Role Discovery-Guided Network Embedding Based on Autoencoder and Attention Mechanism

论文阅读|深读RDAA:Role Discovery-Guided Network Embedding Based on Autoencoder and Attention Mechanism

RNN神经网络- 吴恩达Andrew Ng 循环神经网络 语音识别Sequence Models and the Attention Mechanism Week3 知识总结