Bzoj 31.6 million-path man-manacher algorithm + fast Fourier transform

Source: Internet
Author: User

Topic: Given a string consisting of ' a ' and ' B ', the number of discontinuous palindrome sequences is obtained.

First, the palindrome must be multiplying the string because of the number of discontinuous palindrome sequences so we can find out the total number of palindrome sequences, and then lose the continuous

Continuous is the palindrome string with the Manacher algorithm can be O (n) solution

It's a little hard to do in a row.

First we make f[i] represent the number of symmetric character pairs centered on I

Like s[]=$ #a #b#a so s[4]= ' B ' f[4]=2

So for each center I we have (2^f[i])-1 solution answers namely Σ[1<=i<=n*2+1] ((2^f[i))-1)

The question is how to find the f[] Array

We find that the sum of the positions of a pair of characters contributing to f[i] in the original string array must be I

For example str+1= "ABA" then the first and third characters contribute to the 4th position of the multiplied string

So obviously there's f[i]= (Σ[1<=j<=i-1]bool (str[j]==str[i-j)) +1>>1 parentheses inside is a convolution form that can be solved by FFT

First consider the contribution of ' a ' to the answer so that ' a ' =1 ' B ' = 0 to find the convolution is ' a ' contribution

Consider the contribution of ' B ' to the answer to make ' a ' =0 ' b ' = 1 to find out that convolution is the contribution of ' B '

The sum of the two contributions and +1>>1 is the f[] array before the conclusion can be solved

Classmate actually take this to the question------------and I also really launched the FFT--and also a dead-letter write--not open long long was killed 5 points tat

The brain is not good where the writing is not clear or wrong to write in the following reply to it--look at the code is OK--

#include <cmath> #include <cstdio> #include <cstring> #include <iostream> #include < algorithm> #define M 263000#define MOD 1000000007#define PI 3.141592653589793238462643using namespace Std;typedef      Long Long ll;struct complex{double A, B; Complex () {} Complex (double _,double __): A (_), B (__) {} void operator + = (const Complex &x) {A +        =X.A;    b+=x.b;      } Complex Operator-(const Complex &x) Const {return Complex (a-x.a,b-x.b);      } Complex operator * (const Complex &x) Const {return Complex (A*X.A-B*X.B,A*X.B+B*X.A);      } void operator *= (const Complex &x) {*this= (*this) *x; } Complex operator + (const Complex &x) {Complex re=*this;re+=x;return re;}} A[m],b[m];char s[m];int f[m<<1],power[m],temp[m],ans;int manacher (char str[],int n) {static char s[M<<1]; static int f[m<<1];int i,re=0;for (s[0]= ' $ ', s[1]= ' # ', i=1;i<=n;i+ +) s[i<<1]=str[i],s[i<<1|1]= ' # '; n=n<<1|1;int mx=1,id=1;for (i=1;i<=n;i++) {f[i]=min (f[id+ Id-i],mx-i); while (S[i+f[i]]==s[i-f[i]) f[i]++;if (F[I]+I&GT;MX) Mx=f[i]+i,id=i;re+=f[i]>>1,re%=mod;} return re;} void FFT (Complex x[],int n,int type) {static Complex temp[m];if (n==1) return; int i;for (i=0;i<n;i+=2) temp[i>>1] =x[i],temp[i+n>>1]=x[i+1];memcpy (x,temp,sizeof (Complex) *n); Complex *l=x,*r=x+ (n>>1); FFT (L,n>>1,type); FFT (R,n>>1,type); Complex Root (cos (type*2*pi/n), sin (type*2*pi/n)), W (1,0); for (I=0;i<n>>1;i++,w*=root) temp[i]=l[i]+w*r[i], temp[i+ (n>>1)]=l[i]-w*r[i];memcpy (x,temp,sizeof (Complex) *n);} void pretreatment () {int i;power[0]=1;for (i=1;i<m;i++) power[i]= (power[i-1]<<1)%mod;} int main () {scanf ("%s", s+1); int I,n,len=strlen (s+1); Pretreatment (); for (N=1;n<=len<<1;n<<=1), for (i=1;i<=len;i++) if (s[i]== ' a ') a[i].a=1; FFT (a,n,1), for (i=0;i<n;i++) B[i]=a[i]*a[i];memset (a,0,sizeof a), for (i=1;i<=len;i++) if (s[i]== ' B ') a[i].a=1; FFT (a,n,1); for (i=0;i<n;i++) b[i]+=a[i]*a[i]; FFT (B,n,-1), for (i=1;i<n;i++) temp[i]+=ll (b[i].a+0.5)/n;for (i=1;i<n;i++) ans+=power[temp[i]+1>>1]-1, ans%=mod;cout<< (Ans+mod-manacher (S,len))%mod<<endl;}


Bzoj 31.6 million-path man-manacher algorithm + fast Fourier transform

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.