time limit per test:3 seconds  memory limit per test:256 megabytes

input:standard input
output:standard output

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.

Examples

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.

Solution

只要A串中[i-k,i+k]范围内有字符X,就认为i位置可以匹配字符X。

问有多少位置可以匹配目标串B

生成函数 FFT

注意到只有四种字符,那么可以暴力分别处理这四种字符。

对于每种字符,在A串中扫描出可以匹配它的所有位置,标记为1,再将B串反转,将B串上对应字符的位置也标记为1,卷积即可得到该种字符的匹配情况。

做四遍卷积就可以愉快出解了。

 /*by SilverN*/
#include<iostream>
#include<algorithm>
#include<cstring>
#include<cstdio>
#include<cmath>
#include<vector>
#define LL long long
using namespace std;
const double pi=acos(-1.0);
const int mxn=;
int read(){
int x=,f=;char ch=getchar();
while(ch<'' || ch>''){if(ch=='-')f=-;ch=getchar();}
while(ch>='' && ch<=''){x=x*+ch-'';ch=getchar();}
return x*f;
}
struct com{
double x,y;
com operator + (const com &b){return (com){x+b.x,y+b.y};}
com operator - (const com &b){return (com){x-b.x,y-b.y};}
com operator * (const com &b){return (com){x*b.x-y*b.y,x*b.y+y*b.x};}
com operator / (double v){return (com){x/v,y/v};}
}a[mxn<<],b[mxn<<];
int N,len;
int rev[mxn<<];
void FFT(com *a,int flag){
for(int i=;i<N;i++)
if(i<rev[i])swap(a[i],a[rev[i]]);
for(int i=;i<N;i<<=){
com wn=(com){cos(pi/i),flag*sin(pi/i)};
int p=i<<;
for(int j=;j<N;j+=p){
com w=(com){,};
for(int k=;k<i;k++,w=w*wn){
com x=a[j+k],y=w*a[j+k+i];
a[j+k]=x+y;
a[j+k+i]=x-y;
}
}
}
if(flag==-)for(int i=;i<N;i++) a[i].x/=N;
return;
}
char s[mxn],c[mxn];
int S,T,K;
LL ans[mxn<<];
int hd,tl,ct;
void solve(char tp){
memset(a,,sizeof a);
memset(b,,sizeof b);
hd=;tl=-;ct=;
int i,j;
for(i=;i<S;i++){
while(i-hd>K){if(s[hd]==tp)ct--;hd++;}
while(tl-i+<=K && tl<S){tl++;if(s[tl]==tp)ct++;}
if(ct>) a[i].x=;
}
for(i=;i<T;i++)
if(c[i]==tp) b[i].x=;
FFT(a,);FFT(b,);
for(i=;i<N;i++) a[i]=a[i]*b[i];
FFT(a,-);
for(i=;i<N;i++)ans[i]+=(LL)(a[i].x+0.5); return;
}
int main(){
int i,j;
S=read();T=read();K=read();
scanf("%s",s);scanf("%s",c);
int m=S+T;
for(N=,len=;N<=m;N<<=)len++;
for(i=;i<N;i++)
rev[i]=(rev[i>>]>>)|((i&)<<(len-));
reverse(c,c+T);
solve('A');
solve('G');
solve('C');
solve('T');
int res=;
for(i=;i<N;i++){
if(ans[i]==T)res++;
// printf("%lld\n",ans[i]);
}
printf("%d\n",res);
return ;
}

CodeForces527D. Fuzzy Search的更多相关文章

  1. CF528D. Fuzzy Search [FFT]

    CF528D. Fuzzy Search 题意:DNA序列,在母串s中匹配模式串t,对于s中每个位置i,只要s[i-k]到s[i+k]中有c就认为匹配了c.求有多少个位置匹配了t 预处理\(f[i][ ...

  2. CF 528D. Fuzzy Search NTT

    CF 528D. Fuzzy Search NTT 题目大意 给出文本串S和模式串T和k,S,T为DNA序列(只含ATGC).对于S中的每个位置\(i\),只要中[i-k,i+k]有一个位置匹配了字符 ...

  3. 【Codeforces528D】Fuzzy Search FFT

    D. Fuzzy Search time limit per test:3 seconds memory limit per test:256 megabytes input:standard inp ...

  4. 【CF528D】Fuzzy Search(FFT)

    [CF528D]Fuzzy Search(FFT) 题面 给定两个只含有\(A,T,G,C\)的\(DNA\)序列 定义一个字符\(c\)可以被匹配为:它对齐的字符,在距离\(K\)以内,存在一个字符 ...

  5. Umbraco Examine 实现Fuzzy search

    在Umbraco examine search项目开发中,有一个需求, 就是intercom 和 intercoms需要返回同样的结果 也就是说 搜索intercom 时, 能返回包含intercom ...

  6. CF528D Fuzzy Search 和 BZOJ4259 残缺的字符串

    Fuzzy Search 给你文本串 S 和模式串 T,求 S 的每个位置是否能模糊匹配上 T. 这里的模糊匹配指的是把 T 放到 S 相应位置上之后,T 中每个字符所在位置附近 k 个之内的位置上的 ...

  7. CF-528D Fuzzy Search(FFT字符串匹配)

    Fuzzy Search 题意: 给定一个模式串和目标串按下图方式匹配,错开位置不多于k 解题思路: 总共只有\(A C G T\)四个字符,那么我们可以按照各个字符进行匹配,比如按照\(A\)进行匹 ...

  8. codeforces 528D Fuzzy Search

    链接:http://codeforces.com/problemset/problem/528/D 正解:$FFT$. 很多字符串匹配的问题都可以用$FFT$来实现. 这道题是要求在左边和右边$k$个 ...

  9. Codeforces 528D Fuzzy Search(FFT)

    题目 Source http://codeforces.com/problemset/problem/528/D Description Leonid works for a small and pr ...

随机推荐

  1. Git使用笔记一(关于如何设置密钥及提交)(Windows)

    如何设置密钥 ssh-keygen -t rsa或ssh-keygen -t rsa -C ‘邮箱’ (注意 1.-t前有一个空格:2.keygen是key generate的缩写:3.而后连续输入三 ...

  2. 《剑指offer》---输出链表倒数第k个结点

    本文算法使用python3实现 1 题目描述:   输入一个链表,输出该链表中倒数第k个结点.   时间限制:1s:空间限制:32768K 2 思路描述:   方法一:当链表长度为 $ n $ 时,输 ...

  3. LintCode-532.逆序对

    逆序对 在数组中的两个数字如果前面一个数字大于后面的数字,则这两个数字组成一个逆序对.给你一个数组,求出这个数组中逆序对的总数. 概括:如果a[i] > a[j] 且 i < j, a[i ...

  4. LintCode-381.螺旋矩阵 II

    螺旋矩阵 II 给你一个数n生成一个包含1-n^2的螺旋形矩阵 样例 n = 3 矩阵为 [     [ 1, 2, 3 ],     [ 8, 9, 4 ],     [ 7, 6, 5 ] ] 标 ...

  5. Swift-函数的理解

    /* 函数(Function) 函数是为执行特定功能的自包含的代码块.函数需要给定一个特定标识符(名字),然后当需要的时候, 就调用此函数来执行功能. */ // 函数的定义与调用 // 定义函数时, ...

  6. 【week2】 词频统计第一次更新

    词频统计: 对每个功能预计时间: 功能 预计(min) 实际(min) 数据流读入 20 40 正则规范字符串 15 20 排序 30 45 输出 20 30 其他   25 词频统计psp 日期 类 ...

  7. Gitkraken系列-Gitkraken修改用户名

    修改用户名 为了方便项目中代码的管理,需要重新编辑用户名. 点击右上角的图像即可看到如下图 3‑1所示的下拉菜单,鼠标悬于Profile上,会出现一个Edit按钮. 图 3‑1 编辑个人信息 点击Ed ...

  8. WriteLine(ls.ToString());Console.WriteLine(ls);输出结果相同,为什么要加 .ToString()

    using System; using System.Collections.Generic; using System.Linq; using System.Text; namespace Test ...

  9. 具体数学数论章-----致敬Kunth

    整除性(divisible): 引入了代表整除性. m\n (m|n) 表示m整除n.注意这里的整除.表示的是n = km(k为整数). 在整除性这里.m必须是个正数.也许你可以描述n 是 m 的k倍 ...

  10. [LOJ2538] [PKUWC2018] Slay the Spire

    题目链接 LOJ:https://loj.ac/problem/2538 Solution 计数好题. 首先可以发现这题和期望没关系. 其次对于手上的一套牌,设我们有\(a\)张强化牌,那么: 如果\ ...