字符串题目

 本质不同回文串个数   tot-1;

#include<bits/stdc++.h>
#define ll long long
#define LL long long
using namespace std;
const int maxn=2e5+10;
const int num=26;
char ss[maxn];
int fail[maxn],cnt[maxn],len[maxn],ch[maxn][num];
struct PAM{
    int last,tot;
    ll ans;
    void inint(){
        last=0;  tot=0;
        fail[0]=fail[1]=1;
        len[0]=0; len[1]=-1; tot++;
    }
    int get_fail(int p,int pos){
         while(ss[pos-len[p]-1]!=ss[pos]) p=fail[p];
         return p;
    }
    void add(int x,int pos){
        int p=get_fail(last,pos);
        if(!ch[p][x]){
            len[++tot]=len[p]+2;
            fail[tot]=ch[get_fail(fail[p],pos)][x];
            ch[p][x]=tot;
        }
        cnt[last=ch[p][x]]++;
    }
    void count(){  //  bu zhi bu tong hui wen ge shu
        for(int i=tot;i>=0;i--){
            cnt[fail[i]]+=cnt[i];
            cnt[fail[i]]%=51123987;
        }
    }
    ll count_tot(){
        ll w=0; count();
        for(int i=2;i<=tot;i++) w+=cnt[i];
        return w;
    }
    void clear(){
        for(int i=0;i<=tot;i++){
            fail[i]=0; len[i]=0; cnt[i]=0;
            for(int j=0;j<num;j++) ch[i][j]=0;
        }
        last=tot=ans=0;
    }
}pam;
int main(){
    //int x; scanf("%d",&x);
    scanf("%s",ss);
    int l=strlen(ss); pam.inint();
    for(int i=0;i<l;i++) {
        pam.add(ss[i]-'a',i);
        if(i!=0) printf(" ");
        printf("%d",pam.tot-1);
    }
}
View Code

猜你喜欢

转载自www.cnblogs.com/Andromeda-Galaxy/p/11517152.html