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个字符串中共 ...
随机推荐
- 453. Minimum Moves to Equal Array Elements
Given anon-emptyinteger array of sizen, find the minimum number of moves required to make all array ...
- 公牛与状压dp
T1 疾病管理 裸得不能再裸的状压dp 不过数据范围骗人 考试时k==0的点没过 我也很无奈呀qwq #include<iostream> #include<cstdio> # ...
- Confluence5.4.4迁移至6.3.1
1.数据备份 服务器查看: 2.安装破解文件及安装包至服务器 3.停止旧版本并启动安装 4.访问8090端口开始安装 5.获取授权码,需要能访问国外网站,并且有atlassian账号 6.将数据库连接 ...
- OpenStack运维(一):OpenStack项目和用户
1.添加项目 keystone tenant-create --name=demo [--description tenant-description --enable false] demo:项目名 ...
- 20行JS代码实现贪吃蛇
<!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Linux 文本编辑器vi命令
1.Vim Vim 是一个功能强大的全屏幕文本编辑器,是 Linux/UNIX 上最常用的文本编辑器,它的作用是建立.编辑.显示文本文件. Vim 没有菜单,只有命令 2.Vim 工作模式 3.插入 ...
- Ubuntu16.04 添加 Docker用户组
Ubuntu16.04 添加 Docker用户组 将用户添加到docker用户组就不用每次都 sudo了. ### 首先创建用户组 sudo groupadd docker 将用户加如组 sudo g ...
- ASP.NET Core学习之四 在CentOS上部署.net core
一.安装CentOs 以前在大学学过linux,但是对命令行总是有一种深深的排斥感,几年之后,还是又回来了. 1.下载 现在没法FQ,就算是FQ网速也是蜗牛一样慢,我使用阿里云的镜像站进行下载速度还是 ...
- 微信小程序之HTTPS请求
1.wx.request wx.request发起的是 HTTPS 请求,以下是它的基本用法,这里不再啰嗦 https://mp.weixin.qq.com/debug/wxadoc/dev/api/ ...
- TurnipBit口袋编程计算机:和孩子一起DIY许愿的流星
听说对着流星许愿,许的愿望都会实现,虽然不知道这个说法是不是真的,但是流星还是很好看的,为了能一直看到流星,今天就自己做一个流星保存下来,想什么时候看,就什么时候看. 首先需要想象一下流星是什么样子的 ...