[BZOJ 3676][APIO2014]回文串

会问自动机初识,放个板子

/**************************************************************
    Problem: 3676
    User: MiEcoku
    Language: C++
    Result: Accepted
    Time:368 ms
    Memory:36740 kb
****************************************************************/
 
#include <bits/stdc++.h>
using namespace std;
#define fp(i, a, b) for ( int i = a; i <= b; ++ i)
const int maxn = 300010;
long long ans = 1LL;
namespace SPM {
    int ch[26][maxn], cnt[maxn], link[maxn], len[maxn], S[maxn];
    int P, lst, n;
    void init() {
        len[1] = -1; link[1] = 0; link[0] = 1; S[n = 0] = -1; P = lst = 1;
    }
    int fail(int x) {
        for ( ; S[n-len[x]-1] != S[n]; x = link[x]); return x;
    }
    void insert(int x) {
        S[++ n] = x; lst = fail(lst);
        if( !ch[x][lst]) {
            int nw = ++ P; len[P] = len[lst] + 2;
            link[nw] = ch[x][fail(link[lst])];
            ch[x][lst] = nw;
        } lst = ch[x][lst]; ++ cnt[lst];
    }
    void count() {
        for ( int i = P; ~i; -- i) {
            cnt[link[i]] += cnt[i]; ans = max(ans, 1LL * len[i] * cnt[i]);
        }
    }
}
char s[maxn];
int n;
int main() {
    SPM :: init();
    scanf("%s", s+1); n = strlen(s+1); fp(i, 1, n) SPM :: insert(s[i]-'a');
    SPM :: count(); printf("%lld\n", ans);
}
View Code

 

猜你喜欢

转载自www.cnblogs.com/miecoku/p/9757336.html