Watto and Mechanism Codeforces Round #291 (Div. 2)
3 seconds
256 megabytes
standard input
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.
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'.
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).
2 3
aaaaa
acacaca
aabaa
ccacacc
caaac
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)的更多相关文章
- hash+set Codeforces Round #291 (Div. 2) C. Watto and Mechanism
题目传送门 /* hash+set:首先把各个字符串的哈希值保存在set容器里,然后对于查询的每一个字符串的每一位进行枚举 用set的find函数查找是否存在替换后的字符串,理解后并不难.另外,我想用 ...
- 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 ...
- 暴力/set Codeforces Round #291 (Div. 2) C. Watto and Mechanism
题目传送门 /* set的二分查找 如果数据规模小的话可以用O(n^2)的暴力想法 否则就只好一个一个的换(a, b, c),在set容器找相匹配的 */ #include <cstdio> ...
- Codeforces Round #291 (Div. 2) C - Watto and Mechanism 字符串
[题意]给n个字符串组成的集合,然后有m个询问(0 ≤ n ≤ 3·105, 0 ≤ m ≤ 3·105) ,每个询问都给出一个字符串s,问集合中是否存在一个字符串t,使得s和t长度相同,并且仅有一个 ...
- 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 ...
- 数学 Codeforces Round #291 (Div. 2) B. Han Solo and Lazer Gun
题目传送门 /* 水题,就是用三点共线的式子来判断射击次数 */ #include <cstdio> #include <cmath> #include <string& ...
- 贪心/字符串处理 Codeforces Round #291 (Div. 2) A. Chewbaсca and Number
题目传送门 /* WA了好几次,除了第一次不知道string不用'\0'外 都是欠考虑造成的 */ #include <cstdio> #include <cmath> #in ...
- Codeforces Round #291 (Div. 2)
A 题意:给出变换规则,单个数字t可以变成9-t,然后给出一个数,问最小能够变成多少. 自己做的时候理解成了不能输出前导0,但是题目的本意是不能有前导0(即最高位不能是0,其余位数按照规则就好) 55 ...
- Codeforces Round #291 (Div. 2) B. Han Solo and Lazer Gun
因为是x,y均为整数因此对于同一直线的点,其最简分数x/y是相同的(y可以为0,这里不做除法)于是将这些点不断求最简分数用pair在set中去重即可. #include <cmath> # ...
随机推荐
- 解决MySQL server has gone away问题的两种有效办法
最近做网站有一个站要用到WEB网页采集器功能,当一个PHP脚本在请求URL的时候,可能这个被请求的网页非常慢慢,超过了mysql的 wait-timeout时间,然后当网页内容被抓回来后,准备插入到M ...
- hdu6370 并查集+dfs
Werewolf Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 262144/262144 K (Java/Others)Total ...
- R语言绘图:ROC曲线图
使用pROC包绘制ROC曲线 #####***绘制ROC曲线***##### library("pROC") N <- dim(data2)[1] #数据长度 set.see ...
- 48-Identity MVC:Model前后端验证
1-创建RegisterViewModel类 namespace MvcCookieAuthSample.ViewModel { public class RegisterViewModel { [R ...
- SapScript
* [OPEN_FORM] SAPscript: フォーム印刷の開始 * [START_FORM] SAPscript: 書式を開始 * [WRITE_FORM] SAPscript: 書式ウィンドウ ...
- flask与javascript及ajax
flask与javascript及ajax 1. flask+js 1.1. 最简单的 最简单的元素信息改变. {% block content %} <h1>我的第一张网 ...
- ansible结合SHELL搭建自己的CD持续交付系统
一. 设计出发点 因公司业务面临频繁的迭代上线,一日数次.仅仅依靠手工效率过低且易出错. 考虑搭建一套可以满足现有场景的上线系统. 二 .为何采用ansible+shell方式 1.可控性(完全自主拥 ...
- 简历编写技巧-java开发工程师简历实战
看到一遍简历编写的文章 想到也快找工作了 早晚能够用上 现在摘录如下 640?wx_fmt=jpeg 工欲善其事,必先利其器,这是自古以来的道理.所以如果想找到一份好的工作,一定要先整理一份好的简历. ...
- JavaSE复习(六)函数式接口
函数式接口 有且仅有一个抽象方法的接口 @FunctionalInterface注解 一旦使用该注解来定义接口,编译器将会强制检查该接口是否确实有且仅有一个抽象方法,否则将会报错.需要注 意的是,即使 ...
- java多线程二之线程同步的三种方法
java多线程的难点是在:处理多个线程同步与并发运行时线程间的通信问题.java在处理线程同步时,常用方法有: 1.synchronized关键字. 2.Lock显示加锁. 3.信号量Se ...