zz:https://blog.csdn.net/Y_sofun/article/details/74502970
nodgd的文章由n个小写英文字母组成。文章的一个子串指的是文章中的一段连续的字母,子串的长度就是这一段的字母个数。nodgd在文章中用了排比、对偶、前后照应之类的手法,所以就有很多个子串是相同或者相近的。为了向大家证明这是一篇好文章,nodgd决定给自己的文章进行评分。nodgd 首先确定了一个整数m,然后统计出文章中有多少个不相同的长度为m的子串,这个数量就是文章的评分。
Input
第一行包含两个整数n,m,表示文章的长度和需要统计的子串长度。
1≤m≤n≤200000
第二行包含一个长度为n的只包含小写字母的字符串。
Output
一行一个整数,表示文章的评分。
Sample Input
5 3
aaaab
Sample Output
2
【提示】
【样例解释1】
长度为3的子串有3个,分别是 aaa,aaa,aab,其中不同的只有2个。




#include<cstdio>
#include<algorithm>
#include<queue>
#include<cstring>
#define N 200003
#define ll long long
#define M1 2333333
#define M2 998244353
using namespace std;
char ch;
void read(int& n)
{
n=0;
for(ch=getchar();ch<'0'||ch>'9';ch=getchar());
for(;'0'<=ch && ch<='9';n=(n<<3)+(n<<1)+ch-48,ch=getchar());
}
void G(char &ch)
{
for(ch=getchar();ch<'a'||ch>'z';ch=getchar());
}
int n,m,ans;
ll z1[N],z2[N],p[N],s1,s2,h[M1][2];
bool ins()
{
int x=s1%M1;
while((h[x][0]!=0 || h[x][1]!=0)&&(h[x][0]!=s1 || h[x][1]!=s2))x=(x+1)%M1;
if(h[x][0]==s1 && h[x][1]==s2)return 0;
h[x][0]=s1;
h[x][1]=s2;
return 1;
}
int main()
{
read(n);read(m);
z1[0]=z2[0]=1;
for(int i=1;i<=m;i++)
z1[i]=z1[i-1]*m%M1,z2[i]=z2[i-1]*m%M2,G(ch),p[i]=ch-48;
s1=s2=0;
for(int i=1;i<=m;i++)
s1=(z1[m-i]*p[i]+s1)%M1,s2=(z2[m-i]*p[i]+s2)%M2;
ans=1;
ins();
for(int i=m+1;i<=n;i++)
{
G(ch),p[i]=ch-48;
s1=(s1-z1[m-1]*p[i-m]%M1+M1)%M1;
s2=(s2-z2[m-1]*p[i-m]%M2+M2)%M2;
s1=(s1*m+p[i])%M1;
s2=(s2*m+p[i])%M2;
if(ins())ans++;
}
printf("%d
",ans);
return 0;
}
#include<bits/stdc++.h>
using namespace std;
const int Maxn=200005;
const int e1=31,e2=131,p1=998244353,p2=1e9+7;
long long f1[Maxn]={1},f2[Maxn]={1};
set<pair<long long,long long> >ss;
char s[Maxn];
int main(){
int n,m;scanf("%d%d%s",&n,&m,s+1);
for(int i=1;i<=m;++i)f1[i]=f1[i-1]*e1%p1;
for(int i=1;i<=m;++i)f2[i]=f2[i-1]*e2%p2;
long long st1=0,st2=0;
for(int i=1;i<=m;++i)
{
st1=(st1*e1%p1+s[i]-'a')%p1;
st2=(st2*e2%p2+s[i]-'a')%p2;
}
ss.insert(make_pair(st1,st2));
for(int i=m+1;i<=n;++i){
st1=((st1-(s[i-m]-'a')*f1[m-1]%p1+p1)*e1+s[i]-'a')%p1;
st2=((st2-(s[i-m]-'a')*f2[m-1]%p2+p2)*e2+s[i]-'a')%p2;
ss.insert(make_pair(st1,st2));
}
printf("%d
",ss.size());
return 0;
}
#include <cstdio>
#include <cstring>
#include <iostream>
#include <string>
#include <algorithm>
#include <cstdlib>
#define LL long long
#define MO 1000000007
#define mo 1000000009
#define N 200009
#define P 61
#define p 97
using namespace std;
char s[N];
struct arr
{
LL H, h;
}hashh[N];
LL ans, n, m;
LL HVAL[N], mp[N], MP[N], hval[N];
LL Cmp(arr x, arr y)
{
if (x.H == y.H) return x.h < y.h;
return x.H < y.H;
}
void Pre_work()
{
mp[0] = 1, MP[0] = 1;
for (int i = 1; i <= n; i++)
{
HVAL[i] = (HVAL[i - 1] * P + (s[i] - 'a' + 1)) % MO;
hval[i] = (hval[i - 1] * p + (s[i] - 'a' + 1)) % mo;
mp[i] = (mp[i - 1] * p) % mo, MP[i] = (MP[i - 1] * P) % MO;
}
}
void Getnum(LL l, LL r, LL ain)
{
hashh[ain].h = (hval[r] - hval[l - 1] * mp[r - l + 1] % mo + mo) % mo;
hashh[ain].H = (HVAL[r] - HVAL[l - 1] * MP[r - l + 1] % MO + MO) % MO;
}
int main()
{
scanf("%d%d", &n, &m);
cin >> s + 1;
Pre_work();
for (int i = 1; i <= n - m + 1; i++)
Getnum(i, i + m - 1, i);
sort(hashh + 1, hashh + n - m + 2, Cmp);
for (int i = 1; i <= n - m + 1; i++)
if (hashh[i].h == hashh[i + 1].h && hashh[i].H == hashh[i + 1].H) continue;
else ans++;
printf("%d", ans);
}