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

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

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

这个博客写的吼
代码也很短啊
#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[n] != 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]; n++ ){
		int x = s[n]-'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[n]]+=cnt[n],ans=Max(ans,(ll)cnt[n]*(ll)len[n]);
	printf("%lld", ans);
	return 0;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值