马拉车算法
bzoj3790
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
const int N=50000+5;
int len,pal[N*2];
char s[N],a[N*2];
struct Node{int le,ri;}qu[N*2];
bool cmp(Node a,Node b){return a.le<b.le;}
void insert(){
memset(a,0,sizeof(a));
int lena=-1;a[++lena]='+';
for(int i=0;i<len;i++)a[++lena]='#',a[++lena]=s[i];
a[++lena]='#',a[++lena]='-',len=lena-1;
}
void manacher(){
int mx=0,id;
for(int i=1;i<=len;i++){
if(mx>=i)pal[i]=min(mx-i+1,pal[2*id-i]);else pal[i]=1;
while(a[i-pal[i]]==a[i+pal[i]])++pal[i];
if(mx<i+pal[i]-1)mx=i+pal[i]-1,id=i;
}
}
int fugai(){
int ans=0,far=1;
int i=1;
for(i=1;qu[i].le<=1;i++)
if(qu[i].ri>qu[far].ri) far=i;
while(i<=len){
ans++;
int tmp=far;
for(;qu[i].le<=qu[far].ri;i++)if(qu[i].ri>qu[tmp].ri)tmp=i;
far=tmp;
}
return ans;
}
int main(){
while(scanf("%s",s)!=EOF){
len=strlen(s);
insert();
manacher();
memset(qu,0,sizeof(qu));
for(int i=1;i<=len;i++)qu[i].le=i-pal[i]+1,qu[i].ri=i+pal[i]-1;
sort(qu+1,qu+len+1,cmp);
printf("%d\n",fugai()-1);
}
return 0;
}