zoukankan      html  css  js  c++  java
  • CodeForces

    题意:求母串中可以匹配模式串的子串的个数,但是每一位i的字符可以左右偏移k个位置.
    分析:类似于 UVALive -4671. 用FFT求出每个字符成功匹配的个数.因为字符可以偏移k个单位,先用尺取法处理出每个位置能够取到的字符.设模式串长度为m.
    (C(m-1+k) = sum_{i=0}^{m-1}A_{i+k}*B(m-i-1)).
    反转模式串B, 对每个字符c,若该位上能够取到c,则多项式该位取1,否则为0,FFT求卷积.并记录[m-1,n-1]每个位置4次计算的系数(C)之和.
    若系数之和=m,表示母串中以位置i结尾,长度为m的字串与B相匹配.

    #include <bits/stdc++.h>
    using namespace std;
    typedef long long LL;
    const int MAXN = 4e5 + 10;
    const double PI = acos(-1.0);
    struct Complex{
        double x, y;
        inline Complex operator+(const Complex b) const {
            return (Complex){x +b.x,y + b.y};
        }
        inline Complex operator-(const Complex b) const {
            return (Complex){x -b.x,y - b.y};
        }
        inline Complex operator*(const Complex b) const {
            return (Complex){x *b.x -y * b.y,x * b.y + y * b.x};
        }
    } va[MAXN * 2 + MAXN / 2], vb[MAXN * 2 + MAXN / 2];
    int lenth = 1, rev[MAXN * 2 + MAXN / 2];
    int N, M;   // f 和 g 的数量
        //f g和 的系数
        // 卷积结果
        // 大数乘积
    int f[MAXN],g[MAXN];
    vector<LL> conv;
    vector<LL> multi;
    //f g
    void init()
    {
        int tim = 0;
        lenth = 1;
        conv.clear(), multi.clear();
        memset(va, 0, sizeof va);
        memset(vb, 0, sizeof vb);
        while (lenth <= N + M - 2)
            lenth <<= 1, tim++;
        for (int i = 0; i < lenth; i++)
            rev[i] = (rev[i >> 1] >> 1) + ((i & 1) << (tim - 1));
    }
    void FFT(Complex *A, const int fla)
    {
        for (int i = 0; i < lenth; i++){
            if (i < rev[i]){
                swap(A[i], A[rev[i]]);
            }
        }
        for (int i = 1; i < lenth; i <<= 1){
            const Complex w = (Complex){cos(PI / i), fla * sin(PI / i)};
            for (int j = 0; j < lenth; j += (i << 1)){
                Complex K = (Complex){1, 0};
                for (int k = 0; k < i; k++, K = K * w){
                    const Complex x = A[j + k], y = K * A[j + k + i];
                    A[j + k] = x + y;
                    A[j + k + i] = x - y;
                }
            }
        }
    }
    void getConv(){             //求多项式
        init();
        for (int i = 0; i < N; i++)
            va[i].x = f[i];
        for (int i = 0; i < M; i++)
            vb[i].x = g[i];
        FFT(va, 1), FFT(vb, 1);
        for (int i = 0; i < lenth; i++)
            va[i] = va[i] * vb[i];
        FFT(va, -1);
        for (int i = 0; i <= N + M - 2; i++)
            conv.push_back((LL)(va[i].x / lenth + 0.5));
    }
    
    const int len = 2e5+10;
    
    char s1[len],s2[len];
    int cnt[4];
    int have[MAXN][4];
    map<char,int> id;
    LL ans[MAXN];
    
    void debug(){
        for(int i=0;i<4;++i){
            for(int j=0;j<4;++j){
                cout<<have[i][j]<<" ";
            }
            cout<<endl;
        }
    }
    
    int main()
    {
        #ifndef ONLINE_JUDGE
            freopen("in.txt","r",stdin);
            freopen("out.txt","w",stdout);
        #endif
        int n,m,k;
        id['A'] = 0, id['C'] = 1, id['G'] =2, id['T'] = 3;
        scanf("%d %d %d",&n,&m,&k );
        scanf("%s",s1);
        scanf("%s",s2);
        int L=0,R=-1;
        for(int i=0;i<n;++i){
            while(L<i-k) cnt[id[s1[L++]]]--;         //退
            while(R<n-1 && R<i+k) cnt[id[s1[++R]]]++;  //增
            for(int j=0;j<4;++j){
                if(cnt[j]) have[i][j] = 1;
            }
        }
        for(int k=0;k<4;++k){
            N = n, M = m;
            for(int i=0;i<N;++i){
                if(have[i][k]) f[i] = 1;
                else f[i] = 0;
            }
            for(int i=0;i<M;++i){
                if(id[s2[m-i-1]]==k) g[i] = 1;
                else g[i] = 0;
            }
            getConv();
            int sz = conv.size();
            for(int i=m-1;i<sz;++i){
                ans[i]+= conv[i];
            }
        }
        int res=0;
        for(int i=m-1;i<n;++i){
            if(ans[i]==m){
                res++;
            }
        }
        printf("%d
    ",res);
        return 0;
    }
    
    
  • 相关阅读:
    适者生存还是强者生存
    写给十岁的清为
    毕业后的十年
    Python3 字符编码
    线段树模板
    F
    E
    D
    C
    B
  • 原文地址:https://www.cnblogs.com/xiuwenli/p/9737207.html
Copyright © 2011-2022 走看看