比赛 NOIP模拟赛by mzx Day2 评测结果 AAAAAAAWWW
题目名称 拯救紫萱学姐 最终得分 70
用户昵称 可以的. 运行时间 1.048 s
代码语言 C++ 内存使用 32.74 MiB
提交时间 2016-10-20 21:47:00
显示代码纯文本
#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <deque>
#include <ctime>
#define Mem(a,v) memset(a,v,sizeof(a))
using namespace std;
/*
freopen(".in","r",stdin);
	freopen(".out","w",stdout);
getchar(); getchar();
	return 0;
*/
#define maxn 1000010

int N,Next[maxn],len,head[maxn];
char s[maxn];
struct Edge
{
	int to,next,dis;	
}e[maxn << 1];
void insert(int x,int y,int z){
	e[++len].to = y; e[len].dis = z;
	e[len].next = head[x]; head[x] = len;	
}
void Get_Next(){
	Next[0] = Next[1] = 0;
	int j = 0;
	for(int i = 1;i < N;i ++){
		while(j && s[i]!=s[j]) j = Next[j];	
		if(s[i] == s[j]) j ++ ;
		Next[i+1] = j;
	}	
	/*
	for(int i=1;i<=N;i++){
		printf("%d",Next[i]);	
	}*/
}
int DIS(int s,int t){
	return ((s - t)*(s - t));
}
void Build_Edge(){
	for(int i=1;i<=N;i++){
		int to = Next[i];
		insert(i,to,DIS(i,to));
		insert(to,i,DIS(to,i));	
		//printf("i = %d to = %d  dis = %d\n",i,to,DIS(i,to));
	}
}
bool vis[maxn];
int ans1,Max1,Max,ans;
void Dfs(int x,int dis){
	vis[x] = 1;
	for(int i = head[x]; i ; i = e[i].next){
		int p = e[i].to;
		if(!vis[p]){
			int tdis = dis + e[i].dis;
			if(tdis > Max){
				Max = tdis; ans = p;
			}	
			Dfs(p,tdis);
		}
	}
}
int main(){
	freopen("savemzx.in","r",stdin);
	freopen("savemzx.out","w",stdout);
	
	int __size__=128<<20;
	char *__p__=(char*)malloc(__size__)+__size__;
	__asm__("movl %0, %%esp\n"::"r"(__p__));
	
	scanf("%s",s);
	N = strlen(s);
	Get_Next();	
	Build_Edge();
	Dfs(0,0);
	ans1 = ans; Max1 = Max;
	ans = 0; Max = 0;
	Mem(vis,0);
	Dfs(ans1,0);
	printf("%d\n",max(Max,Max1));
	
	//printf("clock = %d\n",clock());
	getchar(); getchar();
	return 0;
}