2019 Xuzhou Online Contest Problem G Colorful String

1654 단어 데이터 구조
회문수
#include
using namespace std;
typedef long long ll;
const int N=3e5+10;
int NUM[N][30];
int check(int l,int r){
    int ans=0;
    for(int i=1;i<=26;++i)if(NUM[r][i]>NUM[l-1][i])++ans;
    return ans;
}
int ans[N];
struct Palindromic_Automaton {
    int ch[N][26], f[N], len[N], s[N], ok[N];
    int cnt[N]; //                 (  count() )
    int num[N]; //                            
    int last, sz, n;
    int newnode(int x) {
        memset(ch[sz], 0, sizeof(ch[sz]));
        cnt[sz] = num[sz] = 0, len[sz] = x;
        return sz++;
    }
    void init() {
        sz = 0; newnode(0), newnode(-1); last = n = 0, s[0] = -1, f[0] = 1;
    }
    int get(int u) {for (; s[n - len[u] - 1] != s[n]; u = f[u]); return u;}
    void add(int c) {
        s[++n] = c;
        int u = get(last);
        if (!ch[u][c]) {
            int np = newnode(len[u] + 2);
            f[np] = ch[get(f[u])][c];
            num[np] = num[f[np]] + 1;
            ch[u][c] = np;
            ok[np]=check(n-len[np]+1,n);
        }
        last = ch[u][c];
        cnt[last]++;
    }
    ll count(){
        ll ans=0;
        for (int i = sz - 1; ~i; i--) cnt[f[i]] += cnt[i];
        for(int i=sz-1;i>1;--i)ans=ans+1ll*cnt[i]*ok[i];
        return ans;
    }
}pam;
char s[N];
int main(){
    while(scanf("%s",s)!=-1){
        int n=strlen(s);
        for(int i=1;i<=n;++i){
            for(int j=1;j<=26;++j){
                if(s[i-1]-'a'+1==j)NUM[i][j]=NUM[i-1][j]+1;
                else NUM[i][j]=NUM[i-1][j];
            }
        }
        pam.init();
        for(int i=0;i

좋은 웹페이지 즐겨찾기