HASH后就是一个简单的线性统计了
#include <iostream>
#include <cstdio>
#include <cstdlib>
#include <cmath>
#include <queue>
#include <algorithm>
#include <vector>
#include <cstring>
#include <stack>
#include <cctype>
#include <utility>
#include <map>
#include <string>
#include <climits>
#include <set>
#include <string>
#include <sstream>
#include <ctime>
#include <bitset>
#include <iomanip>
//#pragma comment(linker, "/STACK:102400000,102400000")
using std::priority_queue;
using std::vector;
using std::swap;
using std::stack;
using std::sort;
using std::max;
using std::min;
using std::pair;
using std::map;
using std::string;
using std::cin;
using std::cout;
using std::set;
using std::queue;
using std::string;
using std::stringstream;
using std::make_pair;
using std::getline;
using std::greater;
using std::endl;
using std::multimap;
using std::deque;
using std::unique;
using std::lower_bound;
using std::random_shuffle;
using std::bitset;
using std::upper_bound;
using std::multiset;
using std::ios;
using std::make_heap;
using std::push_heap;
using std::pop_heap;
typedef long long LL;
typedef unsigned long long ULL;
typedef unsigned UN;
typedef pair<int, int> PAIR;
typedef multimap<int, int> MMAP;
typedef long double LF;
const int MAXN(100010);
const int MAXM(10010);
const int MAXE(2100010);
const int MAXK(6);
const int HSIZE(13131);
const int SIGMA_SIZE(26);
const int MAXH(18);
const int INFI((INT_MAX-1) >> 1);
const ULL BASE(31);
const LL LIM(1e13);
const int INV(-10000);
const int MOD(1000000007);
const double EPS(1e-7);
const LF PI(acos(-1.0));
template<typename T> inline bool checkmax(T &a, T b){if(b > a) { a = b; return true;} return false;}
template<typename T> inline bool checkmin(T &a, T b){if(b < a) { a = b; return true;} return false;}
template<typename T> inline T ABS(T a){return a < 0? -a: a;}
template<typename T> inline bool EZ(T a){return ABS(a) < EPS;}
char str[MAXN];
map<ULL, int> mp[MAXN];
ULL H[MAXN], XP[MAXN];
ULL getV(int ind, int l){
return H[ind]-H[ind+l]*XP[l];
}
int main(){
int m, l;
XP[0] = 1;
for(int i = 1; i <= 100000; ++i) XP[i] = XP[i-1]*BASE;
while(~scanf("%d%d", &m, &l)){
scanf("%s", str);
int len = strlen(str);
H[len] = 0;
for(int i = len-1; i >= 0; --i)
H[i] = H[i+1]*BASE+str[i]-'a'+1;
int ans = 0;
int ml = m*l;
for(int i = 0; i < l && i+ml <= len; ++i){
mp[i].clear();
int star = i;
for(int j = 0; j < m; ++j, star += l){
ULL val = getV(star, l);
if(mp[i].count(val)) ++mp[i][val];
else mp[i].insert(make_pair(val, 1));
}
if(mp[i].size() == m) ++ans;
}
int tml = l*(m-1);
for(int i = l; i+ml <= len; ++i){
int ti = i%l;
ULL val = getV(i-l, l);
--mp[ti][val];
if(mp[ti][val] == 0) mp[ti].erase(val);
val = getV(i+tml, l);
if(mp[ti].count(val)) ++mp[ti][val];
else mp[ti].insert(make_pair(val, 1));
if(mp[ti].size() == m) ++ans;
}
printf("%d\n", ans);
}
return 0;
}