Leonid works for a small and promising start-up that works on decoding the human genome. His duties include solving complex problems of finding certain patterns in long strings consisting of letters ‘A‘, ‘T‘, ‘G‘ and ‘C‘.
Let‘s consider the following scenario. There is a fragment of a human DNA chain, recorded as a string S. To analyze the fragment, you need to find all occurrences of string T in a string S. However, the matter is complicated by the fact that the original chain fragment could contain minor mutations, which, however, complicate the task of finding a fragment. Leonid proposed the following approach to solve this problem.
Let‘s write down integer k ≥ 0 — the error threshold. We will say that string T occurs in string S on position i (1 ≤ i ≤ |S| - |T| + 1), if after putting string T along with this position, each character of string T corresponds to the some character of the same value in string S at the distance of at most k. More formally, for any j (1 ≤ j ≤ |T|) there must exist such p (1 ≤ p ≤ |S|), that |(i + j - 1) - p| ≤ k and S[p] = T[j].
For example, corresponding to the given definition, string "ACAT" occurs in string "AGCAATTCAT" in positions 2, 3 and 6.
Note that at k = 0 the given definition transforms to a simple definition of the occurrence of a string in a string.
Help Leonid by calculating in how many positions the given string T occurs in the given string S with the given error threshold.
The first line contains three integers |S|, |T|, k (1 ≤ |T| ≤ |S| ≤ 200 000, 0 ≤ k ≤ 200 000) — the lengths of strings S and T and the error threshold.
The second line contains string S.
The third line contains string T.
Both strings consist only of uppercase letters ‘A‘, ‘T‘, ‘G‘ and ‘C‘.
Print a single number — the number of occurrences of T in S with the error threshold k by the given definition.
10 4 1
AGCAATTCAT
ACAT
3
If you happen to know about the structure of the human genome a little more than the author of the problem, and you are not impressed with Leonid‘s original approach, do not take everything described above seriously.
1 #include<iostream> 2 #include<cstdio> 3 #include<cstring> 4 #include<algorithm> 5 #include<cmath> 6 #include<complex> 7 using namespace std; 8 typedef long long lol; 9 typedef complex<double>dob; 10 double pi=acos(-1.0); 11 const int NN=800001; 12 dob a[NN],b[NN]; 13 char ch[4]; 14 int R[NN],lg; 15 char s[200001],t[200001]; 16 int M,inf=2e9,ans[NN],res; 17 void FFT(dob *A,int len,int flag) 18 {int i,j,k; 19 for (i=0;i<len;i++) 20 if (i<R[i]) swap(A[i],A[R[i]]); 21 for (i=1;i<len;i<<=1) 22 { 23 dob wn(cos(pi/i),sin(flag*pi/i)),x,y; 24 for (j=0;j<len;j+=(i<<1)) 25 { 26 dob w(1,0); 27 for (k=0;k<i;k++,w*=wn) 28 { 29 x=A[j+k];y=w*A[j+k+i]; 30 A[j+k]=x+y; 31 A[j+k+i]=x-y; 32 } 33 } 34 } 35 } 36 int main() 37 {int i,n,m,k,p,pos; 38 cin>>n>>m>>k; 39 ch[0]=‘A‘;ch[1]=‘T‘;ch[2]=‘C‘;ch[3]=‘G‘; 40 scanf("%s",s); 41 scanf("%s",t); 42 reverse(t,t+m); 43 M=n+m;int len=1; 44 while (len<=M) len*=2,lg++; 45 for (i=0;i<=len;i++) 46 R[i]=(R[i>>1]>>1)|((i&1)<<(lg-1)); 47 for (p=0;p<4;p++) 48 { 49 memset(a,0,sizeof(a)); 50 memset(b,0,sizeof(b)); 51 pos=-inf; 52 for (i=0;i<n;i++) 53 { 54 if (s[i]==ch[p]) pos=i; 55 if (i-pos<=k) a[i]=1; 56 } 57 pos=inf; 58 for (i=n-1;i>=0;i--) 59 { 60 if (s[i]==ch[p]) pos=i; 61 if (pos-i<=k) a[i]=1; 62 } 63 for (i=0;i<m;i++) 64 { 65 if (t[i]==ch[p]) b[i]=1; 66 } 67 FFT(a,len,1);FFT(b,len,1); 68 for (i=0;i<len;i++) 69 a[i]=a[i]*b[i]; 70 FFT(a,len,-1); 71 for (i=0;i<len;i++) 72 ans[i]+=((int)(a[i].real()/len+0.5)); 73 } 74 for (i=0;i<len;i++) 75 if (ans[i]==m) res++; 76 cout<<res; 77 }