BZOJ3676【回文自动机】

/* I will wait for you */

#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<ctime>
#include<algorithm>
#include<iostream>
#include<fstream>
#include<vector>
#include<queue>
#include<deque>
#include<set>
#include<map>
#include<string>
#define make make_pair
#define fi first
#define se second

using namespace std;

typedef long long LL;
typedef unsigned long long ULL;
typedef pair<int,int> PII;

const int maxn=500010;
const int maxm=1010;
const int maxs=26;
const int INF=1<<29;
const int P=1000000007;
const double error=1e-9;

inline int read()
{
	int x=0,f=1;char ch=getchar();
	while(ch<'0'||ch>'9') f=(ch=='-'?-1:1),ch=getchar();
	while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar();
	return x*f;
}

struct pam
{
	pam *fa,*next[maxs];int val,len;
}su[maxn],*headf,*heads,*last;

char s[maxn];int n,pos,cnt;LL ans;

void init()
{
	headf=&su[++cnt],last=heads=&su[++cnt];
	heads->len=-1,headf->fa=heads;
}

void add(int x)
{
	pam *per=last;
	for(pos++;s[pos-per->len-1]!=s[pos];per=per->fa);
	
	if(!per->next[x])
	{
		pam *now=&su[++cnt];
		now->val=1,now->len=per->len+2;
		last=per->next[x]=now;
		
		if(per==heads) now->fa=headf;
		else 
		{
			for(per=per->fa;s[pos-per->len-1]!=s[pos];per=per->fa);
			now->fa=per->next[x];
		}
	}
	else (last=per->next[x])->val++;	
}

int main()
{
	scanf("%s",s+1),n=strlen(s+1),init();
	for(int i=1;i<=n;i++) add(s[i]-'a');
	for(pam* i=&su[cnt];i!=heads;i--)
		i->fa->val+=i->val,ans=max(ans,(LL)i->len*i->val);
		
	printf("%lld\n",ans);
	return 0;
}

你可能感兴趣的:(BZOJ3676【回文自动机】)