2019 Xuzhou network game G. Colorful String [] palindrome tree

Portal

The meaning of problems

Several different definition of a string of characters of the string value, a string of all seeking palindromic substrings values.

answer

Palindrome automaton board considered the question, but unfortunately time did not do before school, constructed directly palindrome tree, dfs statistics on the line.
In order to write this is about perfect palindrome automatic machine template, templates before writing that does not work well.

Code

#include <iostream>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
using namespace std;
typedef long long LL;
const int N=3e5+10;
char s[N];
int n;
LL ans;
struct PAM{
    int tr[N][26],fail[N],len[N],cnt[N],tot,last,n;
    int getfail(int x){
        while(s[n]!=s[n-len[x]-1]) x=fail[x];
        return x;
    }
    void init(char *s){
        n=tot=last=0;
        cnt[0]=len[0]=cnt[1]=0;
        memset(tr[0],0,sizeof(tr[0]));
        memset(tr[1],0,sizeof(tr[1]));
        fail[0]=1;
        len[++tot]=-1;
        for(n=1;s[n]!='\0';n++){
            int c=s[n]-'a',cur=getfail(last);
            if(!tr[cur][c]){
                len[++tot]=len[cur]+2;cnt[tot]=0;
                memset(tr[tot],0,sizeof(tr[tot]));
                fail[tot]=tr[getfail(fail[cur])][c];
                tr[cur][c]=tot;
            }
            last=tr[cur][c];
            cnt[last]++;
        }
        for(int i=tot;i>=0;i--) cnt[fail[i]]+=cnt[i];
    }
    int count,vis[26];
    void dfs(int u){
        ans+=count*cnt[u];
        for(int i=0;i<26;i++){
            if(!tr[u][i]) continue;
            vis[i]++;if(vis[i]==1) count++;
            dfs(tr[u][i]);
            vis[i]--;if(!vis[i]) count--;
        }
    }
}pam;


int main(){
    scanf("%s",s+1);
    pam.init(s);
    pam.dfs(0);
    pam.dfs(1);
    cout<<ans<<endl;
    return 0;
}

Guess you like

Origin www.cnblogs.com/BakaCirno/p/12529732.html