您的位置:首页 > 其它

[bzoj3676][Apio2014]回文串 回文自动机(回文树)

2017-07-28 15:21 471 查看

3676: [Apio2014]回文串

Time Limit: 20 Sec  Memory Limit: 128 MB

[Submit][Status][Discuss]

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

你们搞的这个回文自动机啊,亦可赛艇!

这个博客写的吼
http://blog.csdn.net/u013368721/article/details/42100363

代码也很短啊
#include<iostream>
#include<cstring>
#include<cstdio>
const int NSOI = 300000 + 5;
typedef long long ll;
ll ans; char s[NSOI];
int n,now,tot,cur,last,cnt[NSOI],fail[NSOI],ch[NSOI][26],len[NSOI];
int newnode( int x ){ len[tot] = x; return tot++; }
int getfail( int x, int n ){ while( s
!= s[n-len[x]-1] ) x = fail[x]; return x; }
ll Max( ll x, ll y ){ return x<y ? y : x; }
int main(){
gets(s+1); s[0] = -1;
newnode(0); newnode(-1); fail[0] = 1;
for( n = 1; s
; n++ ){
int x = s
-'a';
cur = getfail(last,n);
if( !ch[cur][x] ){
now = newnode(len[cur]+2);
fail[now] = ch[getfail(fail[cur],n)][x];
ch[cur][x] = now;
}
cnt[last=ch[cur][x]]++;
}
for( n = --tot; n > 1; n-- ) cnt[fail
]+=cnt
,ans=Max(ans,(ll)cnt
*(ll)len
);
printf("%lld", ans);
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: