CF-528D Fuzzy Search(FFT字符串匹配)
题意:
给定一个模式串和目标串按下图方式匹配,错开位置不多于k

解题思路:
总共只有\(A C G T\)四个字符,那么我们可以按照各个字符进行匹配,比如按照\(A\)进行匹配时,当\(k=1\)时,我们将目标串
\(ACAT\)化作
\(1~0~1~0\)
模式串
\(AGCAATTCAT\)化作
\(1~1~1~1~1~1~0~1~1~1\)
同样是反置目标串
可以得到以x为匹配终点的位置的匹配函数\(p(X)=\sum_{i+j=x}A(i)B(j)\)
如此进行4次FFT,最后如果目标位置贡献等于目标串长度,则说明匹配成功
#include <bits/stdc++.h>
using namespace std;
/* freopen("k.in", "r", stdin);
freopen("k.out", "w", stdout); */
//clock_t c1 = clock();
//std::cerr << "Time:" << clock() - c1 <<"ms" << std::endl;
//#pragma comment(linker, "/STACK:1024000000,1024000000")
#define de(a) cout << #a << " = " << a << endl
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, a, n) for (int i = n; i >= a; i--)
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> PII;
typedef pair<double, double> PDD;
typedef vector<int, int> VII;
#define inf 0x3f3f3f3f
const ll INF = 0x3f3f3f3f3f3f3f3f;
const ll MAXN = 1e6 + 7;
const ll MAXM = 1e6 + 7;
const ll MOD = 998244353;
const double eps = 1e-6;
const double pi = acos(-1.0);
template <class T>
inline void in(T &x)
{
static char ch;
static bool neg;
for (ch = neg = 0; ch < '0' || '9' < ch; neg |= ch == '-', ch = getchar())
;
for (x = 0; '0' <= ch && ch <= '9'; (x *= 10) += ch - '0', ch = getchar())
;
x = neg ? -x : x;
}
struct Complex
{
double x, y;
Complex(double xx = 0, double yy = 0) { x = xx, y = yy; }
} a[MAXN], b[MAXN], c[MAXN], ans[MAXN];
Complex operator+(Complex a, Complex b) { return Complex(a.x + b.x, a.y + b.y); }
Complex operator-(Complex a, Complex b) { return Complex(a.x - b.x, a.y - b.y); }
Complex operator*(Complex a, Complex b) { return Complex(a.x * b.x - a.y * b.y, a.x * b.y + a.y * b.x); } //不懂的看复数的运算那部分
int N, M;
int l, r[MAXN];
int limit = 1;
void FFT(Complex *A, int type)
{
for (int i = 0; i < limit; i++)
if (i < r[i])
swap(A[i], A[r[i]]); //求出要迭代的序列
for (int mid = 1; mid < limit; mid <<= 1)
{ //待合并区间的长度的一半
Complex Wn(cos(pi / mid), type * sin(pi / mid)); //单位根
for (int R = mid << 1, j = 0; j < limit; j += R)
{ //R是区间的长度,j表示前已经到哪个位置了
Complex w(1, 0); //幂
for (int k = 0; k < mid; k++, w = w * Wn)
{ //枚举左半部分
Complex x = A[j + k], y = w * A[j + mid + k]; //蝴蝶效应
A[j + k] = x + y;
A[j + mid + k] = x - y;
}
}
}
/*if (type == -1)
for (int i = 0; i < limit; ++i)
a[i].x /= limit;//我们推过的公式里面有一个1/n这一项*/
}
char s[MAXN], t[MAXN];
void init(int N, int M)
{
while (limit <= N + M)
limit <<= 1, l++;
for (int i = 0; i < limit; i++)
r[i] = (r[i >> 1] >> 1) | ((i & 1) << (l - 1));
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
}
int change(char str)
{
if (str == 'A')
return 1;
else if (str == 'T')
return 2;
else if (str == 'G')
return 3;
else
return 4;
}
int pre[MAXN], cnt;
int main()
{
int n, m, k;
scanf("%d%d%d %s %s", &n, &m, &k, s, t);
reverse(t, t + m);
init(n, m);
for (int ca = 1; ca <= 4; ca++)
{
cnt = -1;
memset(pre, 0, sizeof(pre));
memset(a, 0, sizeof(a));
memset(b, 0, sizeof(b));
for (int i = 0; i < n; i++)
{
if (change(s[i]) == ca)
pre[++cnt] = i;
a[i].x = change(s[i]) == ca ? 1 : 0, a[i].y = 0;
}
for (int i = 0; i < m; i++)
b[i].x = change(t[i]) == ca ? 1 : 0, b[i].y = 0;
int now = -1;
for (int i = 0; i <= cnt; i++)
{
int L = max(pre[i] - k, 0);
int R = min(pre[i] + k, n - 1);
if (now > R)
continue;
now = max(L, now);
for (; now <= R; now++)
a[now].x = 1;
now--;
}
FFT(a, 1);
FFT(b, 1);
for (int i = 0; i < limit; i++)
a[i] = b[i] * a[i];
FFT(a, -1);
for (int i = 0; i < limit; i++)
c[i] = c[i] + a[i];
}
int ans = 0;
for (int i = m - 1; i < limit; i++)
if (int(c[i].x / limit + 0.5) == m)
ans++;
printf("%d\n", ans);
return 0;
}
CF-528D Fuzzy Search(FFT字符串匹配)的更多相关文章
- CF 528D. Fuzzy Search NTT
CF 528D. Fuzzy Search NTT 题目大意 给出文本串S和模式串T和k,S,T为DNA序列(只含ATGC).对于S中的每个位置\(i\),只要中[i-k,i+k]有一个位置匹配了字符 ...
- Codeforces.528D.Fuzzy Search(FFT)
题目链接 \(Descripiton\) 给出文本串S和模式串T和k,S,T为DNA序列(只含\(A,T,G,C\)).对于S中的每个位置\(i\),只要\(s[i-k]\sim s[i+k]\)中有 ...
- CodeForces - 528D Fuzzy Search (FFT求子串匹配)
题意:求母串中可以匹配模式串的子串的个数,但是每一位i的字符可以左右偏移k个位置. 分析:类似于 UVALive -4671. 用FFT求出每个字符成功匹配的个数.因为字符可以偏移k个单位,先用尺取法 ...
- 2018 ACM-ICPC 中国大学生程序设计竞赛线上赛 H题 Rock Paper Scissors Lizard Spock.(FFT字符串匹配)
2018 ACM-ICPC 中国大学生程序设计竞赛线上赛:https://www.jisuanke.com/contest/1227 题目链接:https://nanti.jisuanke.com/t ...
- P4173 残缺的字符串(FFT字符串匹配)
P4173 残缺的字符串(FFT字符串匹配) P4173 解题思路: 经典套路将模式串翻转,将*设为0,设以目标串的x位置匹配结束的匹配函数为\(P(x)=\sum^{m-1}_{i=0}[A(m-1 ...
- codeforces 528D Fuzzy Search
链接:http://codeforces.com/problemset/problem/528/D 正解:$FFT$. 很多字符串匹配的问题都可以用$FFT$来实现. 这道题是要求在左边和右边$k$个 ...
- CF528D. Fuzzy Search [FFT]
CF528D. Fuzzy Search 题意:DNA序列,在母串s中匹配模式串t,对于s中每个位置i,只要s[i-k]到s[i+k]中有c就认为匹配了c.求有多少个位置匹配了t 预处理\(f[i][ ...
- 【Codeforces528D】Fuzzy Search FFT
D. Fuzzy Search time limit per test:3 seconds memory limit per test:256 megabytes input:standard inp ...
- BZOJ4259: 残缺的字符串(FFT 字符串匹配)
题意 题目链接 Sol 知道FFT能做字符串匹配的话这就是个裸题了吧.. 考虑把B翻转过来,如果\(\sum_{k = 0}^M (B_{i - k} - A_k)^2 * B_{i-k}*A_k = ...
随机推荐
- CentOS 7防火墙
CentOS 7.0默认使用的是firewall作为防火墙,这里改为iptables防火墙步骤. 1.关闭firewall: systemctl stop firewalld.service #停止f ...
- Python网络编程笔记二
使用select模块实现IO多路复用服务端 import socket import select #windows上只支持select.select,不支持poll epoll HOST = &qu ...
- TCP/IP||Ping
1.what's PING 由Mike Muuss编写,为了测试另一台主机是否可达,发送一份ICMP回显请求报文给主机,并等待返回ICMP回显应答,如果不能PING到某台主机,那么就不能Telnet或 ...
- 换根DP(二次扫描)
参考来自这里: https://blog.csdn.net/qq_41286356/article/details/94554729 题目在这里 https://ac.nowcoder.com/acm ...
- 2019 ICPC南昌网络赛 B题
英雄灭火问题忽略了一点丫 一个超级源点的事情,需要考虑周全丫 2 #include<cstdio> #include<cstring> #include<queue> ...
- C#反射与特性(二):探究反射
目录 1,反射的使用概述 2,获取 Type 在上一章中,我们探究了 C# 引入程序集的各种方法,这一章节笔者将探究 C# 中使用反射的各种操作和代码实践. 1,反射的使用概述 1.1 什么是反射 & ...
- 用实例理解设计模式——代理模式(Python版)
代理模式:为其他对象提供一种代理以控制对这个对象的访问. 在某些情况下,一个对象不适合或者不能直接引用另一个对象,而代理对象可以在客户端和目标对象之间起到中介的作用. 代理模式分为: 静态代理 动态代 ...
- 小小知识点(二十七)20大5G关键技术
5G网络技术主要分为三类:核心网.回传和前传网络.无线接入网. 核心网 核心网关键技术主要包括:网络功能虚拟化(NFV).软件定义网络(SDN).网络切片和多接入边缘计算(MEC). 1 网络功能虚拟 ...
- 《C++Primer》第五版习题答案--第三章【学习笔记】
[C++Primer]第五版[学习笔记]习题解答第三章 ps:答案是个人在学习过程中书写,可能存在错漏之处,仅作参考. 作者:cosefy Date: 2020/1/10 第三章:字符串,向量和数组 ...
- DevExpress 控件用法笔记(VB)
1.ChartControl 显示条形图 ChartControl1.Titles.Clear() ChartControl1.Series.Clear() Dim db As DataTable S ...