#include<algorithm>
#include<iostream>
#include<cstring>
#include<cstdio>
using namespace std;

typedef long long ll;

struct HH{
    ll mod,base,d;
    ll sta;
    void update(int x){
        sta+=x;sta*=base;sta%=mod;
    }
    void del(int x){
        sta-=(x*d);
        while(sta<0) sta+=mod;
    }
}h1,h2;

const int MAXN=2000005;

ll qpow(ll x,int y,ll mod){
    ll ret=1;
    while(y){
        if(y&1){ret*=x;ret%=mod;}
        x*=x;x%=mod;
        y>>=1;
    }
    return ret;
}

struct Node{
    ll x,y;
    Node(ll u=0,ll v=0){x=u;y=v;}
    bool operator <(const Node &rhs) const {
        return x==rhs.x?y<rhs.y:x<rhs.x;
    }
}node[MAXN];
int tot;

int n,m;
char s[MAXN];
int ans=1;

signed main(){
//    freopen("article.in","r",stdin);
//    freopen("article.out","w",stdout);
    scanf("%d%d",&n,&m);
    h1.base=27;h2.base=27;
    h1.mod=19260817;
    h2.mod=19491001;
    h1.d=qpow(h1.base,m,h1.mod);
    h2.d=qpow(h2.base,m,h2.mod);
    scanf("%s",s+1);
    for(int i=1;i<=m;i++){
        h1.update(s[i]-'a');
        h2.update(s[i]-'a');
    }
    node[++tot]=(Node(h1.sta,h2.sta));
    for(int i=m+1;i<=n;i++){
        h1.del(s[i-m]-'a');
        h2.del(s[i-m]-'a');
        h1.update(s[i]-'a');
        h2.update(s[i]-'a');
        node[++tot]=(Node(h1.sta,h2.sta));
    }
    sort(node+1,node+1+tot);
    for(int i=2;i<=tot;i++){
        if(node[i].x!=node[i-1].x||node[i].y!=node[i-1].y) ans++;
    }
    cout<<ans;
    return 0;
}

 

相关文章:

  • 2021-08-24
  • 2021-08-12
  • 2021-07-23
  • 2021-10-20
  • 2022-01-15
  • 2021-06-28
  • 2021-10-27
  • 2022-01-02
猜你喜欢
  • 2022-01-09
  • 2021-09-13
  • 2021-06-08
  • 2021-04-26
  • 2021-06-06
  • 2021-10-28
  • 2022-01-07
相关资源
相似解决方案