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个字符串中共 ...
随机推荐
- C#中级-Windows Service程序安装注意事项
一.前言 这周除了改写一些识别算法外,继续我的Socket服务编写.服务器端的Socket服务是以Windows Service的形式运行的. 在我完成Windows Service编写后,启动服务时 ...
- jQuery中$(function(){})与(function($){})(jQuery)、$(document).ready(function(){})等的区别详细讲解 ----转载
1.(function($) {-})(jQuery); 1).原理: 这实际上是匿名函数,如下: function(arg){-} 这就定义了一个匿名函数,参数为arg 而调用函数时,是在函数后面写 ...
- find + xargs + cp 遇到文件名中带空格如何处理
一,需求为查询文件名为ZRSH开头的时间为7月至今的所有文件并打包 1.首先想到的就是find + xargs + cp 格式.. find 2016073* -type f -name *ZRS ...
- Visual Representation of SQL Joins
原文:http://www.codeproject.com/Articles/33052/Visual-Representation-of-SQL-Joins 从视图上介绍了7种不同类型的JOIN ...
- Raspberry Pi中可用的Go IDE:liteide
p { margin-bottom: 0.25cm; line-height: 120% } a:link { } Raspberry Pi中可用的Go IDE:liteide p { margin- ...
- flask入门篇
flask,Flask是一个使用 Python 编写的轻量级 Web 应用框架.其 WSGI 工具箱采用 Werkzeug ,模板引擎则使用 Jinja2 . Flask简单易学,属于轻量级的,学起来 ...
- Java中读取txt文件中中文字符时,出现乱码的解决办法
这是我写的一个Java课程作业时,遇到的问题. 问题描述: 我要实现的就是将txt文件中的内容按一定格式读取出来后,存放在相应的数组. 我刚开始运行时发现,英文可以实现,但是中文字符就是各种乱码. 最 ...
- SQL 多列合并一列
select rtrim(姓)+ rtrim(名) as 姓名 from tb
- 常用的 css reset,基本的base.css
@charset "utf-8"; html { overflow-x: hidden; overflow-y: auto; } /*隐藏横向滚动,垂直滚动根据内容自 ...
- python常用模块详解
python常用模块详解 什么是模块? 常见的场景:一个模块就是一个包含了python定义和声明的文件,文件名就是模块名字加上.py的后缀. 但其实import加载的模块分为四个通用类别: 1 使用p ...