比赛 NOIP模拟赛by mzx Day2 评测结果 AAAAAAAAAA
题目名称 拯救紫萱学姐 最终得分 100
用户昵称 Marvolo 运行时间 1.902 s
代码语言 C++ 内存使用 32.74 MiB
提交时间 2016-10-20 21:47:30
显示代码纯文本
#include<cstdio>
#include<iostream>
#include<vector>
#include<cstring>
#define N 1000010
using namespace std;

int n,root;
char a[N];
bool v[N];
long long dis[N];
int next[N],fa[N],z[N];
vector <int> l[N];

inline void Ready(){
	for (int i=1;i<n;i++){
		int t=next[i-1];
		while (t&&a[t]!=a[i])	t=next[t-1];
		if (a[i]==a[t])	next[i]=t+1;
	}
}

inline void Bfs(int qd){
	int i=0,t=0;
	long long dmax=0;
	memset(z,0,sizeof(z));	memset(dis,0,sizeof(dis));	memset(v,0,sizeof(v));
	z[++z[0]]=qd;	v[qd]=1;	t=1;
	while (t<=z[0]){
		for (i=0;i<l[z[t]].size();i++)
		if (!v[l[z[t]][i]]){
			v[l[z[t]][i]]=1;	z[++z[0]]=l[z[t]][i];
			dis[l[z[t]][i]]=dis[z[t]]+(long long)(z[t]-l[z[t]][i])*(z[t]-l[z[t]][i]);
		}
	t++;
	}
	for (i=1;i<=n;i++) if (dis[i]>dmax)	dmax=dis[i],root=i;
}

inline void Work(){
	int i=0;
	long long D=0;
	for (i=0;i<n;i++)	fa[i]=next[i]+1;
	for (i=n+1;i>=2;i--)	fa[i]=fa[i-2];
	fa[1]=0;	n=n+1;
	for (i=1;i<=n;i++){
		if (fa[i]==0)	continue;
		l[i].push_back(fa[i]);	l[fa[i]].push_back(i);
	}
	Bfs(1);	Bfs(root);
	for (i=1;i<=n;i++)	if (dis[i]>D)	D=dis[i];
	cout<<D<<endl;
}

int main(){
	freopen("savemzx.in","r",stdin);
	freopen("savemzx.out","w",stdout);
	scanf("%s",&a);
	n=strlen(a);
	Ready();	Work();
	return 0;
}