当前位置:网站首页>字符串哈希 哈希值
字符串哈希 哈希值
2022-08-08 05:42:00 【一条小小yu】
给定一个长度为 nn 的字符串,再给定 mm 个询问,每个询问包含四个整数 l1,r1,l2,r2l1,r1,l2,r2,请你判断 [l1,r1][l1,r1] 和 [l2,r2][l2,r2] 这两个区间所包含的字符串子串是否完全相同。
字符串中只包含大小写英文字母和数字。
输入格式
第一行包含整数 nn 和 mm,表示字符串长度和询问次数。
第二行包含一个长度为 nn 的字符串,字符串中只包含大小写英文字母和数字。
接下来 mm 行,每行包含四个整数 l1,r1,l2,r2l1,r1,l2,r2,表示一次询问所涉及的两个区间。
注意,字符串的位置从 11 开始编号。
输出格式
对于每个询问输出一个结果,如果两个字符串子串完全相同则输出 Yes,否则输出 No。
每个结果占一行。
数据范围
1≤n,m≤1051≤n,m≤105
输入样例:
8 3
aabbaabb
1 3 5 7
1 3 6 8
1 2 1 2
输出样例:
Yes
No
Yes
y总一语道破,原来就是进制啊
#include<iostream>
#include<cstdio>
#include<string>
using namespace std;
typedef unsigned long long ULL;
const int N = 1e5+5,P = 131;//131 13331
ULL h[N],p[N];
// h[i]前i个字符的hash值
// 字符串变成一个p进制数字,体现了字符+顺序,需要确保不同的字符串对应不同的数字
// P = 131 或 13331 Q=2^64,在99%的情况下不会出现冲突
// 使用场景: 两个字符串的子串是否相同
ULL query(int l,int r){
return h[r] - h[l-1]*p[r-l+1];
}
int main(){
int n,m;
cin>>n>>m;
string x;
cin>>x;
//字符串从1开始编号,h[1]为前一个字符的哈希值
p[0] = 1;
h[0] = 0;
for(int i=0;i<n;i++){
p[i+1] = p[i]*P;
h[i+1] = h[i]*P +x[i]; //前缀和求整个字符串的哈希值
}
while(m--){
int l1,r1,l2,r2;
cin>>l1>>r1>>l2>>r2;
if(query(l1,r1) == query(l2,r2)) printf("Yes\n");
else printf("No\n");
}
return 0;
}
边栏推荐
- 0字典树/字符串中等 LeetCode676. 实现一个魔法字典
- clue binary tree
- Hard Disk Basics
- Why do big Internet companies keep hiring while frantically laying off staff?
- Typescript 命名空间
- 【MySQL】——事务的基本概念
- postman---postman参数化
- Redis设置开机自启动
- The only OpenCyphal/UAVCAN tutorial in the whole network (11) Write a Cyphal protocol parsing tool with candump and gawk tools
- 数据库分库分表,何时分?怎样分?
猜你喜欢

【数学建模】微分方程求解 | dsolve函数 | ode45函数

Redis set to start automatically at boot
![[u-boot] Analysis of the driver model of u-boot](/img/c2/d8ac24a3cfafc4d9bcbd1ed5c5f89b.png)
[u-boot] Analysis of the driver model of u-boot

Postman显示验证码图片(base64字符串)

Unity mouse cursor usage learning

Checkerboard Coloring Problem

基本工具-NETCAT(telnet-banner、传输文本信息)

棋盘染色问题

28.异常检测

Eighteen, OIDC OAuth2 】 【 the understanding of the application
随机推荐
IP核之RAM实验
数据库分库分表,何时分?怎样分?
Filter 过滤器的使用
[Untitled] I haven't thought of a name yet
Week 8 Generative Adversarial Networks
分布式事务 :可靠消息最终一致性方案
Sequence table (below)
Several postman features worth collecting will help you do more with less!
Talk about the principle and implementation of Redis distributed lock [Ant Financial Services Three Sides]
查询跟踪多家快递单号,筛选某一时间发货的单号
[Redis] Redis Learning - Transaction
121 distributed interview questions and answers
tracepoint: 定义函数及调用示例
Servlet---ServletConfig类使用介绍
主脑提示( Master-Mind Hints )
C language framework FreeSwitch custom event introduction and usage example
Rust开发——Struct使用示例
sqlmap+dnslog注入复现
基础了解虚拟 DOM
Web attack log analysis: a guide for beginners