D. Fuzzy Search

$fft$$fft$下标一定要从0开始，不然特麻烦

$s\left[i\right]=\sum _{j=1}^{m}k\left[i-j\right]\ast t\left[j\right]$$s[i] = \sum_{j=1}^m k[i-j]*t[j]$其中$k\left[i\right]$$k[i]$表示第i个位置是否能匹配到某一个字母

c++代码如下:

#include<bits/stdc++.h>
#define PI acos(-1)
#define rep(i,x,y) for(register int i = x; i <= y; ++ i)
#define repd(i,x,y) for(register int i = x; i >= y; -- i)
#define id(x) (x == 'A' ? 0 : x == 'G' ? 1 : x == 'C' ? 2 : 3)
using namespace std;
typedef long long ll;

const int N = 8e5+50;
char s[N],t[N];
int n,m,k,L,len,R[N],ans[N],lst;

struct cpx
{
double x,y;
cpx(){}
cpx(double a,double b) { x = a,y = b; }
inline void clear(){ x = 0,y = 0; }
inline cpx operator * (cpx b) { return cpx(x*b.x - y * b.y,b.x*y + b.y * x); }
inline cpx operator *= (cpx b) { *this = *this * b; }
inline cpx operator + (cpx b) { return cpx(x + b.x,y + b.y); }
inline cpx operator - (cpx b) { return cpx(x - b.x,y - b.y); }
}a[N],b[N];

inline void fft(cpx*a,int f)
{
rep(i,0,len-1) if(i < R[i]) swap(a[i],a[R[i]]);
for(register int i = 1;i < len; i <<= 1)
{
cpx wn = cpx(cos(PI/i),f*sin(PI/i));
for(register int j = 0; j < len; j += i << 1)
{
cpx w = cpx(1,0);
for(register int k = 0; k < i; ++ k,w *= wn)
{
cpx x = a[j + k],y = w * a[i + j + k] ;
a[j + k] = x + y;
a[i + j + k] = x - y;
}
}
}
if(f == -1) rep(i,0,len-1) a[i].x /= len;
}

int main()
{
scanf("%d%d%d",&n,&m,&k);
scanf("%s",s);scanf("%s",t);

for(int i = 0,j = m-1;i < j ;++i,--j) swap(t[i],t[j]);
for(len = 1; len <= n + m; len <<= 1) ++ L;
rep(i,0,len-1) R[i] = ((R[i>>1]>>1) | ((i&1) << (L - 1)));

rep(i,0,3)
{
lst = -N;
rep(j,0,len-1) a[j].clear(),b[j].clear();
rep(j,0,k-1) if(i == id(s[j])) lst = j;
rep(j,0,n-1)
{
if(i == id(s[j + k]) && j + k < n) lst = j+k;
a[j].x = abs(lst - j) <= k;
}

rep(j,0,m-1) b[j].x = id(t[j]) == i;

fft(a,1); fft(b,1);
rep(j,0,len-1) a[j] *= b[j];
fft(a,-1);
rep(j,0,len-1) ans[j] += (int)(a[j].x+0.5);
}

int cnt = 0;
rep(i,m-1,n-1) if(ans[i] == m) ++cnt;
cout << cnt << endl;

return 0;
}

• 广告
• 抄袭
• 版权
• 政治
• 色情
• 无意义
• 其他

120