cf_514C(字符串哈希)
Time Limit:3000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u
Description
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 froms 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).
Sample Input
2 3
aaaaa
acacaca
aabaa
ccacacc
caaac
YES
NO
NO
题意:
给出n个字符串,再做m次询问,每次询问给你一条字符串,问你之前的n个字符串里是否存在和待查询串,有差别且仅差一个字符的字符串。有输出YES,没有NO
思路:
先算出N个字符串的哈希值,存进set。
每次查询时,算出待查询串的哈希值,遍历串上每一个位置,改变位置上字母,以产生差且仅差一个字符的新字符串,直接生成新字符串的哈希值。
注意生成新哈希值是可以根据原哈希值用O(1)方法算出,对于一个HASH值来说,你可以对它做除法以外的任何运算,不会影响HASH值的正确性,我之前对字符串哈希的理解是有偏颇的。HASH值被取模之后虽然失去了数学意义,但仍保持数学正确性,它的每一位并不再有意义,但是整体来讲,对其运算仍然是正确的。这题就很好的说明了这一点。
然后毛主席说,MOD值要取得很大,WA了就去把MOD值改大!!注意改大的话用long long
字符串的hash主要是通过模拟字符串每一位对应到相应的数然后用相应进制去模拟加法即可
下面是代码:
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <set>
using namespace std;
typedef long long ll;
const int maxn = *1e5 + ;
const ll MOD = 1e11 + ;
const int base = ; char str[maxn];
set<ll> mySet;
ll key;
ll p[maxn]; ll getHash()
{
ll num=;
int len = strlen(str);
for(int i=len-;i>=;i--){
num = (num*base + str[i]-'a'+) % MOD;
}
return num;
} ll changeKey(ll key,int pos,int type)
{
ll tkey = ((key - (str[pos]-'a'+)*p[pos] ) % MOD + MOD + (type+)*p[pos] ) % MOD;
return tkey;
} int main()
{
int i,j;
p[] = ;
for(i=;i<maxn;i++){
p[i] = p[i-]*base % MOD;
}
int n,m;
cin>>n>>m;
while(n--){
scanf("%s",str);
key = getHash();
mySet.insert(key);
//cout<<key<<endl;
}
while(m--){
scanf("%s",str);
key = getHash();
int len = strlen(str);
int found = ;
for(i=;i<len;i++){
if(found) break;
for(j=;j<;j++){
if(str[i]-'a' == j) continue;
ll tkey = changeKey(key,i,j);
//cout<<tkey<<endl;
if(mySet.find(tkey) != mySet.end()){
cout<<"YES"<<endl;
found = ; break; }
}
}
if(!found) cout<<"NO"<<endl;
}
return ;
}
cf_514C(字符串哈希)的更多相关文章
- HDU 1880 魔咒词典(字符串哈希)
题目链接 Problem Description 哈利波特在魔法学校的必修课之一就是学习魔咒.据说魔法世界有100000种不同的魔咒,哈利很难全部记住,但是为了对抗强敌,他必须在危急时刻能够调用任何一 ...
- 洛谷P3370 【模板】字符串哈希
P3370 [模板]字符串哈希 143通过 483提交 题目提供者HansBug 标签 难度普及- 提交 讨论 题解 最新讨论 看不出来,这题哪里是哈希了- 题目描述 如题,给定N个字符串(第i个 ...
- HDU2594 Simpsons’ Hidden Talents 字符串哈希
最近在学习字符串的知识,在字符串上我跟大一的时候是没什么区别的,所以恶补了很多基础的算法,今天补了一下字符串哈希,看的是大一新生的课件学的,以前觉得字符串哈希无非就是跟普通的哈希没什么区别,倒也没觉得 ...
- LA 6047 Perfect Matching 字符串哈希
一开始我用的Trie+计数,但是不是计多了就是计少了,后来暴力暴过去的…… 看了别人的代码知道是字符串哈希,但是仍有几个地方不理解: 1.26^500溢出问题 2.没考虑哈希碰撞? 跪求指点! #in ...
- AC日记——【模板】字符串哈希 洛谷 3370
题目描述 如题,给定N个字符串(第i个字符串长度为Mi,字符串内包含数字.大小写字母,大小写敏感),请求出N个字符串中共有多少个不同的字符串. 友情提醒:如果真的想好好练习哈希的话,请自觉,否则请右转 ...
- 从Hash Killer I、II、III论字符串哈希
首先,Hash Killer I.II.III是BZOJ上面三道很经典的字符串哈希破解题.当时关于II,本人还琢磨了好久,但一直不明白为啥别人AC的代码都才0.3kb左右,直到CYG神犇说可以直接随机 ...
- 【NOIP模拟】Grid(字符串哈希)
题目背景 SOURCE:NOIP2016-RZZ-1 T3 题目描述 有一个 2×N 的矩阵,矩阵的每个位置上都是一个英文小写字符. 现在需要从某一个位置开始,每次可以移动到一个没有到过的相邻位置,即 ...
- 洛谷 P3370 【模板】字符串哈希
洛谷 P3370 [模板]字符串哈希 题目描述 如题,给定N个字符串(第i个字符串长度为Mi,字符串内包含数字.大小写字母,大小写敏感),请求出N个字符串中共有多少个不同的字符串. 友情提醒:如果真的 ...
- 洛谷 P3370 字符串哈希 (模板)
<题目链接> <转载于 >>> > 题目描述 如题,给定N个字符串(第i个字符串长度为Mi,字符串内包含数字.大小写字母,大小写敏感),请求出N个字符串中共 ...
随机推荐
- Hive实际应用小结
1.简介 Hive是数据仓库平台,构建在Hadoop之上用来处理结构化数据.Hive是一个SQL解析引擎,能够将SQL语句转化成MapReduce作业并在Hadoop上执行,从而使得查询和分析更加方便 ...
- Mysql 5.6到5.7的mysql.user改变
很久没配置mysql.昨天在centos服务器上装了个mysql,desc user的时候,找不到password column,看了官方API 才知道原来的password已经修改为authenti ...
- JDK8.0新特性
连接转载地址:http://www.2cto.com/kf/201609/544044.html Eclipse: http://aiyiupload.oss-cn-beijing.aliyuncs. ...
- Cleaner, more elegant, and wrong(msdn blog)
Cleaner, more elegant, and wrong Just because you can't see the error path doesn't mean it doesn't e ...
- Java订单功能模块设计与实现
在商城项目中,之前我们介绍了购物车功能模块的实现,商品加入到购物车之后,就是到购物车结算,然后显示购物车的商品列表,点击去结算,然后到了未提交前的订单列表, 点击提交订单后,生成此订单,返回订单的订单 ...
- C# 使用 CancellationTokenSource 终止线程
http://blog.csdn.net/hezheqiang/article/details/51966511 我们在多线程中通常使用一个 C# 使用 CancellationTokenSource ...
- .net 4.5 webform 提取ModelState错误信息
.net4.5以后,webform也可以使用模型绑定和模型验证. user实体: public class User { [Required] [Display(Name = "用户ID&q ...
- Java笔记:语法基础
Java语法基础 更新时间:2018-1-7 10:34:05 Hello World 文件名:HelloWorld.java public class HelloWorld { public sta ...
- 关于 python 新式类和旧式类继承顺序的验证
参考:http://www.cnblogs.com/blackmatrix/p/5630515.html 官方:https://docs.python.org/2/tutorial/classes.h ...
- zabbix2.2部署安装(安装环境Centos 6.* X64)
1.在已有的LAMP或者LNMP的基础上安装zabbix,安装一些依赖包: 安装epel源:rpm -ivh http://dl.fedoraproject.org/pub/epel/6/x86_64 ...