题面
多hash+map防卡就是了。
详细的卡hash方法
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<map>
#define ll long long
using namespace std;
const int maxn=200010;
int n,m,mod[10]={1000000007,1000000009,998244353,666623333,19260817};
char s[maxn];
ll h[maxn][10],mi[10];
struct node
{
ll h[10];
};
bool operator <(node a,node b)
{
for(int t=0;t<=4;t++)
if(a.h[t]!=b.h[t]) return a.h[t]<b.h[t];
return 0;
}
map<node,bool> mp;
int main()
{
scanf("%d%d",&n,&m);
scanf("%s",s+1);
for(int t=0;t<=4;t++)
{
mi[t]=1;
for(int i=1;i<=m;i++) mi[t]=mi[t]*131%mod[t];
for(int i=1;i<=n;i++) h[i][t]=(h[i-1][t]*131%mod[t]+(s[i]-'a'))%mod[t];
}
for(int i=m;i<=n;i++)
{
node tmp;
for(int t=0;t<=4;t++) tmp.h[t]=(h[i][t]-h[i-m][t]*mi[t]%mod[t]+mod[t])%mod[t];
if(!mp.count(tmp)) mp[tmp]=1;
}
printf("%d",mp.size());
return 0;
}
本文介绍了一种使用多Hash结合Map的数据结构来防止在特定场景下出现卡顿的方法。通过定义多个不同的模数并利用哈希函数,确保字符串匹配的准确性与效率。代码示例展示了如何实现这一算法,并通过实例解释了如何避免哈希冲突。

1268

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



