841. 字符串哈希 ###K ###K //K

题目链接:https://www.acwing.com/problem/content/description/843/

把每个字符串转换成对应的P 进制下的数字   P取131时 在ull 范围内 的哈希冲突可以忽略

用ull存不用取模 溢出的时候就等同于取模

 1 #include<bits/stdc++.h>
 2 using namespace std;
 3 const int maxn=1e5+10;
 4 const int mod=998244353;
 5 #define ll long long
 6 #define ull unsigned long long
 7 #define pb push_back
 8 #define pi pair<int,int>
 9 #define fi first
10 #define sc second
11 
12 char s[maxn];
13 int P=131;
14 ull p[maxn],h[maxn];
15 int n,m;
16 
17 ull get(int l,int r)
18 {
19     return h[r]-h[l-1]*p[r-l+1];
20     // 高位对齐 差r-l+1位
21     // 123456 -123 将123 变为123000 剩下的456即要求哈希值
22 }
23 
24 
25 int main()
26 {
27     ios::sync_with_stdio(0);
28     cin.tie(0);
29     cin>>n>>m;
30     cin>>(s+1);
31     p[0]=1;//p^0=1
32     for(int i=1;i<=n;i++)
33     {
34         p[i]=p[i-1]*P;
35         h[i]=h[i-1]*P+s[i];
36     }
37     while(m--)
38     {
39         int l1,r1,l2,r2;
40         cin>>l1>>r1>>l2>>r2;
41         if(get(l1,r1)==get(l2,r2)) cout<<"Yes"<<'\n';
42         else cout<<"No"<<'\n';
43     }
44 
45 
46 
47 
48 }
View Code

 

posted @ 2021-01-29 15:23  canwinfor  阅读(170)  评论(0)    收藏  举报