Answer = number of Palindrome sub-series-palindrome substring number.
The number of palindrome substrings can be calculated by Manacher in $o (n) $ time.
For the number of palindrome subsequence, F[i] is the logarithm of the character with the symmetry center of I, then its contribution to the answer is $2^{f[i]}-1$.
For two numbers $1\leq I\leq j\leq n$, if A[I]==A[J], then they have 1 contributions to f[i+j].
So you can calculate the contribution of the match by enumerating the current character and then using the FFT in $o (n\log N) $ time.
#include <cstdio> #include <cmath> #include <cstring> #include <algorithm>using namespace std; const int N=262144,p=1000000007;char a[n],s[n];int n,m,k,i,r,p,f[n],two[n],ans;struct comp{double r,i;comp (double _r= 0,double _i=0) {r=_r;i=_i;} Comp operator+ (const comp x) {return comp (r+x.r,i+x.i);} Comp operator-(const comp x) {return comp (r-x.r,i-x.i);} Comp operator* (const comp x) {return comp (R*X.R-I*X.I,R*X.I+I*X.R);}} B[n],c[n];const double Pi=acos ( -1.0); void FFT (comp a[],int n,int t) {for (int. i=1,j=0;i<n-1;i++) {for (int S=N;J^=S&G) t;>=1,~j&s;); if (i<j) swap (a[i],a[j]); } for (int d=0; (1<<d) <n;d++) {int m=1<<d,m2=m<<1; Double O=pi/m*t;comp _w (cos (o), sin (o)); for (int i=0;i<n;i+=m2) {Comp W (1,0); for (int j=0;j<m;j++) {Comp &A=a[i+j+m],&B=a[i+j],t=w*A; A=b-t; B=b+t;w=w*_w; }}} if (T==-1) for (int i=0;i<n;i++) a[i].r/=n;} int main () {scanf ("%s", a+1), N=std::strlen (a+1); for (TWo[0]=i=1;i<=n;i++) two[i]= (two[i-1]<<1)%P; for (i=1;i<=n;i++) s[i<<1]=a[i],s[i<<1|1]= ' # '; s[0]= ' $ ', s[1]= ' # ', s[m= (n+1) <<1]= ' @ '; for (f[1]=1,i=2;i<m;i++) {for (F[i]=r>i?min (R-i,f[p*2-i]): 1;s[i-f[i]]==s[i+f[i]];f[i]++); if (i+f[i]>r) r=i+f[i],p=i; Ans= (ans-f[i]/2+p)%P; } for (k=1;k<=n;k<<=1);k<<=1; for (i=1;i<=n;i++) {if (a[i]== ' a ') b[i].r=1; if (a[i]== ' B ') c[i].r=1; } FFT (b,k,1); FFT (c,k,1); for (i=0;i<k;i++) b[i]=b[i]*b[i]+c[i]*c[i]; FFT (b,k,-1); for (i=0;i<k;i++) ans= (ans+two[((int) (b[i].r+0.5) +1)/2]-1)%P; Return printf ("%d", ans), 0;}
BZOJ3160: million-path human tracer