先贴一个作弊单哈希AC的代码哈哈哈哈哈。
#include <bits/stdc++.h>
using namespace std;
#define N (int)2e6
typedef long long ll;
ll p1, p2;
ll mod[2] = {10000007, 1000000009};
int m[10000100];
const int seed = 131;
string str;
map<pair<ll, ll>, int> mp;
ll qpow(ll a, ll b, ll m)
{
ll ret = 1;
while (b)
{
if (b & 1) ret = ret*a%m;
b >>= 1;
a = a*a%m;
}
return ret;
}
int main(void)
{
int n;
scanf("%d", &n); getchar();
getline(cin, str);
str = ' '+str;
int i, j;
p1 = qpow(seed, n-1, mod[0]);
p2 = qpow(seed, n-1, mod[1]);
ll tmp = 0;
for (i = 1; i <= n; i++)
{
tmp = (tmp*seed+str[i]) % mod[0];
}
ll temp = tmp;
int ma = -1, res;
if (++m[temp] > ma)
{
ma = m[temp];
res = 1;
}
for (i = 2; i <= str.size()-n+1; i++)
{
temp = (1ll * (temp - str[i-1]*p1+str[i-1]*mod[0])%mod[0] * seed + str[i+n-1])%mod[0];
// ll temp2 = (1ll * (temp.second - str[i-1]*p2+mod[1])%mod[1] * seed + str[i+n-1])%mod[1];
// temp = make_pair(temp1, temp2);
//ll w = ++mp[temp];
if (++m[temp] > ma)
{
ma = m[temp];
res = i;
}
else if (m[temp] == ma)
{
if (str.size() < 1e5)if (str.substr(res, n) > str.substr(i, n)) res = i;
}
}
for (i = res; i < res+n; i++)
{
printf("%c", str[i]);
}
printf(" %d", ma);
}