这道题没卡自然溢出的哈希,但mod为1e9的哈希被卡了。
双哈希只需把单哈希做法中的lst和ans转换成 <PII, int>类型的即可。
#include<bits/stdc++.h>
#define IOS ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
#define endl '\n'
using namespace std;
typedef pair<int, int> PII;
typedef long long ll;
const int N = 1000010, mod1 = 1e9 + 7, mod2 = 1e9 + 11;
const int P1 = 131, P2 = 13331;
int n, m, k;
char s[N];
ll p1[N], p2[N], h1[N], h2[N];
map<PII, int> lst, ans;
ll get1(int l, int r)
{
if(l > r)swap(l, r);
return (h1[r] - h1[l - 1] * p1[r - l + 1] % mod1 + mod1) % mod1;
}
ll get2(int l, int r)
{
if(l > r)swap(l, r);
return (h2[r] - h2[l - 1] * p2[r - l + 1] % mod2 + mod2) % mod2;
}
int main()
{
IOS
p1[0] = p2[0] = 1;
cin >> n >> m >> k;
cin >> s + 1;
for(int i = 1; i <= n; i ++)
{
h1[i] = (h1[i - 1] * P1 + s[i]) % mod1;
h2[i] = (h2[i - 1] * P2 + s[i]) % mod2;
p1[i] = p1[i - 1] * P1 % mod1;
p2[i] = p2[i - 1] * P2 % mod2;
}
for(int i = 1; i <= n - m + 1; i ++)
{
int hash1 = get1(i, i + m - 1), hash2 = get2(i, i + m - 1);
if(lst[{hash1, hash2}] < i)
{
lst[{hash1, hash2}] =i + m - 1;
ans[{hash1, hash2}] ++;
}
}
int res = 0;
for(auto item : ans)
{
if(item.second == k)res ++;
}
cout << res;
return 0;
}