codeforces 528D Fuzzy Search

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.
Input

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’.
Output

Print a single number — the number of occurrences of T in S with the error threshold k by the given definition.
Example

Input
10 4 1
AGCAATTCAT
ACAT

Output
3

Note

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.


【分析】
由于只有4个字符…所以我们可以枚举每一个字符ch,把短串翻转一下。把长串可以匹配ch的位置和短串是ch的位置预处理出来,为1,其余为0。然后求一个卷积,如果卷积后某一个位置的系数与短串中ch出现的次数相等,那么这个长串中以这个位置结尾可以匹配短串中的所有ch。

如果某个位置可以匹配所有4个字符,那么该位置对答案产生贡献。


【代码】

//codeforces 528D Fuzzy Search
#include<cmath>
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define pi acos(-1)
#define ll long long
#define M(a) memset(a,0,sizeof a)
#define fo(i,j,k) for(i=j;i<=k;i++)
using namespace std;
const int mxn=1600005;
char s[mxn],t[mxn];
int N,M,K,L,n,m,fuck;
int R[mxn],ans[mxn];
struct E
{
    double r,f;
    E (double _r,double _f) {r=_r,f=_f;}
    E () {}
    E operator + (E u) {return E(r+u.r,f+u.f);}
    E operator - (E u) {return E(r-u.r,f-u.f);}
    E operator * (E u) {return E(r*u.r-f*u.f,r*u.f+f*u.r);}
    E operator / (int u) {return E(r/u,f/u);}
}a[mxn],b[mxn],c[mxn];
inline void FFT(E *a,int f)
{
    int i,j,k;
    fo(i,0,n-1) if(i<R[i]) swap(a[i],a[R[i]]);
    for(i=1;i<n;i<<=1)
    {
        E wn(cos(pi/i),f*sin(pi/i));
        for(j=0;j<n;j+=(i<<1))
        {
            E w(1,0);
            for(k=0;k<i;k++,w=w*wn)
            {
                E x=a[j+k],y=w*a[j+k+i];
                a[j+k]=x+y,a[j+k+i]=x-y;
            }
        }
    }
    if(f==-1) fo(i,0,n-1) a[i]=a[i]/n;
}
inline void solve(char ch)
{
    M(a),M(b);
    int i,j,cnt=0;
    fo(i,0,N) if(s[i]==ch)
    {
        a[i].r=1.0;
        for(j=i+1;j<=i+K && j<=N;j++)
          if(s[j]==ch) break;
          else a[j].r=1.0;
        for(j=i-1;j>=i-K && j>=0;j--)
          if(s[j]==ch) break;
          else a[j].r=1.0;
    }
    fo(i,0,M) if(t[i]==ch) cnt++,b[i].r=1.0;
    FFT(a,1),FFT(b,1);
    fo(i,0,n-1) c[i]=a[i]*b[i];
    FFT(c,-1);
    fo(i,M,N) if((int)(c[i].r+0.5)==cnt) ans[i]++;
}
int main()
{
    int i,j;
    scanf("%d%d%d",&N,&M,&K);N--,M--;
    scanf("%s%s",s,t);
    for(i=0;i+i<=M;i++) swap(t[i],t[M-i]);
    n=N+M,m=2*n;for(n=1;n<=m;n<<=1) L++;
    fo(i,0,n-1) R[i]=(R[i>>1]>>1)|((i&1)<<L-1);
    solve('A'),solve('G'),solve('C'),solve('T');
    fo(i,M,N) if(ans[i]==4) fuck++;
    printf("%d\n",fuck);
    return 0;
}
/*
10 4 1
AGCAATTCAT
ACAT
*/
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值