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个字符串中共 ...
随机推荐
- Python Pandas 库的使用例子
主要在jupyter notebook里面熟悉这个库的使用,它的安装方法与实现,可自行搜索. Pandas是一个优秀的数据分析工具,官网:http://pandas.pydata.org/ 相关的库使 ...
- 关于vs2010下水晶报表的使用入门
关于vs2010下使用水晶报表了解情况记录如下: 1.首先vs2010不再自带水晶报表控件了,需要下载安装vs2010配套的水晶报表控件:CRforVS_13_0.这个控件安装很简单,基本上都选择默认 ...
- 房上的猫:了解java与学习java前的准备
一.java 概述: 1.通常指完成某些事情的一种既定方式和过程 2.程序可以看做对一系列动作执行过程的描述 3.计算机按照某种顺序完成一系列指令的集合称为程序 4.计算机仅识别二进制低级语言 ...
- vue实现登录后跳转到之前的页面
在开发中我们经常遇到这样的需求,需要用户直接点击一个链接进入到一个页面,用户点击后链接后会触发401拦截返回登录界面,登录后又跳转到链接的页面而不是首页,这种问题该如何去做呢? 先说一下我们需要用到的 ...
- openldap 搭建
环境构建 1)软件安装: yum -y install openldap-servers openldap-clients openldap openldap-devel migrationtools ...
- zabbix 图形插件 Grafana的安装
看http://www.myexception.cn/software-testing/2008870.html 就好了.
- JS中金额转换以及格式化
abs = function(val){ //金额转换 分->元 保留2位小数 并每隔3位用逗号分开 1,234.56 var str = (val/100).toFixed(2) + ''; ...
- 基于web的网上书城系统开发-----登录注册
注册功能实现 signup.jsp //时间实现 function showLocale(objD) { var str,colorhead,colorfoot; var yy = objD.getY ...
- 关于dl dt dd 文字过长换行在移动端显示对齐的探讨总结
关于dl dt dd 文字过长换行在移动端显示对齐的探讨总结 <dl> <dt>抵押房产:</dt> <dd>1.北京市大兴区兴华大街丽园小区3单大兴 ...
- 机器学习小记——KNN(K近邻) ^_^ (一)
为了让绝大多数人都可以看懂,所以我就用简单的话语来讲解机器学习每一个算法 第一次写ML的博文,所以可能会有些地方出错,欢迎各位大佬提出意见或错误 祝大家开心进步每一天- 博文代码全部为python 简 ...