【51nod】1565 模糊搜索
题解
这个字符集很小,我们可以把每个字符拿出来做一次匹配,把第一个字符串处理每个出现过的该字符处理成一个区间加,即最后变成第一个字符串的该位置能够匹配某字符
例如对于样例
10 4 1
AGCAATTCAT
ACAT
我们做A的时候,把第一个串处理成
AAAAAA00AA0
第二个串
A0A0
那么就变成第二个串从第一个串每个位置开始能不能匹配上第二个串所有的A了
我们发现把第二个串反序之后和第一个串求一个卷积,那么第一个串每个位置如果系数等于第二个串该字符出现次数,那么证明这个位置可以匹配一个第二个串的末尾
对于每个位置把所有字符集的答案&起来就好
代码
#include <iostream>
#include <algorithm>
#include <cstdio>
#include <cstring>
#include <vector>
#include <set>
#include <cmath>
#define enter putchar('\n')
#define space putchar(' ')
//#define ivorysi
#define pb push_back
#define MAXN 200005
#define pii pair<int,int>
#define mp make_pair
using namespace std;
typedef long long int64;
typedef double db;
template<class T>
void read(T &res) {
res = 0;char c = getchar();T f = 1;
while(c < '0' || c > '9') {
if(c == '-') f = -1;
c = getchar();
}
while(c >= '0' && c <= '9') {
res = res * 10 - '0' + c;
c = getchar();
}
res = res * f;
}
template<class T>
void out(T x) {
if(x < 0) {x = -x;putchar('-');}
if(x >= 10) out(x / 10);
putchar('0' + x % 10);
}
const db PI = acos(-1.0);
char s1[MAXN],s2[MAXN];
int K,S,T;
int a[MAXN],b[MAXN],c[MAXN];
bool vis[MAXN];
struct Complex {
db r,i;
Complex(db real = 0.0,db image = 0.0) {
r = real;i = image;
}
friend Complex operator + (const Complex &a,const Complex &b) {
return Complex(a.r + b.r,a.i + b.i);
}
friend Complex operator - (const Complex &a,const Complex &b) {
return Complex(a.r - b.r,a.i - b.i);
}
friend Complex operator * (const Complex &a,const Complex &b) {
return Complex(a.r * b.r - a.i * b.i,a.r * b.i + a.i * b.r);
}
}p1[MAXN * 4],p2[MAXN * 4];
void FFT(Complex *p,int L,int on) {
for(int i = 1 , j = L / 2 ; i < L - 1 ; ++i) {
if(i < j) swap(p[i],p[j]);
int k = L / 2;
while(j >= k) {
j -= k;
k >>= 1;
}
j += k;
}
for(int h = 2 ; h <= L ; h <<= 1) {
Complex wn = Complex(cos(on * 2 * PI / h),sin(on * 2 * PI / h));
for(int k = 0 ; k < L ; k += h) {
Complex w = Complex(1.0,0.0);
for(int j = k ; j < k + h / 2 ; ++j) {
Complex u = p[j],t = p[j + h / 2] * w;
p[j] = u + t;
p[j + h / 2] = u - t;
w = w * wn;
}
}
}
if(on == -1) {
for(int i = 0 ; i < L ; ++i) p[i].r /= L;
}
}
int ci(char c) {
if(c == 'A') return 1;
else if(c == 'G') return 2;
else if(c == 'C') return 3;
else return 4;
}
void Init() {
read(S);read(T);read(K);
scanf("%s%s",s1 + 1,s2 + 1);
for(int i = 1 ; i <= S ; ++i) a[i] = ci(s1[i]);
for(int i = 1 ; i <= T ; ++i) b[i] = ci(s2[i]);
}
void Solve() {
memset(vis,1,sizeof(vis));
for(int i = 1 ; i <= 4; ++i) {
int t = 1;
while(t <= S + T) t <<= 1;
int cnt = 0;
memset(c,0,sizeof(c));
for(int j = 1 ; j <= S ; ++j) {
if(a[j] == i) c[max(1,j - K)]++,c[min(j + K + 1,S + 1)]--;
}
for(int j = 1 ; j <= S ; ++j) {
c[j] += c[j - 1];
if(c[j]) p1[j - 1] = Complex(1.0,0.0);
else p1[j - 1] = Complex(0.0,0.0);
}
for(int j = S ; j < t ; ++j) p1[j] = Complex(0.0,0.0);
for(int j = 1 ; j <= T ; ++j) {
if(b[j] == i) p2[T - j] = Complex(1.0,0.0),++cnt;
else p2[T - j] = Complex(0.0,0.0);
}
for(int j = T ; j < t ; ++j) p2[j] = Complex(0.0,0.0);
FFT(p1,t,1);FFT(p2,t,1);
for(int j = 0 ; j < t ; ++j) p1[j] = p1[j] * p2[j];
FFT(p1,t,-1);
for(int j = 0 ; j < S ; ++j) {
if((int)(p1[j].r + 0.5) == cnt) vis[j] &= 1;
else vis[j] &= 0;
}
}
int ans = 0;
for(int i = 0 ; i < S ; ++i) if(vis[i]) ++ans;
out(ans);enter;
}
int main() {
#ifdef ivorysi
freopen("f1.in","r",stdin);
#endif
Init();
Solve();
}
【51nod】1565 模糊搜索的更多相关文章
- 51nod 1565模糊搜索(FFT)
题目大意就是字符串匹配,不过有一个门限k而已 之前有提到过fft做字符串匹配,这里和之前那种有些许不同 因为只有A,C,G,T四种字符,所以就考虑构造4个01序列 例如,模板串a关于'A'的01序列中 ...
- 51nod 1565 模糊搜索 FFT
这...好强啊\(QwQ\) 思路:卷积?\(FFT\)? 提交:\(5\)次 错因:一开始的预处理写错了(竟然只错了最后几个大点)闹得我以为\(FFT\)写挂了\(QwQ\) 题解: 对四种字符分开 ...
- 51NOD 1565:模糊搜索——题解
http://www.51nod.com/onlineJudge/questionCode.html#problemId=1565¬iceId=445588 有两个基因串S和T,他们只包 ...
- 51Nod 快速傅里叶变换题集选刷
打开51Nod全部问题页面,在右边题目分类中找到快速傅里叶变换,然后按分值排序,就是本文的题目顺序. 1.大数乘法问题 这个……板子就算了吧. 2.美妙的序列问题 长度为n的排列,且满足从中间任意位置 ...
- 【51Nod 1244】莫比乌斯函数之和
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1244 模板题... 杜教筛和基于质因子分解的筛法都写了一下模板. 杜教筛 ...
- BZOJ 1565: [NOI2009]植物大战僵尸
1565: [NOI2009]植物大战僵尸 Time Limit: 10 Sec Memory Limit: 64 MBSubmit: 2317 Solved: 1071[Submit][Stat ...
- 51Nod 1268 和为K的组合
51Nod 1268 和为K的组合 1268 和为K的组合 基准时间限制:1 秒 空间限制:131072 KB 分值: 20 难度:3级算法题 给出N个正整数组成的数组A,求能否从中选出若干个,使 ...
- 51Nod 1428 活动安排问题
51Nod 1428 活动安排问题 Link: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1428 1428 活 ...
- 51Nod 1278 相离的圆
51Nod 1278 相离的圆 Link: http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1278 1278 相离的圆 基 ...
随机推荐
- 安装VisualSVN Server 报错The specified TCP port is occupied
安装过程中报错,如下图所示. The specified TCP port is occupied by another service.Please stop that service or use ...
- 2017 清北济南考前刷题Day 4 morning
考场思路: 倒着算就是 可以对一个数-1 可以合并两个数 可以证明只有0和0才能执行合并操作 然后模拟 #include<cstdio> #include<iostream> ...
- UNDERSTANDING THE GAUSSIAN DISTRIBUTION
UNDERSTANDING THE GAUSSIAN DISTRIBUTION Randomness is so present in our reality that we are used to ...
- protobuffer
[protobuffer] 1.扩展名为.proto. 2.定义一个协议: 3.定义一个Service: 4.编译器为protoc,使用protoc: 5.style:所有的类型名均CamelCase ...
- bzoj 5055: 膜法师——树状数组
Description 在经历过1e9次大型战争后的宇宙中现在还剩下n个完美维度, 现在来自多元宇宙的膜法师,想偷取其中的三个维度为伟大的长者续秒, 显然,他能为长者所续的时间,为这三个维度上能量的乘 ...
- Tetrahedron(Codeforces Round #113 (Div. 2) + 打表找规律 + dp计数)
题目链接: https://codeforces.com/contest/166/problem/E 题目: 题意: 给你一个三菱锥,初始时你在D点,然后你每次可以往相邻的顶点移动,问你第n步回到D点 ...
- 程序移植到AUTOCAD2013笔记
1:需要引用acmgd.dll acdbmgd.dll,AcCoreMdg.dll, accui.dll 四个dll 2: 2010下的的acmgd.dll被拆分为acmgd.dll和AcCoreMd ...
- js_模块化
https://www.cnblogs.com/scq000/p/10647128.html
- 怎么样通过编写Python小程序来统计测试脚本的关键字
怎么样通过编写Python小程序来统计测试脚本的关键字 通常自动化测试项目到了一定的程序,编写的测试代码自然就会很多,如果很早已经编写的测试脚本现在某些基础函数.业务函数需要修改,那么势必要找出那些引 ...
- 【环境变量】Linux 下三种方式设置环境变量与获取环境变量
1.在Windows 系统下,很多软件安装都需要配置环境变量,比如 安装 jdk ,如果不配置环境变量,在非软件安装的目录下运行javac 命令,将会报告找不到文件,类似的错误. 2.那么什么是环境变 ...