codeforces 528D Fuzzy Search
Original title address : Http://codeforces.com/problemset/problem/528/D
Test Instructions:
Given a two string s,t (character set size is 4:a, G, C, T), given the threshold K,
Defines that the T-string matches at a certain position in the S-string, and that at least one of the characters in the position of the parent string corresponding to the character at any position of the T-string is the same as the character.
The number of matches of T string in s string.
For example, for S=agcaattcat,t=acat,k=1, the number of matches is 3, as shown in the figure:
Data Range
1≤| T| ≤| s| ≤200 000,0≤k≤200 000
The following:
Similar to the previous question, the "match" is converted into a convolution form.
However, there is a problem with the definition of a match, there is a threshold for this thing that we cannot directly construct the appropriate representation method.
To make the match accurate, only one position can represent the character type of the left and right K positions.
Well, if we only consider the match of one character, we can think of:
For S-strings, the construction sequence F causes the character to be present in the left or right K-bit of I, where position I is 1, otherwise position i is 0.
For T-strings, construct the sequence g so that when I is the character, position I is 1, otherwise position i is 0.
So the number of matches for this character is:
ansi=∑j=0| T|−1fi−j∗gj ans_i=\sum\limits_{j=0}^{| T|-1}f_{i-j}*g_j
So you can run the four characters separately, add up, ans for | The t| match up.
Code:
#include <cstdio> #include <iostream> #include <algorithm> #include <cstring> #include <
Cmath> #define LD long double using namespace std;
const int n=200005;
const int mxn=524288+1000;
Const long Double Pi=acos (-1);
Char S[n],t[n];
int S,T,K,CNT[5],NUM[128],LEN,P,R[MXN],ANS[MXN];
struct Virt {long double r,i; Virt () {} Virt (long double R,long double i): R (R), I (i) {} Virt operator+ (const Virt &a) {return Virt (R+A.R,I+A.I);
} Virt operator-(const Virt &a) {return Virt (R-A.R,I-A.I);} Virt operator* (const Virt &a) {return Virt (R*A.R-I*A.I,R*A.I+I*A.R);}}
OMG[MXN],_OMG[MXN],A[MXN],B[MXN],C[MXN];
BOOL Ok[mxn][5];
void FFT (Virt *x,int opt) {for (int i=0;i<len;i++) if (I<r[i]) swap (x[i],x[r[i]]); Virt *w; if (opt==1) w=omg;
else w=_omg;
for (int m=2;m<=len;m<<=1) {int l=m>>1;
for (int j=0;j<len;j+=m) for (int i=0;i<l;i++) {Virt y=w[len/m*i]*x[i+j+l]; X[i+j+l]=x[i+j]-y;
X[i+j]=x[i+j]+y;
}} if (Opt==-1) for (int i=0;i<len;i++) x[i].r= (LD) x[i].r/(LD) Len;
} int main () {num[' A ']=0,num[' G ']=1,num[' C ']=2,num[' T ']=3; scanf ("%d%d%d", &s,&t,&k);
scanf ("%s%s", s,t);
Cnt[num[s[0]]]=1;
for (int i=0,p1=0,p2=0;i<s;i++) {while (p1<i-k) {cnt[num[s[p1]]]--; p1++;}
while (p2<i+k&&p2<s-1) {p2++; cnt[num[s[p2]]]++;}
for (int j=0;j<4;j++) if (cnt[j]>0) ok[s-i-1][j]=1;
} for (len=1,p=0;len< (s+t); len<<=1,p++); r[0]=0; for (int i=1;i<len;i++) r[i]= (r[i>>1]>>1) |
((i&1) << (p-1)); for (int i=0;i<len;i++) omg[i]=virt (cos (LD) 2*pi/(LD) len*i), Sin ((LD) 2*pi/(LD) len*i), _omg[i]=virt (omg[i].r,-
OMG[I].I); for (int c=0;c<4;c++) {for (int i=0;i<len;i++) A[i]=virt (ok[i][c],0), B[i]=virt ((i<t&& (num[t[i)
]==c)), 0); FFT (a,1);
FFT (b,1); for (int i=0;i<len;i++) A[i]=a[i]*b[i];
FFT (a,-1);
for (int i=0;i<len;i++) ans[i]+= (int) (a[i].r+0.5);
} int ret=0;
for (int i=t-1;i<s;i++) if (ans[i]==t) ret++;
printf ("%d\n", ret);
return 0;
}