# 【BZOJ3160】万径人踪灭（FFT，Manacher）

BZOJ

## 题解

$s[x-i]=s[x+i]$

$(x-i)+(x+i)=2x$

$Manacher$

#include<iostream>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<algorithm>
#include<set>
#include<map>
#include<vector>
#include<queue>
#include<complex>
using namespace std;
#define ll long long
#define RG register
#define MAX 500000
#define MOD 1000000007
const double Pi=acos(-1);
{
RG int x=0,t=1;RG char ch=getchar();
while((ch<'0'||ch>'9')&&ch!='-')ch=getchar();
if(ch=='-')t=-1,ch=getchar();
while(ch<='9'&&ch>='0')x=x*10+ch-48,ch=getchar();
return x*t;
}
int len;
char s[MAX];
int p[MAX],N,M;
ll Manacher()
{
s[len+len+1]='#';s[0]='-';
for(int i=len;i;--i)
{
s[i*2]=s[i];
s[i*2-1]='#';
}
int mx=0,id=0;
for(int i=1;i<=len+len;++i)
{
p[i]=mx>i?min(p[id*2-i],mx-i):1;
while(s[i+p[i]]==s[i-p[i]])++p[i];
if(i+p[i]>mx)id=i,mx=p[i]+i;
}
ll ret=0;
for(int i=1;i<=len+len;++i)ret=(ret+p[i]/2)%MOD;
return ret;
}
int r[MAX],l;
complex<double> a[MAX],b[MAX];
ll f[MAX],tw[MAX],ans=0;
void FFT(complex<double> *P,int opt)
{
for(int i=0;i<N;++i)if(i<r[i])swap(P[i],P[r[i]]);
for(int i=1;i<N;i<<=1)
{
complex<double> W(cos(Pi/i),opt*sin(Pi/i));
for(int p=i<<1,j=0;j<N;j+=p)
{
complex<double> w(1,0);
for(int k=0;k<i;++k,w*=W)
{
complex<double> X=P[j+k],Y=P[i+j+k]*w;;
P[j+k]=X+Y;P[i+j+k]=X-Y;
}
}
}
}
void Work(char cc)
{
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(int i=1;i<=len;++i)a[i]=b[i]=s[i]==cc;
FFT(a,1);FFT(b,1);
for(int i=0;i<N;++i)a[i]*=b[i];
FFT(a,-1);
for(int i=1;i<N;++i)a[i].real()=a[i].real()/N+0.5;
for(int i=0;i<=M;++i)f[i]+=((int)(a[i].real())+1)/2;
}
int main()
{
scanf("%s",s+1);
len=strlen(s+1);
M=len+len;
tw[0]=1;
for(int i=1;i<=M;++i)tw[i]=(tw[i-1]+tw[i-1])%MOD;
for(N=1;N<=M;N<<=1)++l;
for(int i=0;i<N;++i)r[i]=(r[i>>1]>>1)|((i&1)<<(l-1));
Work('a');Work('b');
for(int i=1;i<=M;++i)ans=(ans+tw[f[i]]-1)%MOD;
ans=(ans-Manacher()+MOD)%MOD;
printf("%lld\n",ans);
return 0;
}


posted @ 2018-02-09 14:53  小蒟蒻yyb  阅读(487)  评论(0编辑  收藏  举报