3676: [Apio2014]回文串

3676: [Apio2014]回文串

Time Limit: 20 Sec Memory Limit: 128 MB
Submit: 1740 Solved: 744
[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

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

在第一个样例中,回文子串有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。

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<algorithm>
#include<queue>
using namespace std;
#define LL long long
int read()
{
	int s=0,f=1;char ch=getchar();
	while(!('0'<=ch&&ch<='9')){if(ch=='-')f=-1;ch=getchar();}
	while('0'<=ch&&ch<='9'){s=(s<<3)+(s<<1)+ch-'0';ch=getchar();}
	return s*f;
}
int n;
char z[300005];
int fa[300005],l[300005],las,np,ch[300005][26];
LL siz[300005],ans;
int main()
{
	//freopen(".in","r",stdin);
	//freopen(".out","w",stdout);
	scanf("%s",z+1);
	n=strlen(z+1);
	np++;fa[0]=fa[1]=1;l[1]=-1;
	for(int i=1;i<=n;i++)
	   {int &x=las;
	    while(z[i-l[x]-1]!=z[i])x=fa[x];
	    if(!ch[x][z[i]-'a'])
	       {int to=++np,k=fa[x];//cout<<k<<" ";
	        l[to]=l[x]+2;
	        while(z[i-l[k]-1]!=z[i])k=fa[k];
			fa[to]=ch[k][z[i]-'a'];ch[x][z[i]-'a']=to;
		   }
		x=ch[x][z[i]-'a'];
		siz[x]++;
	   }
	for(int i=np;i;i--)
	   {siz[fa[i]]+=siz[i];
	    ans=max(ans,siz[i]*l[i]);
	   }
	printf("%lld\n",ans);
	//fclose(stdin);
	//fclose(stdout);
	return 0;
}


posted on 2016-06-14 10:23  wuyuhan  阅读(222)  评论(0编辑  收藏  举报

导航