记录编号 326309 评测结果 AAAAAAAAAA
题目名称 拯救紫萱学姐 最终得分 100
用户昵称 GravatarAntiLeaf 是否通过 通过
代码语言 C++ 运行时间 2.306 s
提交时间 2016-10-21 07:18:58 内存使用 125.58 MiB
显示代码纯文本
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1000010<<1;
struct edge{int to,prev;long long w;}e[maxn<<1];
void addedge(int,int,long long);
void init();
void bfs();
int LCA(int,int);
int prt[maxn],size[maxn]={0},son[maxn]={0},depth[maxn]={0},top[maxn];
int n,k,last[maxn]={0},len=0,fail[maxn],q[maxn],head,tail,u,v;
long long ans=0,tmp=0,d[maxn]={0};
char s[maxn];
int main(){
#define MINE
#ifdef MINE
	freopen("savemzx.in","r",stdin);
	freopen("savemzx.out","w",stdout);
#endif	
	scanf("%s",s);
	n=strlen(s);
	init();
	bfs();
	v=1;
	for(int i=2;i<=n+1;i++)if(d[v]+d[i]-(d[LCA(v,i)]<<1)>tmp){
		u=i;
		tmp=d[v]+d[i]-(d[LCA(v,i)]<<1);
	}
	for(v=1;v<=n+1;v++)ans=max(ans,d[u]+d[v]-(d[LCA(u,v)]<<1));
	printf("%lld",ans);
#ifndef MINE
	printf("\n-------------------------DONE-------------------------\n");
	for(;;);
#endif
	return 0;
}
void addedge(int x,int y,long long z){
	e[++len].to=y;
	e[len].w=z;
	e[len].prev=last[x];
	last[x]=len;
}
void init(){
	memset(fail,0,sizeof(fail));
	for(int i=1;i<n;i++){
		int j=fail[i];
		while(j&&s[i]!=s[j])j=fail[j];
		fail[i+1]=j+(s[i]==s[j]);
	}
	//for(int i=0;i<n;i++)printf("%d ",fail[i]);printf("\n");
	for(int i=1;i<=n;i++){
		prt[i]=(fail[i]?fail[i]:n+1);
		addedge(prt[i],i,(long long)(i-fail[i])*(i-fail[i]));
	}
}
void bfs(){
	int x;
	head=tail=1;
	q[tail++]=n+1;
	while(head!=tail){
		x=q[head++];
		for(int i=last[x];i;i=e[i].prev){
			d[e[i].to]=d[x]+e[i].w;
			depth[e[i].to]=depth[x]+1;
			q[tail++]=e[i].to;
		}
	}
	for(int i=n+1;i;i--){
		x=q[i];
		size[prt[x]]+=size[x];
		if(size[x]>size[son[prt[x]]])son[prt[x]]=x;
	}
	for(int i=1;i<=n+1;i++){
		x=q[i];
		if(x==son[prt[x]])top[x]=top[prt[x]];
		else top[x]=x;
	}
}
int LCA(int x,int y){
	while(top[x]!=top[y]){
		if(depth[top[x]]<depth[top[y]])swap(x,y);
		x=prt[top[x]];
	}
	if(depth[x]>depth[y])swap(x,y);
	return x;
}
/*
abcab
Answer:
23
*/