题目链接:点击查看

题目大意:给出一个长度为 nnn 的字符串 sss 和一个长度为 mmm 的字符串 ttt,问字符串 sss 有哪些子串可以匹配 ttt。给出一个参数 kkk,TTT 在 SSS 的第 iii 个位置中出现,当且仅当把 TTT 的首字符和 SSS 的第 iii 个字符对齐后,TTT 中的每一个字符能够在 SSS 中找到一个位置偏差不超过 kkk 的相同字符。

需要注意的是,在字符串 sss 中通过偏差匹配的字符可以重复与 ttt 匹配

题目分析:预处理出数组 g[i][j]g[i][j]g[i][j],代表字符串 sss 的第 iii 个位置可以放置字符 jjj,然后就是套路了:枚举每个字符,将字符串 ttt 反转,用 NTTNTTNTT 处理出数组 f(x)f(x)f(x) 代表子串 s[x−m+1:x]s[x-m+1:x]s[x−m+1:x] 与字符串 ttt 可以匹配的位置,最后需要统计 f(i)=mf(i)=mf(i)=m 的个数

代码:

// Problem: D. Fuzzy Search
// Contest: Codeforces - Codeforces Round #296 (Div. 1)
// URL: https://codeforces.com/contest/528/problem/D
// Memory Limit: 256 MB
// Time Limit: 3000 ms
//
// Powered by CP Editor (https://cpeditor.org)// #pragma GCC optimize(2)
// #pragma GCC optimize("Ofast","inline","-ffast-math")
// #pragma GCC target("avx,sse2,sse3,sse4,mmx")
#include<iostream>
#include<cstdio>
#include<string>
#include<ctime>
#include<cmath>
#include<cstring>
#include<algorithm>
#include<stack>
#include<climits>
#include<queue>
#include<map>
#include<set>
#include<sstream>
#include<cassert>
#include<bitset>
#include<list>
#include<unordered_map>
#define lowbit(x) (x&-x)
using namespace std;
typedef long long LL;
typedef unsigned long long ull;
template<typename T>
inline void read(T &x)
{T f=1;x=0;char ch=getchar();while(0==isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}while(0!=isdigit(ch)) x=(x<<1)+(x<<3)+ch-'0',ch=getchar();x*=f;
}
template<typename T>
inline void write(T x)
{if(x<0){x=~(x-1);putchar('-');}if(x>9)write(x/10);putchar(x%10+'0');
}
const int inf=0x3f3f3f3f;
const int N=2e5+100;
const int mod=998244353,G=3,Gi=332748118;
int n,m,k,limit=1,L,r[N<<2];
LL a[N<<2],b[N<<2],f[N<<2];
char s[N],t[N];
inline LL fastpow(LL a, LL k) {LL base = 1;while(k) {if(k & 1) base = (base * a ) % mod;a = (a * a) % mod;k >>= 1;}return base % mod;
}
inline void NTT(LL *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) {    LL Wn = fastpow( type == 1 ? G : Gi , (mod - 1) / (mid << 1));for(int j = 0; j < limit; j += (mid << 1)) {LL w = 1;for(int k = 0; k < mid; k++, w = (w * Wn) % mod) {int x = A[j + k], y = w * A[j + k + mid] % mod;A[j + k] = (x + y) % mod,A[j + k + mid] = (x - y + mod) % mod;}}}
}
void init() {limit=1,L=0;while(limit<=n+m) limit<<=1,L++;for(int i=0;i<limit;i++) r[i]=(r[i>>1]>>1)|((i&1)<<(L-1));
}
void solve(char ch) {for(int i=0;i<limit;i++) {a[i]=b[i]=0;}int last=-inf;for(int i=0;i<n;i++) {if(s[i]==ch) {last=i;}if(i-last<=k) {a[i]=1;}}last=inf;for(int i=n-1;i>=0;i--) {if(s[i]==ch) {last=i;}if(last-i<=k) {a[i]=1;}}for(int i=0;i<m;i++) {b[i]=t[i]==ch;}NTT(a,1),NTT(b,1);for(int i=0;i<limit;i++) {f[i]=(f[i]+a[i]*b[i])%mod;}
}
int main()
{#ifndef ONLINE_JUDGE
//  freopen("data.in.txt","r",stdin);
//  freopen("data.out.txt","w",stdout);
#endif
//  ios::sync_with_stdio(false);read(n),read(m),read(k);init();scanf("%s%s",s,t);reverse(t,t+m);solve('A'),solve('C'),solve('G'),solve('T');NTT(f,-1);LL inv=fastpow(limit,mod-2);for(int i=0;i<=n+m;i++) {f[i]=(f[i]*inv)%mod;}int ans=0;for(int i=m-1;i<n;i++) {ans+=f[i]==m;}cout<<ans<<endl;return 0;
}

CodeForces - 528D Fuzzy Search(多项式匹配字符串)相关推荐

  1. Codeforces Round #296 (Div. 1) D. Fuzzy Search FFT匹配字符串

    传送门 文章目录 题意: 思路: 题意: n,m,k≤2e5n,m,k\le2e5n,m,k≤2e5 思路: 直接考虑fftfftfft来匹配字符串. 由于kkk是给定的,所以难度低了很多,普通的字符 ...

  2. codeforces 528D. Fuzzy Search 快速傅里叶变换

    题目链接 D. Fuzzy Search time limit per test 3 seconds memory limit per test 256 megabytes input standar ...

  3. HDU多校3 - 6975 Forgiving Matching(多项式匹配字符串)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的字符串 sss 和一个长度为 mmm 的字符串 ttt.规定 kkk 匹配的意思是,两个长度相同的字符串至多有 kkk 个位置是不同的,特别的, ...

  4. 洛谷 - P4173 残缺的字符串(多项式匹配字符串-NTT)

    题目链接:点击查看 题目大意:给出一个长度为 nnn 的字符串 sss 和一个长度为 mmm 的字符串 ttt,都含有通配符 '*',现在问字符串 ttt 可以匹配字符串 nnn 的哪些位置 题目分析 ...

  5. python re.search模糊匹配字符串/找出含有指定某几个/多个字符串的文件

    paths = ['bbb','bbb123ccc'] result = [] for fname in paths:match1 = re.search('bbb', fname)match2 = ...

  6. python 字符匹配_python 中如何匹配字符串

    python 中如何匹配字符串? 1. re.match 尝试从字符串的起始位置匹配一个模式,如果不是起始位置匹配成功的话,match()就返回none.import re line="th ...

  7. 汇编实验2.2 查找匹配字符串(附有详细注释和源代码和相关知识)

    实验2.2 查找匹配字符串 实验要求: 程序接收用户键入的一个关键字以及一个句子.如果句子中不包含关键字则显示'No match!';如果句子中包含关键字则显示'Match',且把该字在句子中的位置用 ...

  8. [基础语法] SEARCH搜索指定字符串详解

    今天遇到一个问题:读取服务器目录返回文件夹信息,如果文件夹不存在就创建,下面是返回的信息 文件夹是以日期.日期的前六位命名的,此例中如果我们直接search 201503,虽然成功,但我们不能判断是不 ...

  9. re匹配字符串的中间一段_爬虫利器之 re 模块

    无情未必真豪杰,怜子如何不丈夫 鲁迅 前言 对于文本的过滤或者规则的匹配,最强大的就是正则表达式. 正则表达式,又称规则表达式,通常被用来检索.替换那些符合某个模式(规则)的文本. 正则表达式是对字符 ...

最新文章

  1. Failed to instantiate one or more classes
  2. java反射构造函数_【译】3. Java反射——构造函数
  3. 前端利用JS导出数据到Excel表 数字是文本类型 无法计算
  4. e0266 cout 不明确_荐书 | 不正义的时代,识别不正义的多重面孔
  5. check_mysql 脚本_如何使用myisamchk和mysqlcheck工具快速修复损坏的MySQL数据库文件
  6. JustForFly struts2标签s:generator
  7. Google 编程之夏:海量优质项目,丰厚报酬,你竟然还不知道?
  8. Python环境搭建及PyCharm下载安装
  9. vue项目整合到springboot方法
  10. 泡泡龙游戏开发系列教程(二)
  11. isc dhcp 服务器性能,DHCP 服务器搭建问题
  12. Codecademy-中文JavaScript系列教程-初认JS
  13. (转)Intel Atom处理器详细指标及市场前景
  14. SSH 端口转发与 SOCKS 代理
  15. 新茶饮迎来新玩家,柠檬茶酸涩难甜
  16. PDF - 使用 Adobe Acrobat 压缩 PDF 大小
  17. 解决echarts的title和legend重合问题(转)
  18. linux c计算时间差值,获取时间和计算时间差的几种方法总结,时间差几种方法...
  19. Android上传蒲公英平台脚本
  20. CSP题目:小明种苹果树

热门文章

  1. 新手指南:我应该学哪种编程语言?
  2. 如何干掉网易云音乐?这儿有个可行性未知的技术方案
  3. java param request_使用@RequestParam将请求参数绑定至方法参数
  4. springboot日志处理
  5. Spring FactoryBean的开发1
  6. Zookeeper基于Java访问-节点事件监听
  7. MybatisPlus添加操作
  8. 线程池原理与自定义线程池
  9. 上传图片-服务端-创建文件系统服务工程
  10. 循环结构_while循环