题目大意:
求有多少个子串满足条件:
1. 长度为M*L
2. 每个长度为L的小子串不能都完全相同的情况
解题方法:
字符串hash
我的代码:
#include <cstdio>
#include <string>
#include <iostream>
#include <cstring>
#include <map>
#define seed 131
#define maxn 100010
#define ull unsigned long long
using namespace std;
char s[maxn];
ull num[maxn],base[maxn];
map <ull,int> vis;
int totl,n,m;
int len;
int main (){
//freopen("test.in","r",stdin);
base[0]=1;
for (int i=1;i<=100009;i++) base[i]=base[i-1]*seed;
while (~scanf("%d%d",&n,&m)){
scanf("%s",s);
len=strlen(s);totl=0;
ull ub=0;
for (int i=0;i<m;i++){
ub=ub*seed+s[i]-'a'+1;
}
num[totl++]=ub;
for (int i=0;i+m<len;i++){
ub=ub*seed+(s[i+m]-'a'+1)-(s[i]-'a'+1)*base[m];
num[totl++]=ub;
}
int ans=0;
for (int i=0;i<m&&i+n*m<=len;i++){
vis.clear();
for (int j=i;j<i+n*m;j+=m)
vis[num[j]]++;
if (vis.size()==n) ans++;
for (int j=i+n*m;j+m<=len;j+=m){
vis[num[j-n*m]]--;
if (vis[num[j-n*m]]==0)vis.erase(num[j-n*m]);
vis[num[j]]++;
if (vis.size()==n) ans++;
}
}
printf("%d\n",ans);
}
return 0;
}