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. JAVA 泛型方法<T>

    public static void main(String[] args) throws Exception { String[] arr = new String[]{"1", ...

  2. spark优化系列一:参数介绍

    1 spark on yarn常用属性介绍 属性名 默认值 属性说明 spark.yarn.am.memory 512m 在客户端模式(client mode)下,yarn应用master使用的内存数 ...

  3. Error loading MySQLdb module: libmysqlclient.so.18: cannot open shared object file: No such file or directory

    在hue上配置Mysql的时候,出现的错误:  发生服务器错误: Error loading MySQLdb module: libmysqlclient.so.18: cannot open sha ...

  4. Sql Server 2008 R2数据库中插入中文变成了问号

            通过Insert语句插入数据库中,结果中文都变成了乱码.原因是在数据库中有一个属性需要设置,可以通过Sql server manager studio来进行设置,也要可以通过代码来设置 ...

  5. JavaScript序列化对象成URL格式

    http://access911.net/fixhtm/72FABF1E15DCEAF3.htm?tt=

  6. python基础篇 05字典

    本节主要内容:1. 字典的简单介绍2. 字典增删改查和其他操作3. 字典的嵌套 一. 字典的简单介绍:字典(dict)是python中唯一的一个映射类型.他是以{ }括起来的键值对组成. 在dict中 ...

  7. 《python机器学习—预测分析核心算法》:构建预测模型的一般流程

    参见原书1.5节 构建预测模型的一般流程 问题的日常语言表述->问题的数学语言重述重述问题.提取特征.训练算法.评估算法 熟悉不同算法的输入数据结构:1.提取或组合预测所需的特征2.设定训练目标 ...

  8. 计算机概念总结5-阿里云的了解-ecs

    1.ecs 1.1ecs 云服务器Elastic Compute Service(ECS)是阿里云提供的一种基础云计算服务.使用云服务器ECS就像使用水.电.煤气等资源一样便捷.高效.您无需提前采购硬 ...

  9. Python不同进制之间的转换

    不同的进制 二进制    0b101 以数字0和字母b打头的表示二进制数 如果出现大于等于2的数 会抛出SyntaxError异常 八进制    0711 以数字0打头的数字表示八进制数 如果出现大于 ...

  10. 修改CodeSmith中的SchemaExplorer.MySQLSchemaProvider

    修改C:\Program Files (x86)\CodeSmith\v6.5\Samples\Projects\CSharp\MySQLSchemaProvider\MySQLSchemaProvi ...