Fast Fourier transform (FFT): COGS 2216. Do you think it's KMP?

Source: Internet
Author: User

2216. Do you think it's KMP?

★★★☆ input file: guess.in output file: guess.out Simple comparison
Time limit: 1 s memory limit:

"Title description"

XX is playing two strings of games. First, he took out two strings of S and T,xx want to know how many times T appeared in S and where they were. Note that there may be "?" in T. Character, this character can match any character.

"Input Format"

Two lines of two strings, respectively, representing S and T

"Output Format"

The first line is a positive integer k, which indicates that T appears several times in S.

Next k-line positive integers,

Represents the start position of T each occurrence in S. According to small to large

Sequential output, S subscript starting from 0.

"Sample Input"

Ababcadaca

A?a

"Sample Output"

3

0

5

7

Prompted

For 10% of the data, the length of S and T is not more than 100

For another 20% of the data, there is no "?" in T.

For 100% of the data, s length does not exceed 10^5,t length without exceeding s. S contains only lowercase

Letters, T contains only lowercase letters and "?"

Source

Classic Topics

  

The problem is the FFT oh, cough ...

1#include <iostream>2#include <cstring>3#include <cstdio>4#include <cmath>5 using namespacestd;6 Const intmaxn=400010;7 Const DoublePi=acos (-1.0);8 CharS[MAXN],T[MAXN];9 intA[MAXN],B[MAXN];Ten structcomplex{ One     Doubler,i; AComplexDoubler_=0.0,Doublei_=0.0){ -R=r_;i=I_; -     } theComplexoperator+(Complex a) { -         returnComplex (r+a.r,i+A.I); -     } -Complexoperator-(Complex a) { +         returnComplex (r-a.r,i-A.I); -     } +Complexoperator*(Complex a) { A         returnComplex (r*a.r-i*a.i,r*a.i+i*A.R); at     } - }; - complex A[MAXN],B[MAXN],C[MAXN],D[MAXN],E[MAXN]; -  - voidRader (Complex *a,intLen) { -     intK; in      for(intI=1,j=len>>1; i<len-1; i++){ -         if(i<j) Swap (A[i],a[j]); toK=len>>1; +          while(j>=k) { -j-=K; thek>>=1; *         } $j+=K;Panax Notoginseng     }         - } the  + voidFFT (Complex *a,intLenintOn ) { A rader (a,len); the      for(intH=2; h<=len;h<<=1){ +Complex WN (cos (-on*pi*2.0/h), sin (-on*pi*2.0/h)); -          for(intj=0; j<len;j+=h) { $Complex W (1,0); $              for(intk=j;k<j+ (h>>1); k++){ -Complex u=A[k]; -Complex v=a[k+ (h>>1)]*W; thea[k]=u+v; -a[k+ (h>>1)]=u-v;Wuyiw=w*WN; the             } -         } Wu     } -     if(on==-1) About          for(intI=0; i<len;i++) $A[i].r/=Len;  - } - intAns[maxn],tot; - intMain () { AFreopen ("guess.in","R", stdin); +Freopen ("Guess.out","W", stdout); thescanf"%s%s", s,t); -     intlens=strlen (s); $     intLent=strlen (t); the      for(intI=0; i<lens;i++) thea[i]=s[i]-'a'+1; the      for(intI=0; i<lent;i++){ the         if(t[i]=='?') -b[lent-i-1]=0; in         Else theb[lent-i-1]=t[i]-'a'+1;  the     } About      the     intlen=1; the      while(len<=lens+lent) len<<=1; the      +      for(intI=0; i<lens;i++) A[i]=complex (1.0,0); -      for(intI=0; i<lent;i++) B[i]=complex (1.0*b[i]*b[i]*b[i],0); theFFT (A,len,1); FFT (B,len,1);Bayi      for(intI=0; i<len;i++) c[i]=a[i]*B[i]; theFFT (c,len,-1);  the      -memset (A,0,sizeof(A)); -memset (B,0,sizeof(B)); the      for(intI=0; i<lens;i++) A[i]=complex (2.0*a[i],0); the      for(intI=0; i<lent;i++) B[i]=complex (1.0*b[i]*b[i],0); theFFT (A,len,1); FFT (B,len,1); the      for(intI=0; i<len;i++) d[i]=a[i]*B[i]; -FFT (d,len,-1); the          thememset (A,0,sizeof(A)); thememset (B,0,sizeof(B));94      for(intI=0; i<lens;i++) A[i]=complex (1.0*a[i]*a[i],0); the      for(intI=0; i<lent;i++) B[i]=complex (1.0*b[i],0); theFFT (A,len,1); FFT (B,len,1); the      for(intI=0; i<len;i++) e[i]=a[i]*B[i];98FFT (e,len,-1); About      -      for(inti=lent-1; i<lens;i++)101         if(Fabs (C[I].R-D[I].R+E[I].R) <1e-5)102ans[++tot]=i-lent+1;103         104printf"%d\n", tot); the      for(intI=1; i<=tot;i++)106printf"%d\n", Ans[i]); 107     return 0;108}

Fast Fourier transform (FFT): COGS 2216. Do you think it's KMP?

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.