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各种卡种子

  

 #include <bits/stdc++.h>
using namespace std;
typedef long long LL; const LL seed = ;
const LL mod = 1e9 + ;
const int maxn = 6e5 + ;
int n,m;
LL p[maxn];
char str[maxn];
set<LL>st;
void init(){
p[]=;
for (int i= ;i<maxn ;i++)
p[i]=p[i-]*seed%mod;
}
LL Hash(char s[]){
LL ret=;
for (int i= ; s[i] ;i++)
ret=(ret*seed+s[i])%mod;
return ret;
}
int check(char s[]){
LL h=Hash(s);
int len=strlen(s);
for (int i= ;i<len ;i++){
for (int j='a' ; j<='c' ;j++) {
if (j==s[i]) continue;
LL now=((((j-s[i])*p[len--i]%mod)+mod)+h)%mod;
if (st.find(now)!=st.end()) return ;
}
}
return ;
}
int main() {
scanf("%d%d", &n, &m);
init();
for (int i = ; i < n ; i++) {
scanf("%s", str);
st.insert(Hash(str));
}
for (int i = ; i < m ; i++) {
scanf("%s", str);
if (check(str)) printf("YES\n");
else printf("NO\n");
}
return ;
}

Watto and Mechanism Codeforces Round #291 (Div. 2)的更多相关文章

  1. hash+set Codeforces Round #291 (Div. 2) C. Watto and Mechanism

    题目传送门 /* hash+set:首先把各个字符串的哈希值保存在set容器里,然后对于查询的每一个字符串的每一位进行枚举 用set的find函数查找是否存在替换后的字符串,理解后并不难.另外,我想用 ...

  2. Codeforces Round #291 (Div. 2) C. Watto and Mechanism [字典树]

    传送门 C. Watto and Mechanism time limit per test 3 seconds memory limit per test 256 megabytes input s ...

  3. 暴力/set Codeforces Round #291 (Div. 2) C. Watto and Mechanism

    题目传送门 /* set的二分查找 如果数据规模小的话可以用O(n^2)的暴力想法 否则就只好一个一个的换(a, b, c),在set容器找相匹配的 */ #include <cstdio> ...

  4. Codeforces Round #291 (Div. 2) C - Watto and Mechanism 字符串

    [题意]给n个字符串组成的集合,然后有m个询问(0 ≤ n ≤ 3·105, 0 ≤ m ≤ 3·105) ,每个询问都给出一个字符串s,问集合中是否存在一个字符串t,使得s和t长度相同,并且仅有一个 ...

  5. Codeforces Round #291 (Div. 2) D. R2D2 and Droid Army [线段树+线性扫一遍]

    传送门 D. R2D2 and Droid Army time limit per test 2 seconds memory limit per test 256 megabytes input s ...

  6. 数学 Codeforces Round #291 (Div. 2) B. Han Solo and Lazer Gun

    题目传送门 /* 水题,就是用三点共线的式子来判断射击次数 */ #include <cstdio> #include <cmath> #include <string& ...

  7. 贪心/字符串处理 Codeforces Round #291 (Div. 2) A. Chewbaсca and Number

    题目传送门 /* WA了好几次,除了第一次不知道string不用'\0'外 都是欠考虑造成的 */ #include <cstdio> #include <cmath> #in ...

  8. Codeforces Round #291 (Div. 2)

    A 题意:给出变换规则,单个数字t可以变成9-t,然后给出一个数,问最小能够变成多少. 自己做的时候理解成了不能输出前导0,但是题目的本意是不能有前导0(即最高位不能是0,其余位数按照规则就好) 55 ...

  9. Codeforces Round #291 (Div. 2) B. Han Solo and Lazer Gun

    因为是x,y均为整数因此对于同一直线的点,其最简分数x/y是相同的(y可以为0,这里不做除法)于是将这些点不断求最简分数用pair在set中去重即可. #include <cmath> # ...

随机推荐

  1. 人人都会设计模式:观察者模式--Observer

    https://segmentfault.com/a/1190000012295887 观察者模式是抽像通知者和观察者,达到具体通知者跟具体观察者没有偶合.能达到不管是切换通知者,或者是切换观察者,都 ...

  2. Eclipse_安装SAP_HANA数据库插件

    1.对于Eclipse Oxygen,请添加URL https://tools.hana.ondemand.com/oxygen 2.对于Eclipse luna,请添加URL     https:/ ...

  3. js数组长度

    js数组长度,一般使用length 属性即可获取,但这个数组是个对象则只能使用以下方式 var t=typeof o; var length=0; if(t=='string'){ length=o. ...

  4. python2.7练习小例子(二十八)

    28):题目:请输入星期几的第一个字母来判断一下是星期几,如果第一个字母一样,则继续判断第二个字母.     程序分析:用情况语句比较好,如果第一个字母一样,则判断用情况语句或if语句判断第二个字母. ...

  5. python2.7入门---面向对象

        Python从设计之初就已经是一门面向对象的语言,正因为如此,在Python中创建一个类和对象是很容易的.所以,这篇文章我们来记录下Python的面向对象编程.如果你以前没有接触过面向对象的编 ...

  6. HBase 是什么

    Apache HBase™ is the Hadoop database, a distributed, scalable, big data store. HBase 是 Hadoop databa ...

  7. 洛谷P1364 医院设置

    LITTLESUN的第一道图论,撒花~~ 题目链接 这道题是Floyd的板子题 注意对于矩阵图的初始值赋值要全部赋值成最大值 十六进制的最大值表示方式是0x3f3f3f3f memset(G,0x3f ...

  8. springmvc基础篇—通过注解的方式去配置项目

    学习了通过xml方式去配置项目后,当然要掌握更简单更灵活的注解方式哟,这是官方推荐使用的方式. 一.修改配置文件,建议大家直接使用我的配置文件 <?xml version="1.0&q ...

  9. Prolog奇怪奇妙的思考方式

    今天在<七周七语言>中接触到了prolog,发现它的编程模式和思考方式的确比较奇怪,但同时也非常奇妙,值得学习一下. 1. prolog语言介绍     和SQL一样,Prolog基于数据 ...

  10. python学习总结------邮件与短信

    邮件发送 - 简介: - 邮件服务器.用户名.密码 - 相关协议: - SMTP:简单邮件传输协议 - POP3:邮局通讯协议 - IMAP:交互式邮件存取协议 - SMTP协议默认端口是25 - 用 ...