回文自动机T了一个点,WA了三个点,求助

P4287 [SHOI2011] 双倍回文

3493441984zz @ 2018-12-08 16:42:38

代码:

#include<iostream>
#include<cstdio>
#include<cstring>
#include<string>
#define N 500010
using namespace std;
char in[N];
int fail[N],len[N],ch[N][26];
int n,cnt=1,last,ans;
bool Search(int x,int nx)
{
    int now=0,first=x-(len[nx]>>2)+1;
    for(int i=first;i<=x;i++)
    {
        if(!ch[now][in[i]-'a'])
            return 0;
        now=ch[now][in[i]-'a'];
    }
    return 1;
}
int main()
{
    scanf("%d",&n);
    scanf("%s",in+1);
    in[0]='&';
    fail[0]=1;len[1]=-1;
    for(int i=1;i<=n;i++)
    {
        int j;
        while(in[i-len[last]-1]!=in[i])
            last=fail[last];
        if(!ch[last][in[i]-'a'])
        {
            len[++cnt]=len[last]+2;
            j=fail[last];
            while(in[i-len[j]-1]!=in[i])
                j=fail[j];
            fail[cnt]=ch[j][in[i]-'a'];
            ch[last][in[i]-'a']=cnt;
            if(len[cnt]%4==0&&Search(i,cnt))
                ans=max(ans,len[cnt]);
        }
        last=ch[last][in[i]-'a'];
    }
    printf("%d",ans);
    return 0;
}

by Tank1014 @ 2018-12-08 19:02:10

前排

看不出问题


|