BZOJ2565:最長雙迴文串(迴文自動機)

題面
題意:給出一個串,求出其最長子串T,使得T是兩個迴文串拼接而成。

我的做法是在建出迴文自動機時,求出每個前綴的最長迴文後綴。
然後用反串在自動機上跑,求出每個後綴的最長迴文前綴
就可以算答案了

而串在迴文自動機上跑
由於每次轉移是增加了兩個字符
不僅要判有麼有這個兒子
還要判是不是迴文

#include <iostream>
#include <fstream>
#include <algorithm>
#include <cmath>
#include <ctime>
#include <cstdio>
#include <cstdlib>
#include <cstring>

using namespace std;
#define mmst(a, b) memset(a, b, sizeof(a))
#define mmcp(a, b) memcpy(a, b, sizeof(b))

typedef long long LL;

const int N=200200;

int n,ans;
int pre[N],len[N],son[N][26],cnt,last;
int l[N],r[N];
char s[N];

void Insert(int pos)
{
    int x=last,ch=s[pos]-'a';
    while(s[pos-len[x]-1]!=s[pos])
    x=pre[x];
    if(!son[x][ch])
    {
        len[++cnt]=len[x]+2;
        int y=pre[x];
        while(s[pos-len[y]-1]!=s[pos])
        y=pre[y];
        pre[cnt]=son[y][ch];
        son[x][ch]=cnt;
    }
    x=son[x][ch];
    last=x;
    r[pos]=len[x];
}

int main()
{
    pre[0]=pre[1]=cnt=1;
    len[1]=-1;

    scanf("%s",s+1);
    n=strlen(s+1);
    for(int i=1;i<=n;i++)
    Insert(i);

    int now=1;
    for(int i=n;i>1;i--)
    {
        int ch=s[i]-'a';
        while(!son[now][ch]||s[i+len[now]+1]!=s[i])
        now=pre[now];
        now=son[now][ch];
        ans=max(ans,r[i-1]+len[now]);
    }
    cout<<ans<<endl;

    return 0;
}
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章