【NOIP2017提高A组冲刺11.8】好文章

#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;
}

 

转载于:https://www.cnblogs.com/ghostcai/p/9502780.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值