比赛 |
NOIP模拟赛by mzx Day2 |
评测结果 |
AAAAAAAWWW |
题目名称 |
拯救紫萱学姐 |
最终得分 |
70 |
用户昵称 |
AntiLeaf |
运行时间 |
1.167 s |
代码语言 |
C++ |
内存使用 |
116.64 MiB |
提交时间 |
2016-10-20 19:42:05 |
显示代码纯文本
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int maxn=1000010<<1;
struct edge{int to,w,prev;}e[maxn<<1];
void addedge(int,int,int);
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],d[maxn]={0},q[maxn],head,tail,u,v,tmp=0,ans=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("%d",ans);
#ifndef MINE
printf("\n-------------------------DONE-------------------------\n");
for(;;);
#endif
return 0;
}
void addedge(int x,int y,int 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,(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
*/