# Bzoj2342 [shoi2011]双倍回文

## Manacher基础题

Posted by yjjr's blog on February 17, 2018

# 题目

## Sample Input

16
ggabaabaabaaball


## Sample Output

12


N<=500000

# 分析

• y-p[y]<=x

• x+(p[x]/2)>=y

p数组预处理出之后，按照y-p[y]的大小排序

# code

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<set>
#define rep(i,a,b) for(int i=a;i<=b;i++)
#define dep(i,a,b) for(int i=a;i>=b;i--)
#define ll long long
#define mem(x,num) memset(x,num,sizeof x)
#define reg(x) for(int i=last[x];i;i=e[i].next)
using namespace std;
{
ll f=1,x=0;char ch=getchar();
while(ch<'0'||ch>'9'){if(ch=='-')f=-1;ch=getchar();}
while(ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
return x*f;
}
const int maxn=5e5+6;
char ch[maxn];int p[maxn],que[maxn<<1],n,ans;
set<int> t;
void manacher(){
int Max=0,id;
rep(i,1,n){
if(Max>=i)p[i]=min(Max-i,p[2*id-i]);else p[i]=0;
while(ch[i+p[i]+1]==ch[i-p[i]])p[i]++;
if(p[i]+i>Max)Max=p[i]+i,id=i;
}
}
inline bool cmp(int a,int b){return (a-p[a])<(b-p[b]);}
int main()
{
scanf("%s",ch+1);
ch[0]='#';
manacher();
rep(i,1,n)que[i]=i;
sort(que+1,que+n+1,cmp);
int now=1;
rep(i,1,n){
while(now<=n&&que[now]-p[que[now]]<=i)t.insert(que[now++]);
set<int>::iterator tmp=t.upper_bound(i+p[i]/2);
if(tmp!=t.begin())ans=max(ans,(*--tmp-i)*4);
}
cout<<ans<<endl;
return 0;
}