比赛 NOIP模拟赛by mzx Day2 评测结果 WWAAAAWAAA
题目名称 拯救紫萱学姐 最终得分 70
用户昵称 Sky_miner 运行时间 1.779 s
代码语言 C++ 内存使用 78.52 MiB
提交时间 2016-10-20 20:40:13
显示代码纯文本
#include <queue>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
typedef long long ll;
inline void read(ll &x){
	x=0;char ch;bool flag = false;
	while(ch=getchar(),ch<'!');if(ch == '-') ch=getchar(),flag = true;
	while(x=10*x+ch-'0',ch=getchar(),ch>'!');if(flag) x=-x;
}
inline ll cat_max(const ll &a,const ll &b){return a>b ? a:b;}
inline ll cat_min(const ll &a,const ll &b){return a<b ? a:b;}
const ll maxn = 1000005;
const ll maxm = maxn;
struct Edge{
	ll to,next,dis;
}G[maxm<<1];
ll head[maxn],cnt;
void add(ll u,ll v,ll d){
	G[++cnt].to = v;
	G[cnt].next = head[u];
	G[cnt].dis = d;
	head[u] = cnt;
}
char str[maxn];ll nxt[maxn];
void getnext(){
	ll j = 0;
	nxt[0] = nxt[1] = 0;
	for(ll i = 1;str[i] != '\0'; ++i){
		while(j != 0 && str[i] != str[j] ) j = nxt[j];
		if(str[i] == str[j]) ++j;
		nxt[i+1] = j;
	}return;
}
ll T,q[maxn],l,r,n,dis[maxn];
bool vis[maxn];
#define v G[i].to
ll bfs(){
	memset(vis,0,sizeof vis);
	vis[0] = true;
	l = 0;r = -1;
	q[++r] = dis[0] = 0;
	while(l <= r){
		ll x = q[l++];
		for(ll i = head[x];i;i = G[i].next){
			if(vis[v]) continue;
			vis[v] = true;
			q[++r] = v;
			dis[v] = dis[x] + G[i].dis;
		}
		
	}ll pos = 0;
	for(ll i=1;i<=n;++i) if(dis[i] > dis[pos])pos=i;
	return pos;
}
ll spfa(ll s){
	memset(vis,0,sizeof vis);
	l = 0;r = -1;q[++r] = s;dis[s] = 0;
	while(l <= r){
		ll x = q[l++];
		for(ll i = head[x];i;i = G[i].next){
			if(vis[v]) continue;
			vis[v] = true;
			q[++r] = v;
			dis[v] = dis[x] + G[i].dis;
		}
	}ll pos = 0;
	for(ll i=0;i<=n;++i) if(dis[i] > pos) pos = dis[i];
	return pos;
}
#undef v
int main(){
	freopen("savemzx.in","r",stdin);
	freopen("savemzx.out","w",stdout);
	scanf("%s",str);
	n = strlen(str);
	getnext();
	for(ll i=1;i<=n;++i){
		add(nxt[i],i,(i-nxt[i])*(i-nxt[i]));
		add(i,nxt[i],(i-nxt[i])*(i-nxt[i]));
	}
	printf("%lld\n",spfa(bfs()));
	fclose(stdin);fclose(stdout);
	return 0;
}