当前位置:网站首页>841. String hash
841. String hash
2022-07-07 19:55:00 【Hunter_ Kevin】
Given a length of n String , Re given m A asked , Each query contains four integers l1,r1,l2,r2, Please judge [l1,r1] and [l2,r2] Whether the strings and substrings contained in these two intervals are exactly the same .
The string contains only uppercase and lowercase English letters and numbers .
Input format
The first line contains integers n and m, Represents the length of the string and the number of queries .
The second line contains a length of n String , The string contains only uppercase and lowercase English letters and numbers .
Next m That's ok , Each line contains four integers l1,r1,l2,r2, Indicates the two intervals involved in a query .
Be careful , The position of the string is from 1 Numbered starting .
Output format
Output a result for each query , If two string substrings are identical, output Yes, Otherwise output No.
Each result takes up one line .
Data range
1≤n,m≤105
sample input :
8 3
aabbaabb
1 3 5 7
1 3 6 8
1 2 1 2
sample output :
Yes
No
Yes
Ideas :
①、 String subscript from 1 Start
②、 First , The given string , Press the subscript to find from left to right i Is the last string , Find its character based ascii code , be based on P Hexadecimal accumulated string hash value
③、 Based on empirical values , A string of hash value , With 264 Data size for storage , With 131 perhaps 13331 When it is a decimal number , Cumulative hash It's worth it 264 modulus , The probability of conflict is very small , Think ignore
④、unsigned long long just 264, use ull For storage , If the data overflows , That is, high-order truncation , Keep it low , It's equivalent to automatic Yes 264 modulus
⑤、 The key point of the algorithm is the prefix of string hash Value preprocessing and p^i Array preprocessing
seekA string l~r
Corresponding hash value , It only needsh[r] - h[l-1] * p[(r-1) - (l-1-1)]
namelyh[r] - h[l-1] * p[r-l+1]
Example :123456
seek 34 Of hash value
1234 - 12 * 10^2 == 34
#include <iostream>
#include <cstring>
using namespace std;
typedef unsigned long long ULL;
const int N = 100010, P = 131;
// Empirical value Use 131 perhaps 13331 Use 2^64 To store the size of the string hash value , Theoretically With 131 perhaps 13331 For the string of hexadecimal numbers hash value mod 2^64 The probability of conflict after that is very small , It can be seen that there is no conflict
ULL h[N], p[N];//h The array stores the string before the subscript hash value ,p The array stores the bit weight of the subscript position base
char s[N];
int main()
{
int n, m;
cin>> n >> m >> s+1;
// Find the string of hash Value preprocessing results and Subscript to be i The position of the corresponding p^i
p[0] = 1;
for(int i = 1; i <= n; i++)
{
p[i] = p[i-1] * P;// Find the subscript i need × Bit weight of p^0 p^1 p^2 p^3 1 1*p 1*p*p 1*p*p*p
h[i] = h[i-1] * P + s[i];// Find out i The position corresponds to hash value ,ULL After spilling , High truncation , Keep it low , It's equivalent to 2^64 modulus ,s[i] The actual value is the hash value of the character
}
int l1,r1,l2,r2;
while(m -- )
{
cin>>l1>>r1>>l2>>r2;
ULL x = h[r1] - h[l1-1] * p[r1-l1+1]; // Find a string hash value ,r1 Corresponding hash value - (l1-1) Corresponding hash value * p^(r1-l1+1) namely The difference between the two positions p^(r1-1 - (l1-1-1))
ULL y = h[r2] - h[l2-1] * p[r2-l2+1];
if(x == y)cout<<"Yes"<<endl; // If the hash values of two strings are equal , Then the strings are equal
else cout<<"No"<<endl;
}
return 0;
}
边栏推荐
- 关于ssh登录时卡顿30s左右的问题调试处理
- 我的创作纪念日
- 浏览积分设置的目的
- 831. KMP字符串
- 模拟实现string类
- Kunpeng developer summit 2022 | Kirin Xin'an and Kunpeng jointly build a new ecosystem of computing industry
- R语言ggplot2可视化:使用ggpubr包的ggviolin函数可视化小提琴图、设置palette参数自定义不同水平小提琴图的填充色、add参数在小提琴图添加箱图
- Visual Studio 插件之CodeMaid自动整理代码
- 开源OA开发平台:合同管理使用手册
- Navicat连接2002 - Can‘t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock‘解决
猜你喜欢
RESTAPI 版本控制策略【eolink 翻译】
[RT thread env tool installation]
LeetCode力扣(剑指offer 36-39)36. 二叉搜索树与双向链表37. 序列化二叉树38. 字符串的排列39. 数组中出现次数超过一半的数字
PMP practice once a day | don't get lost in the exam -7.7
Chief technology officer of Pasqual: analog quantum computing takes the lead in bringing quantum advantages to industry
Classification automatique des cellules de modules photovoltaïques par défaut dans les images de lecture électronique - notes de lecture de thèse
Kunpeng developer summit 2022 | Kirin Xin'an and Kunpeng jointly build a new ecosystem of computing industry
Tips and tricks of image segmentation summarized from 39 Kabul competitions
多个线程之间如何协同
Download from MySQL official website: mysql8 for Linux X Version (Graphic explanation)
随机推荐
【剑指offer】剑指 Offer II 012. 左右两边子数组的和相等
Tp6 realize Commission ranking
AD域组策略管理
Research and practice of super-resolution technology in the field of real-time audio and video
Kirin Xin'an cloud platform is newly upgraded!
R语言ggplot2可视化:使用ggpubr包的ggstripchart函数可视化分组点状条带图(dot strip plot)、设置position参数配置不同分组数据点的分离程度
openEuler 有奖捉虫活动,来参与一下?
Classification automatique des cellules de modules photovoltaïques par défaut dans les images de lecture électronique - notes de lecture de thèse
A pot of stew, a collection of common commands of NPM and yarn cnpm
tp6 实现佣金排行榜
超分辨率技术在实时音视频领域的研究与实践
实训九 网络服务的基本配置
使用高斯Redis实现二级索引
Jürgen Schmidhuber回顾LSTM论文等发表25周年:Long Short-Term Memory. All computable metaverses. Hierarchical reinforcement learning (RL). Meta-RL. Abstractions in generative adversarial RL. Soccer learn
华南X99平台打鸡血教程
R语言ggplot2可视化:使用ggpubr包的ggecdf函数可视化分组经验累积密度分布函数曲线、linetype参数指定不同分组曲线的线型
Kirin Xin'an won the bid for the new generation dispatching project of State Grid!
The DBSCAN function of FPC package of R language performs density clustering analysis on data, checks the clustering labels of all samples, and the table function calculates the two-dimensional contin
ASP.NET体育馆综合会员管理系统源码,免费分享
Matplotlib drawing 3D graphics