比赛 NOIP模拟赛by mzx Day2 评测结果 AAAAAAAAAA
题目名称 拯救紫萱学姐 最终得分 100
用户昵称 Cydiater 运行时间 0.950 s
代码语言 C++ 内存使用 47.04 MiB
提交时间 2016-10-20 20:09:27
显示代码纯文本
//B
//by Cydiater
//2016.10.20
#include <iostream>
#include <cstdio>
#include <cstring>
#include <string>
#include <algorithm>
#include <queue>
#include <map>
#include <ctime>
#include <cmath>
#include <cstdlib>
#include <iomanip>
using namespace std;
#define ll long long
#define up(i,j,n)		for(int i=j;i<=n;i++)
#define down(i,j,n)		for(int i=j;i>=n;i--)
#define FILE "savemzx"
const ll MAXN=1e6+5;
const ll oo=1LL<<60;
char s[MAXN];
ll next[MAXN],LEN,LINK[MAXN],len=0,ans=0,dist[MAXN];
struct edge{
	ll y,next,v;
}e[MAXN];
namespace solution{
	inline void insert(int x,int y,ll v){e[++len].next=LINK[x];LINK[x]=len;e[len].y=y;e[len].v=v;}
	void init(){
		scanf("%s",s+1);
		LEN=strlen(s+1);
	}
	void dfs(int node){
		ll maxdist=0;
		for(int i=LINK[node];i;i=e[i].next){
			dfs(e[i].y);
			if(dist[e[i].y]+e[i].v+maxdist>ans)ans=max(ans,maxdist+e[i].v+dist[e[i].y]);
			maxdist=max(maxdist,dist[e[i].y]+e[i].v);
		}
		dist[node]=maxdist;
	}
	void slove(){
		next[1]=0;ll j=0;
		up(i,2,LEN){
			while(j!=0&&s[j+1]!=s[i])j=next[j];
			if(s[j+1]==s[i])j++;
			next[i]=j;
		}
		up(i,1,LEN){
			ll v=i-next[i];v*=v;
			insert(next[i],i,v);
		}
		memset(dist,0,sizeof(dist));
		dfs(0);
	}
	void output(){
		cout<<ans<<endl;
	}
}
int main(){
	freopen(FILE".in","r",stdin);
	freopen(FILE".out","w",stdout);
	using namespace solution;
	init();
	slove();
	output();
	return 0;
}