#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#define N 100010
#define M 262144
#define pi acos(-1)
#define mod 1000000007
using namespace std;
typedef long long ll;
int n,m,L;
char s[N];
char ss[N<<1];
struct complex
{
double r,i;
complex(double x=0.0,double y=0.0){r=x,i=y;}
complex operator + (const complex a)
{return complex(r+a.r,i+a.i);}
complex operator - (const complex a)
{return complex(r-a.r,i-a.i);}
complex operator * (const complex a)
{return complex(r*a.r-i*a.i,r*a.i+i*a.r);}
}a[M],b[M];
int rev[M];
void FFT(complex *a,int f)
{
for(int i=0;i<n;i++)if(i<rev[i])swap(a[i],a[rev[i]]);
for(int h=2;h<=n;h<<=1)
{
complex wn(cos(2*pi*f/h),sin(2*pi*f/h));
for(int i=0;i<n;i+=h)
{
complex w(1,0);
for(int j=0;j<(h>>1);j++,w=w*wn)
{
complex t=w*a[i+j+(h>>1)];
a[i+j+(h>>1)]=a[i+j]-t;
a[i+j]=a[i+j]+t;
}
}
}
if(f==-1)for(int i=0;i<n;i++)a[i].r/=n;
}
int p[N<<1];
int powtwo[N<<1];
void pre()
{
n=strlen(s+1);
ss[0]=‘&‘,ss[1]=‘^‘;
for(int i=1;i<=n;i++)
{
ss[i<<1]=s[i];
ss[i<<1|1]=‘^‘;
}
n++,n<<=1;
}
void manacher()
{
int ret=0,mx=0,id=0;
for(int i=1;i<n;i++)
{
if(mx>i)p[i]=min(p[2*id-i],mx-i);
else p[i]=1;
while(ss[i-p[i]]==ss[i+p[i]])p[i]++;
if(mx<p[i]+i)mx=p[i]+i,id=i;
}
}
void init()
{
powtwo[0]=1;
for(int i=1;i<=2*n;i++)
powtwo[i]=(powtwo[i-1]<<1)%mod;
}
int ans;
int f[N<<1];
int main()
{
scanf("%s",s+1);
n=strlen(s+1);
n--;
int nn=n;
init();
for(int i=0;i<=n;i++)
{
a[i].r=(s[i+1]==‘a‘)?1:0;
b[i].r=a[i].r;
a[i].i=b[i].i=0;
}
m=2*n,L=0;
for(n=1;n<=m;n<<=1)L++;
for(int i=0;i<n;i++)rev[i]=(rev[i>>1]>>1)|((i&1)<<(L-1));
FFT(a,1),FFT(b,1);
for(int i=0;i<=n;i++)a[i]=a[i]*b[i];
FFT(a,-1);
for(int i=0;i<=m;i++)
{
int tmp=(int)(a[i].r+0.1);
f[i]=f[i]+tmp;
}
m=2*nn,L=0;
for(n=1;n<=m;n<<=1)L++;
for(int i=0;i<n;i++)rev[i]=(rev[i>>1]>>1)|((i&1)<<(L-1));
memset(a,0,sizeof(a));
memset(b,0,sizeof(b));
for(int i=0;i<=nn;i++)
{
a[i].r=(s[i+1]==‘b‘)?1:0;
b[i].r=a[i].r;
a[i].i=b[i].i=0;
}
FFT(a,1),FFT(b,1);
for(int i=0;i<=n;i++)a[i]=a[i]*b[i];
FFT(a,-1);
for(int i=0;i<=m;i++)
{
int tmp=(int)(a[i].r+0.1);
f[i]=f[i]+tmp;
}
for(int i=0;i<=m;i++)
{
int tmp=(f[i]+1)/2;
if(tmp>=0)
ans=(((ans+powtwo[tmp])%mod-1)%mod+mod)%mod;
}
pre();
manacher();
for(int i=0;i<=n;i++)ans=((ans-p[i]/2)%mod+mod)%mod;
printf("%d\n",ans);
}
版权声明:本文为博主原创文章,未经博主允许不得转载。
原文地址:http://blog.csdn.net/wzq_qwq/article/details/48173545