CodeForces527D. Fuzzy Search
time limit per test:3 seconds memory limit per test:256 megabytes
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
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的更多相关文章
- CF528D. Fuzzy Search [FFT]
CF528D. Fuzzy Search 题意:DNA序列,在母串s中匹配模式串t,对于s中每个位置i,只要s[i-k]到s[i+k]中有c就认为匹配了c.求有多少个位置匹配了t 预处理\(f[i][ ...
- CF 528D. Fuzzy Search NTT
CF 528D. Fuzzy Search NTT 题目大意 给出文本串S和模式串T和k,S,T为DNA序列(只含ATGC).对于S中的每个位置\(i\),只要中[i-k,i+k]有一个位置匹配了字符 ...
- 【Codeforces528D】Fuzzy Search FFT
D. Fuzzy Search time limit per test:3 seconds memory limit per test:256 megabytes input:standard inp ...
- 【CF528D】Fuzzy Search(FFT)
[CF528D]Fuzzy Search(FFT) 题面 给定两个只含有\(A,T,G,C\)的\(DNA\)序列 定义一个字符\(c\)可以被匹配为:它对齐的字符,在距离\(K\)以内,存在一个字符 ...
- Umbraco Examine 实现Fuzzy search
在Umbraco examine search项目开发中,有一个需求, 就是intercom 和 intercoms需要返回同样的结果 也就是说 搜索intercom 时, 能返回包含intercom ...
- CF528D Fuzzy Search 和 BZOJ4259 残缺的字符串
Fuzzy Search 给你文本串 S 和模式串 T,求 S 的每个位置是否能模糊匹配上 T. 这里的模糊匹配指的是把 T 放到 S 相应位置上之后,T 中每个字符所在位置附近 k 个之内的位置上的 ...
- CF-528D Fuzzy Search(FFT字符串匹配)
Fuzzy Search 题意: 给定一个模式串和目标串按下图方式匹配,错开位置不多于k 解题思路: 总共只有\(A C G T\)四个字符,那么我们可以按照各个字符进行匹配,比如按照\(A\)进行匹 ...
- codeforces 528D Fuzzy Search
链接:http://codeforces.com/problemset/problem/528/D 正解:$FFT$. 很多字符串匹配的问题都可以用$FFT$来实现. 这道题是要求在左边和右边$k$个 ...
- Codeforces 528D Fuzzy Search(FFT)
题目 Source http://codeforces.com/problemset/problem/528/D Description Leonid works for a small and pr ...
随机推荐
- lintcode-39-恢复旋转排序数组
39-恢复旋转排序数组 给定一个旋转排序数组,在原地恢复其排序. 说明 什么是旋转数组? 比如,原始数组为[1,2,3,4], 则其旋转数组可以是[1,2,3,4], [2,3,4,1], [3,4, ...
- Ajax在jQuery中的应用(加载异步数据、请求服务器数据)
加载异步数据 jQuery中的load()方法 load(url,[data],[callback]) url:被加载的页面地址 [data]:可选项表示发送到服务器的数据,其格式为 key/valu ...
- 可视化自编码器训练结果&稀疏自编码器符号一览表
训练完(稀疏)自编码器,我们还想把这自编码器学习到的函数可视化出来,好弄明白它到底学到了什么.我们以在10×10图像(即n=100)上训练自编码器为例.在该自编码器中,每个隐藏单元i对如下关于输入的函 ...
- BZOJ4299 Codechef FRBSUM(主席树)
感觉非常不可做,于是考虑有什么奇怪的性质. 先考虑怎么求子集和mex.将数从小到大排序,假设已经凑出了0~n的所有数,如果下一个数>n+1显然mex就是n+1了,否则若其为x则可以凑出1~n+x ...
- springboot2.0 集成elasticsearch,实现检索、分页、排序
springboot整合es的方式: transport方式(7.0弃用,8.0移除) spring-data(完全当做数据库来用,无法全部支持es,内部也是基于transport,包装后使用非常简单 ...
- [洛谷P3332][ZJOI2013]K大数查询
题目大意:有$n$个位置,$m$个操作.操作有两种: $1\;l\;r\;x:$在区间$[l,r]$每个位置加上一个数$x$ $2\;l\;r\;k:$询问$[l,r]$中第$k$大的数是多少. 题解 ...
- bzoj 1103: [POI2007]大都市meg (dfs序)
dfs序,加个bit维护前缀和就行了 type arr=record toward,next:longint; end; const maxn=; var edge:..maxn]of arr; bi ...
- CF#312 558e A Simple Task
~~~题面~~~ 题解: 观察到字母只有26个,因此考虑对这26个字母分别维护,每个线段树维护一个字母,如果一个线段树的某个叶节点有值,表示当前叶节点所在位置的字母是现在这个线段树代表的字母. 那么对 ...
- POJ3243:Clever Y——题解
http://poj.org/problem?id=3243 求最小的非负整数y满足x^y=k(mod z) 写完板子之后等待了半个小时poj才终于进入…… poj不行啊.jpg 以前一直觉得BSGS ...
- BZOJ3534:[SDOI2014]重建——题解
https://www.lydsy.com/JudgeOnline/problem.php?id=3534 https://www.luogu.org/problemnew/show/P3317 T国 ...