BZOJ3676[APIO2014] 回文串

原题链接:https://www.lydsy.com/JudgeOnline/problem.php?id=3676

回文串

Description

考虑一个只包含小写拉丁字母的字符串s。我们定义s的一个子串t的“出现值”为t在s中的出现次数乘以t的长度。请你求出s的所有回文子串中的最大出现值。

Input

输入只有一行,为一个只包含小写字母(a -z)的非空字符串s。

Output

输出一个整数,为逝查回文子串的最大出现值。

Sample Input
【样例输入l】

abacaba

【样例输入2]

www

Sample Output
【样例输出l】

7

【样例输出2]

4

HINT

一个串是回文的,当且仅当它从左到右读和从右到左读完全一样。

在第一个样例中,回文子串有7个:a,b,c,aba,aca,bacab,abacaba,其中:

● a出现4次,其出现值为4:1:1=4

● b出现2次,其出现值为2:1:1=2

● c出现1次,其出现值为l:1:l=l

● aba出现2次,其出现值为2:1:3=6

● aca出现1次,其出现值为1=1:3=3

●bacab出现1次,其出现值为1:1:5=5

● abacaba出现1次,其出现值为1:1:7=7

故最大回文子串出现值为7。

【数据规模与评分】

数据满足1≤字符串长度≤300000。

题解

Manacher+后缀自动机。

然而在回文自动机出来以后就被爆艹了。。。

板子板子,都是板子

代码
#include<bits/stdc++.h>
using namespace std;
const int M=3e5+5;
char ch[M];
int cot[M],son[M][26],len[M],fail[M],odd=1,even=2,tot=2,last,p,l;
void in(){scanf("%s",ch+1);}
bool go(int v,int x,int pos){return ch[pos-len[v]-1]-'a'==x;}
void add(int x,int pos)
{
    for(;!go(last,x,pos);last=fail[last]);
    if(son[last][x])last=son[last][x];
    else
    {
        p=last,son[p][x]=++tot,len[tot]=len[p]+2;
        if(p==odd)fail[tot]=even;
        else{for(p=fail[p];!go(p,x,pos);p=fail[p]);fail[tot]=son[p][x];}
        last=tot;
    }
    ++cot[last];
}
void ac()
{
    l=strlen(ch+1);
    len[odd]=-1,fail[odd]=odd,fail[even]=odd,last=odd;
    for(int i=1;i<=l;++i)add(ch[i]-'a',i);
    long long ans=0;
    for(int i=tot;i;--i)cot[fail[i]]+=cot[i],ans=max(ans,1ll*cot[i]*len[i]);
    printf("%lld",ans);
}
int main(){in();ac();}

猜你喜欢

转载自blog.csdn.net/shadypi/article/details/81088576