Problem

Description

你有一个长度为 \(n\) 的串 \(S\),以及长度为 \(m\) 的串 \(T\)。

现给定一个数 \(k\) ,我们说 \(T\) 在 \(S\) 的位置 \(i\) 匹配上,当且仅当对于每一个 \(1\le a\le m\) ,都有一个位置 \(1\le b\le n\) 满足 \(|(i+a-1)-b|\le k\) ,且 \(S_b=T_a\) 。

请回答 \(T\) 在 \(S\) 中匹配上了多少个不同的位置。

Range

\(n,m,k\le2*10^5\)

Algorithm

多项式

Mentality

思路很妙的说。

先考虑 \(k=0\) 的情况。不难发现,当 \(T\) 与 \(S\) 匹配上时,\(T\) 中的每个字符与 \(S\) 中对应匹配字符的 位置的差 是相等的。

同时考虑在多项式乘法中,若许多对项最后会贡献在同一个位置上,那么它们的 次方的和 是相等的。

则考虑倒转原串,得到 \(T'\) 。

由于字符集大小仅仅为 \(4\) ,我们可以尝试一下分开考虑每种不同的字符。

对于当前字符 \(c\),考虑设 \(f_i=[S_i==c]*x^i\),\(g_i=[T'_i==c]*x^i\)。

则对于得到的 \(F(x)=f(x)*g(x)\) 而言,我们发现对于原串中的字符 \(S_i=T_j=c\) ,它们在 \(f\) 与 \(g\) 中对应项的乘积为 \(1\) ,且对位置 \(m-j+1+i\) 产生了 \(1\) 的贡献。

对于下一种字符 \(p\) 而言,若有 \(S_{i+1}=T_{j+1}=p\),则会对 \(m-(j+1)+1+(i+1)=m-j+1+i\) 有 \(1\) 的贡献。

换句话说,若有 \(T\) 在 \(S\) 的位置 \(i\) 匹配上了,那么必定有:\(T_1=S_i,T_2=S_{i+1}\dots T_m=S{i+m-1}\) ,也就必定会在对四种字符的卷积里对 \(m-j+1+i\) 这一项总共产生 \(m\) 的贡献。

将四次卷积的结果相加,则最后的答案为:系数为 \(m\) 的项的个数。

接着考虑 \(k>0\) 的情况,我们会发现和 \(k=0\) 的思路几乎一致,对于这个 \(k\) ,想想它的意义:我们在处理每种不同的字符的时候,若当前字符为 \(c\) ,对于每个为 \(c\) 的位置,它往左右两边 \(k\) 位也都是可以匹配的位置。

那我们直接将每个 \(c\) 的左右 \(k\) 位也都设成 \(c\) 不就好了嘛?

那么思路就很清晰了:四种字符分别统计,然后对于每种当前统计的字符,将左右 \(k\) 位设为同样的字符,得到 \(f,g\) 两个多项式,并将其卷积得到 \(F(x)=f(x)*g(x)\) ,将四个 \(F(x)\) 相加,统计系数为 \(m\) 的项数。

完毕。

Code

#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
using namespace std;
#define LL long long
#define go(x, i, v) for (int i = hd[x], v = to[i]; i; v = to[i = nx[i]])
#define inline __inline__ __attribute__((always_inline))
LL read() {
long long x = 0, w = 1;
char ch = getchar();
while (!isdigit(ch)) w = ch == '-' ? -1 : 1, ch = getchar();
while (isdigit(ch)) {
x = (x << 3) + (x << 1) + ch - '0';
ch = getchar();
}
return x * w;
}
const int Max_n = 2e5 + 5, mod = 998244353, G = 3;
int n, m, K, ans;
int f[Max_n << 2], g[Max_n << 2], A[Max_n << 2];
int lim, bit, rev[Max_n << 2];
char S[Max_n], T[Max_n];
inline void Convert(char &c) {
if (c == 'A') c = 'a';
if (c == 'C') c = 'b';
if (c == 'G') c = 'c';
if (c == 'T') c = 'd';
}
inline int ksm(int a, int b) {
int res = 1;
for (; b; b >>= 1, a = 1ll * a * a % mod)
if (b & 1) res = 1ll * res * a % mod;
return res;
}
namespace NTT {
inline void dft(int *f, bool t) {
for (int i = 0; i < lim; i++)
if (rev[i] > i) swap(f[i], f[rev[i]]);
for (int len = 1; len < lim; len <<= 1) {
int Wn = ksm(G, (mod - 1) / (len << 1));
if (t) Wn = ksm(Wn, mod - 2);
for (int i = 0; i < lim; i += len << 1) {
int Wnk = 1;
for (int k = i; k < i + len; k++, Wnk = 1ll * Wnk * Wn % mod) {
int x = f[k], y = 1ll * f[k + len] * Wnk % mod;
f[k] = (x + y) % mod, f[k + len] = (x - y + mod) % mod;
}
}
}
}
} // namespace NTT
inline void ntt(int *f, int *g) {
NTT::dft(f, 0), NTT::dft(g, 0);
for (int i = 0; i < lim; i++) f[i] = 1ll * f[i] * g[i] % mod;
NTT::dft(f, 1);
int Inv = ksm(lim, mod - 2);
for (int i = 0; i < lim; i++) f[i] = 1ll * f[i] * Inv % mod;
}
int main() {
#ifndef ONLINE_JUDGE
freopen("D.in", "r", stdin);
freopen("D.out", "w", stdout);
#endif
n = read(), m = read(), K = read();
scanf(" %s", S + 1), scanf("%s", T + 1);
for (int i = 1; i <= n; i++) Convert(S[i]);
for (int i = 1; i <= m; i++) Convert(T[i]);
for (int i = 1; i <= m / 2; i++) swap(T[i], T[m - i + 1]);
bit = log2(n + m) + 1, lim = 1 << bit;
for (int i = 0; i < lim; i++)
rev[i] = rev[i >> 1] >> 1 | ((i & 1) << (bit - 1));
for (int k = 'a'; k <= 'd'; k++) {
memset(f, 0, sizeof(f)), memset(g, 0, sizeof(g));
for (int i = 1, cnt = 0; i <= n; i++) {
if (S[i] == k)
cnt = K, f[i] = 1;
else if (cnt)
cnt--, f[i] = 1;
}
for (int i = n, cnt = 0; i >= 1; i--) {
if (S[i] == k)
cnt = K, f[i] = 1;
else if (cnt)
cnt--, f[i] = 1;
}
for (int i = 1; i <= m; i++) g[i] = T[i] == k;
ntt(f, g);
for (int i = 0; i < lim; i++) A[i] += f[i];
}
for (int i = 0; i < lim; i++)
ans += A[i] == m;
cout << ans;
}

【CF528D】Fuzzy Search的更多相关文章

  1. 【CF528D】Fuzzy Search(FFT)

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

  2. 【Codeforces528D】Fuzzy Search FFT

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

  3. 【HDU2222】Keywords Search AC自动机

    [HDU2222]Keywords Search Problem Description In the modern time, Search engine came into the life of ...

  4. 【计算机视觉】Selective Search for Object Recognition论文阅读3

    Selective Search for Object Recoginition surgewong@gmail.com http://blog.csdn.net/surgewong       在前 ...

  5. 【HDU2222】Keywords Search(AC自动机)

    Problem Description In the modern time, Search engine came into the life of everybody like Google, B ...

  6. 【LeetCode】74. Search a 2D Matrix

    Difficulty:medium  More:[目录]LeetCode Java实现 Description Write an efficient algorithm that searches f ...

  7. 【codeforces 528D】 Fuzzy Search

    http://codeforces.com/problemset/problem/528/D (题目链接) 题意 给定母串和模式串,字符集大小为${4}$,给定${k}$,模式串在某个位置匹配当且仅当 ...

  8. 【知识】location.search获取中文时候会被编码成一串字符

    [转码] 例如:case.html?id='这个是页面的标题' 当想要使用location.search获取?id='这个是页面的标题'的时候,包含的中文会被编码成一串字符串. 所以我们需要进行解码, ...

  9. 【leetcode】Word Search

    Word Search Given a 2D board and a word, find if the word exists in the grid. The word can be constr ...

随机推荐

  1. c语言l博客作业08

    问题 答案 这个作业属于那个课程 C语言程序设计II 这个作业要求在哪里 https://edu.cnblogs.com/campus/zswxy/CST2019-2/homework/8655 我在 ...

  2. 开发者应该掌握 WebSocekt 协议的知识

    文章介绍 WebSocket 是一种在单个 TCP 连接上进行全双工通信的协议,它的出现使客户端和服务器之间的数据交换变得更加简单.WebSocket 通常被应用在实时性要求较高的场景,例如赛事数据. ...

  3. Xcode中.a文件引起的错误

    一.     TARGETS -> Build Settings-> Search Paths下 1.  Library Search Paths 删除不存在的路径,保留.a文件的路径(此 ...

  4. iOS开发-KVO的奥秘

    转自:http://www.jianshu.com/p/742b4b248da9 序言 在iOS开发中,苹果提供了许多机制给我们进行回调.KVO(key-value-observing)是一种十分有趣 ...

  5. git配置文件—— .gitattributes

    目录 .gitattributes 文档 1. gitattributes文件以行为单位设置一个路径下所有文件的属性,格式如下: 2. 在gitattributes文件的一行中,一个属性(以text属 ...

  6. 2019CCPC秦皇岛 J MUV LUV EXTRA(KMP)

    MUV LUV EXTRA Time Limit: 2000/1500 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others)T ...

  7. Dapr 运用之集成 Asp.Net Core Grpc 调用篇

    前置条件: <Dapr 运用> 改造 ProductService 以提供 gRPC 服务 从 NuGet 或程序包管理控制台安装 gRPC 服务必须的包 Grpc.AspNetCore ...

  8. 计算机二级Python

    概述 计算机二级在近两年新加了python的选择,趁机考了一下,顺便记录一下学习的一些所获 第一章 程序设计语言概述 考纲考点: 这一部分主要是介绍计算机语言的公共常识,一些尝试我就按照自己的理解方式 ...

  9. 这道Java基础题真的有坑!我求求你,认真思考后再回答。

    本文目录 一.题是什么题? 二.阿里Java开发规范. 2.1 正例代码. 2.2 反例代码. 三.层层揭秘,为什么发生异常了呢? 3.1 第一层:异常信息解读. 3.2 第二层:抛出异常的条件解读. ...

  10. 分享一个mysql服务启动与关闭的bat文件

    有时候打开数据库可视化工具(sqlyog.navicat)连接数据库时,会出现以下报错信息. 大家都知道是数据库的服务没有启动. 所以我想给大家分享一个bat文件可供快速启动mysql的数据库的服务, ...