You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
50 lines
1.2 KiB
C++
50 lines
1.2 KiB
C++
#include <cstdio>
|
|
#include <cstring>
|
|
#include <map>
|
|
#include <algorithm>
|
|
using namespace std;
|
|
typedef unsigned long long ull;
|
|
const int N=1e5+50;
|
|
char str[N];
|
|
ull h[N],b[N];
|
|
map<ull,int> m;
|
|
ull gethash(int l,int r)
|
|
{
|
|
return h[r-1] - h[l-1]*b[r-l];
|
|
}
|
|
int main()
|
|
{
|
|
b[0]=1;
|
|
for(int i = 1; i < N; i++)
|
|
b[i] = b[i-1]*31;
|
|
int M,L;
|
|
while(~scanf("%d%d%s",&M,&L,str+1))
|
|
{
|
|
int len=strlen(str+1),ans=0;
|
|
h[0]=0;
|
|
for(int i=1; i<=len; i++)
|
|
h[i] = h[i-1]*31+ str[i] - 'a';
|
|
int lmt=min(L,len-M*L);
|
|
for(int i = 1; i <= lmt; i++)
|
|
{
|
|
m.clear();
|
|
for(int j = i; j < i + M*L ; j+=L)
|
|
m[gethash(j,j+L)]++;
|
|
if(m.size() == M)
|
|
ans++;
|
|
for(int j = i + M*L; j + L-1 <= len; j += L)
|
|
{
|
|
ull x = gethash(j,j+L);
|
|
ull y = gethash(j-M*L,j-M*L+L);
|
|
m[x]++,m[y]--;
|
|
if(m[y] == 0)
|
|
m.erase(y);
|
|
if(m.size() == M)
|
|
ans++;
|
|
}
|
|
}
|
|
printf("%d\n",ans);
|
|
}
|
|
return 0;
|
|
}
|