Codeforces 528D Fuzzy Search(FFT)

题目

Source

http://codeforces.com/problemset/problem/528/D

Description

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.

Sample Input

10 4 1
AGCAATTCAT
ACAT

Sample Output

3

分析

题目大概相当于说给一个主串和模式串,主串各个位置i的字符可以等价于[i-k,i+k]位置中的任意一个字符,问模式串在主串中能匹配几次。

首先O(n)扫一遍主串就可以预处理出主串各个位置等价的字符集合,然后就是主串有多少个子串和模式串匹配的问题了。

这其实是FFT的经典应用:快速求出模式串某字符在主串所有位置中有多少个被匹配。通过枚举各个字符反转模式串构造多项式用FFT求乘积即可得出,LA4671

代码

#include<cstdio>
#include<cstring>
#include<cmath>
#include<algorithm>
using namespace std;
#define INF (1<<30)
#define MAXN 555555
const double PI=acos(-1.0);

struct Complex{
	double real,imag;
	Complex(double _real,double _imag):real(_real),imag(_imag){}
	Complex(){}
	Complex operator+(const Complex &cp) const{
		return Complex(real+cp.real,imag+cp.imag);
	}
	Complex operator-(const Complex &cp) const{
		return Complex(real-cp.real,imag-cp.imag);
	}
	Complex operator*(const Complex &cp) const{
		return Complex(real*cp.real-imag*cp.imag,real*cp.imag+cp.real*imag);
	}
	void setValue(double _real=0,double _imag=0){
		real=_real; imag=_imag;
	}
};

int len;
Complex wn[MAXN],wn_anti[MAXN];

void FFT(Complex y[],int op){
	for(int i=1,j=len>>1,k; i<len-1; ++i){
		if(i<j) swap(y[i],y[j]);
		k=len>>1;
		while(j>=k){
			j-=k;
			k>>=1;
		}
		if(j<k) j+=k;
	}
	for(int h=2; h<=len; h<<=1){
		Complex Wn=(op==1?wn[h]:wn_anti[h]);
		for(int i=0; i<len; i+=h){
			Complex W(1,0);
			for(int j=i; j<i+(h>>1); ++j){
				Complex u=y[j],t=W*y[j+(h>>1)];
				y[j]=u+t;
				y[j+(h>>1)]=u-t;
				W=W*Wn;
			}
		}
	}
	if(op==-1){
		for(int i=0; i<len; ++i) y[i].real/=len;
	}
}
void Convolution(Complex A[],Complex B[],int n){
	for(len=1; len<(n<<1); len<<=1);
	for(int i=n; i<len; ++i){
		A[i].setValue();
		B[i].setValue();
	}

	FFT(A,1); FFT(B,1);
	for(int i=0; i<len; ++i){
		A[i]=A[i]*B[i];
	}
	FFT(A,-1);
}

char S[222222],T[222222];
int cnt[4];
int get_idx(char ch){
	if(ch==‘A‘) return 0;
	if(ch==‘T‘) return 1;
	if(ch==‘C‘) return 2;
	if(ch==‘G‘) return 3;
	return -1;
}

int sta[222222],ans[MAXN];
Complex A[MAXN],B[MAXN];

int main(){
	for(int i=0; i<MAXN; ++i){
		wn[i].setValue(cos(2.0*PI/i),sin(2.0*PI/i));
		wn_anti[i].setValue(wn[i].real,-wn[i].imag);
	}
	int n,m,k;
	scanf("%d%d%d",&n,&m,&k);
	scanf("%s%s",S,T);
	int l=0,r=min(n,k)-1;
	for(int i=l; i<=r; ++i) ++cnt[get_idx(S[i])];
	for(int i=0; i<n; ++i){
		if(i-l>k) --cnt[get_idx(S[l++])];
		if(r+1<n) ++cnt[get_idx(S[++r])];

		for(int j=0; j<4; ++j){
			if(cnt[j]) sta[i]|=(1<<j);
		}
	}
	for(int i=0; i<4; ++i){
		for(int j=0; j<len; ++j){
			A[j].setValue();
			B[j].setValue();
		}
		for(int j=0; j<m; ++j){
			if(get_idx(T[j])==i) B[m-j-1].setValue(1);
		}
		for(int j=0; j<n; ++j){
			if(sta[j]>>i&1) A[j].setValue(1);
		}
		Convolution(A,B,n);
		for(int j=0; j<len; ++j){
			ans[j]+=(int)(A[j].real+0.5);
		}
	}
	int res=0;
	for(int i=0; i<len; ++i){
		if(ans[i]==m) ++res;
	}
	printf("%d",res);
	return 0;
}
时间: 2024-08-25 05:03:10

Codeforces 528D Fuzzy Search(FFT)的相关文章

B - Fuzzy Search (FFT)

题目链接:https://cn.vjudge.net/contest/281959#problem/B 题目大意:给你n,m,k.然后输入两个字符串,n代表第一个字符串s1,m代表第二个字符串s2,然后问你第二个字符串在第一个字符串能匹配的次数(选定第一个字符串的位置之后,任意s2中一个字符串,都能在s1对应的位置左右k个都能找到相同的字符). 具体思路:和 这一篇的思路基本使用一样的. AC代码: 1 #include<iostream> 2 #include<cstring>

快速傅里叶变换(FFT)

快速傅里叶变换(FFT)算法[详解] 快速傅里叶变换(Fast Fourier Transform)是信号处理与数据分析领域里最重要的算法之一.我打开一本老旧的算法书,欣赏了JW Cooley 和 John Tukey 在1965年的文章中,以看似简单的计算技巧来讲解这个东西. 本文的目标是,深入Cooley-Tukey  FFT 算法,解释作为其根源的"对称性",并以一些直观的python代码将其理论转变为实际.我希望这次研究能对这个算法的背景原理有更全面的认识. FFT(快速傅里叶

Hdu 1402 (FFT)

题目链接 A * B Problem Plus Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Submission(s): 12490    Accepted Submission(s): 2206 Problem Description Calculate A * B. Input Each line will contain two integers A and

CodeForces 534C - Polycarpus&#39; Dice(思路)

题意:给定n (1 <= n <= 2*10^5) 个骰子,给定每个骰子最大可以掷出的最大数(最小数始终为1),给定所有骰子掷出的点数和A,求每个骰子不可能掷出的点数个数. 考虑最大和最小情况,作差即可(详情见代码注释) #include<cstdio> #include<cstring> #include<cctype> #include<cstdlib> #include<cmath> #include<iostream&g

洛谷P3803 【模板】多项式乘法(FFT)

P3803 [模板]多项式乘法(FFT) 题目背景 这是一道FFT模板题 题目描述 给定一个n次多项式F(x),和一个m次多项式G(x). 请求出F(x)和G(x)的卷积. 输入输出格式 输入格式: 第一行2个正整数n,m. 接下来一行n+1个数字,从低到高表示F(x)的系数. 接下来一行m+1个数字,从低到高表示G(x))的系数. 输出格式: 一行n+m+1个数字,从低到高表示F(x)∗G(x)的系数. 输入输出样例 输入样例#1: 复制 1 2 1 2 1 2 1 输出样例#1: 复制 1

BZOJ[3527],[ZJOI2014]力(FFT)

BZOJ[3527],[ZJOI2014]力(FFT) 题意: 给出\(n\)个数\(q_i\),给出\(Fj\)的定义如下: \(F_j=\sum \limits _ {i < j} \frac{q_iq_j}{(i-j)^2}-\sum \limits _{i >j} \frac{q_iq_j}{(i-j)^2}.\) 令\(E_i=F_i/q_i\),求\(E_i\). 题解: 一开始没发现求\(E_i\)... 其实题目还更容易想了... \(E_i=\sum\limits _{j&l

【知识总结】快速傅里叶变换(FFT)

这可能是我第五次学FFT了--菜哭qwq 先给出一些个人认为非常优秀的参考资料: 一小时学会快速傅里叶变换(Fast Fourier Transform) - 知乎 小学生都能看懂的FFT!!! - 胡小兔 - 博客园 快速傅里叶变换(FFT)用于计算两个\(n\)次多项式相乘,能把复杂度从朴素的\(O(n^2)\)优化到\(O(nlog_2n)\).一个常见的应用是计算大整数相乘. 本文中所有多项式默认\(x\)为变量,其他字母均为常数.所有角均为弧度制. 一.多项式的两种表示方法 我们平时常

HDU 4609 3-idiots(FFT)

题意:给出n个正整数(数组A).每次随机选出三个数.问这三个数能组成三角形的概率为多大? 首先,我们用类似桶排计数的方法作出两个数组a,b,储存每个长度有几条边,然后对两个数组求卷积. 求出卷积后,这就代表了2条边能构成的边长度的集合了,注意,由于求卷积的时候可能把两条相同的边相加,所以最后求出的数组一定要减去这重复的部分,然后,先算x后算y等价于先算y后算x,所以要除以二. 然后,对于第三条边a[i],我们这样考虑:令它作为这三条边中最大的那条! 所以之前的卷积求出来的两边和一定会比这条边大,

傅里叶变换(FFT)的多相滤波结构实现

作者:桂. 时间:2017-09-25  14:53:01 链接:http://www.cnblogs.com/xingshansi/p/7591868.html 前言 以前在梳理信号频域变换的时候,提到逆序级联FFT(Inverse cascade FFT)的实现思路,后来分析多相滤波信道化,才发现其实Cascade FFT就是FFT的多相结构实现,在此系统梳理一下. 一.多相结构FFT实现 A-传统测频技术分析 信号的短时傅里叶变换(STFT)可表示为: 其中s为输入信号,w为对应窗函数.长