Description

Input

Output

Sample Input
abcdcba
abcdef

Sample Output
0
2
5

HINT

Source

manacher加上线段区间覆盖问题。

#include <cstdio>
#include <algorithm>
#include <cstring>
#include <iostream>

const int maxn=50000;

struct data
{
int x,y;

bool operator <(const data &other) const
{
return x<other.x;
}
};

data d[maxn+10];
char s[maxn+10],a[(maxn<<1)+10];
int p[(maxn<<1)+10],id,rmax,len;

int solve()
{
len=strlen(s+1);
a[0]='!';
a[1]='$'; for(register int i=1; i<=len; ++i) { a[i<<1]=s[i]; a[i<<1|1]='$';
}
len=len<<1|1;
a[len+1]='*';
memset(p,0,sizeof p);
id=1;
p[1]=1;
rmax=1;
for(register int i=2; i<=len; ++i)
{
if(i>rmax)
{
p[i]=1;
}
else
{
if(p[(id<<1)-i]<rmax-i)
{
p[i]=p[(id<<1)-i];
}
else
{
p[i]=rmax-i;
}
}
while(a[i+p[i]]==a[i-p[i]])
{
++p[i];
}
if(i+p[i]-1>rmax)
{
rmax=i+p[i]-1;
id=i;
}
}
for(register int i=1; i<=len; ++i)
{
d[i].x=i-p[i]+1;
d[i].y=i+p[i]-1;
}
std::sort(d+1,d+len+1);
int now=1,pre=0,ans=0;
while(now<=len)
{
int maxy=0;
while((now<=len)&&(d[now].x-1<=pre))
{
if(d[now].y>maxy)
{
maxy=d[now].y;
}
++now;
}
if(now<=len)
{
++ans;
}
pre=maxy;
}
printf("%d\n",ans-1);
return 0;
}

int main()
{
while(scanf("%s",s+1)!=EOF)
{
solve();
}
return 0;
}