好久不见。
出于某方面的原因,以及自己近两个月的无所事事实力下降,让我意识到了写博客来监督自己的学习成果是十分重要的。
也希望各位哥哥们发现我经常摸鱼的时候直接骂我。
以下内容可能有许多错误,仅供个人学习参考
什么是哈希?
可以理解成对一串字符串的处理,将一串字符串处理为一个数字,即该数字就可以代表该字符串,类似于密码的加密过程。那么,我们便可以通过一个字符串得到一个哈希值(反过来不行),相同的字符串一定拥有相同的哈希值。
该如何得到哈希值呢?
将字符串转化成一个p进制的数字,然后对其模q。
为了减少哈希值的重复,我们一般将p规定为131或13331
将q规定为264,于是我们只需要对hash的值规定为unsigned long long,便可以自动实现取模的效果
兔子与兔子
对于字符串所有子串hash值
h
[
i
]
=
h
[
i
−
1
]
∗
131
+
a
h[i]=h[i-1]*131+a
h[i]=h[i−1]∗131+a
同时也要记录截至位置131的幂次方
p
[
i
]
=
p
[
i
−
1
]
∗
131
p[i]=p[i-1]*131
p[i]=p[i−1]∗131
于是,对于l-r范围内的子串的hash值
h
a
s
h
(
l
−
r
)
=
h
a
s
h
[
r
]
−
h
a
s
h
[
l
−
1
]
∗
p
o
w
(
131
,
(
l
−
r
)
)
hash(l-r)=hash[r]-hash[l-1]*pow(131,(l-r))
hash(l−r)=hash[r]−hash[l−1]∗pow(131,(l−r))
即
h
a
s
h
(
l
−
r
)
=
h
a
s
h
[
r
]
−
h
a
s
h
[
l
−
1
]
∗
p
[
l
−
r
+
1
]
hash(l-r)=hash[r]-hash[l-1]*p[l-r+1]
hash(l−r)=hash[r]−hash[l−1]∗p[l−r+1]
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<cmath>
#include<queue>
#include<map>
#include<vector>
#include<stack>
#include<set>
#include<cstring>
#include<string>
#define inf 0x3f3f3f3f
typedef long long ll;
typedef unsigned long long ull;
using namespace std;
char s[1000005];
ull base=131,p[1000005],h[1000005];
ull get_hash(int l,int r)
{
return h[r]-h[l-1]*p[r-l+1];
}
int main()
{
scanf("%s",s+1);
int l=strlen(s+1);
p[0]=1;
for(int i=1;i<=l;i++)
{
h[i]=h[i-1]*base+s[i]-'a'+1;
p[i]=p[i-1]*base;
}
int m;
scanf("%d",&m);
while(m--)
{
int a,b,c,d;
scanf("%d%d%d%d",&a,&b,&c,&d);
if(get_hash(a,b)==get_hash(c,d))
{
printf("%llu %llu\n",get_hash(a,b),get_hash(c,d));
printf("Yes\n");
}
else
printf("No\n");
}
}
本文介绍了哈希算法的基本概念,通过将字符串转化为数字并取模来减少冲突,以此快速查找字符串的子串。文章还展示了如何计算子串的哈希值,并提供了C++代码实现。此外,文中提到了利用131和264作为模数来减少哈希冲突的方法。
545

被折叠的 条评论
为什么被折叠?



