「HDU6599 I Love Palindrome String」 - 回文自动机

HDU 6599 I Love Palindrome String

tags:回文自动机

题意

让你求有多少个 \([l,r]\) 满足 \(s[l,r]\)\(s\left[l,\frac{l+r}2\right]\) 都是回文串

题解

考虑本质不同的回文串总共有 \(O(n)\) 种,然后统计每个满足条件的回文串出现次数和

这不是回文自动机板子题吗

#include<cstdio>
#include<cstring>
#include<algorithm>
const int N=300005,P=31;
typedef unsigned long long ull;
int n,ans[N],nd[N];char s[N];ull ha[N],ha2[N],pw[N];
ull get(int l,int r){
    return ha[r]-ha[l-1]*pw[r-l+1];
}
ull get2(int l,int r){
    return ha2[l]-ha2[r+1]*pw[r-l+1];
}
bool chk(int l,int r){
    return get(l,r)==get2(l,r)&&get(l,(l+r)>>1)==get2(l,(l+r)>>1);
}
struct PAM{
    int lst,cnt,len[N],fa[N],ch[N][26],sz[N],cur[N],ok[N];
    int newnode(int a,int b){return len[cnt]=a,fa[cnt]=b,cnt++;}
    void clear(){
        for(int i=0;i<cnt;++i)len[i]=fa[i]=sz[i]=0,memset(ch[i],0,sizeof(ch[i]));
        cnt=0,newnode(0,1),newnode(-1,0);
    }
    int get(int p,int n){for(;s[n-len[p]-1]!=s[n];p=fa[p]);return p;}
    int extend(int c,int pos){
        int p=get(lst,pos);
        if(!ch[p][c]){
            ch[p][c]=newnode(len[p]+2,ch[get(fa[p],pos)][c]);
            ok[ch[p][c]]=chk(pos-len[ch[p][c]]+1,pos);
        }
        ++sz[lst=ch[p][c]];
        return lst;
    }
    void calc(){
        for(int i=cnt-1;i>=2;--i)sz[fa[i]]+=sz[i],cur[i]=0;
        for(int i=cnt-1;i>=2;--i){
            ans[len[i]]+=ok[i]*sz[i];
        }
    }
}pam;
int main(){
    pw[0]=1;for(int i=1;i<N;++i)pw[i]=pw[i-1]*P;
    while(scanf("%s",s+1)!=EOF){
        n=strlen(s+1);pam.clear();
        for(int i=1;i<=n;++i)ha[i]=ha[i-1]*P+s[i]-'a'+1;
        ha2[n+1]=0;
        for(int i=n;i>=1;--i)ha2[i]=ha2[i+1]*P+s[i]-'a'+1;
        for(int i=1;i<=n;++i)nd[i]=pam.extend(s[i]-'a',i),ans[i]=0;
        pam.calc();
        for(int i=1;i<=n;++i)printf("%d%c",ans[i],i==n?'\n':' ');
    }
    return 0;
}

转载于:https://www.cnblogs.com/xay5421/p/HDU6599.html

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值