D. Fuzzy Search

版权声明:orz https://blog.csdn.net/Tgotp/article/details/79959085

fft下标一定要从0开始,不然特麻烦
观察题目容易发现,可以在O(n)时间内判断对于位置i是否能在[i-k,i+k]区间存在AGCT
然后得到一个O(nm)的算法,考虑优化
那么分开考虑AGCT,令s[i]表示在第i个位置匹配到了多少字母。
s[i]=j=1mk[ij]t[j]其中k[i]表示第i个位置是否能匹配到某一个字母
类似于某个字符串匹配的题一样 ,t数组还是要取反。
然后fft即可,注意要四舍五入,复杂度O(nlogn)
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;
}
阅读更多

扫码向博主提问

Tgotp

QAQ
  • 擅长领域:
  • OI
去开通我的Chat快问
换一批

没有更多推荐了,返回首页