CF528D Fuzzy Search 【NTT】

题目链接

CF528D

题解

可以预处理出\(S\)每个位置能匹配哪些字符
对每种字符
构造两个序列
如果\(S[i]\)可以匹配该字符,则该位置为\(0\),否则为\(1\)
如果\(T[i]\)可以匹配该字符,则该位置为\(1\),否则为\(0\)
\(T\)翻转一下做卷积
如果某个字符意义下的某个位置为\(1\),就说明出现了\(T\)能匹配而\(S\)不能的情况,此时\(T\)不匹配\(S\)
否则\(T\)匹配\(S\)
即寻找有多少位置都为\(0\)

#include<algorithm>
#include<iostream>
#include<cstdlib>
#include<cstring>
#include<cstdio>
#include<vector>
#include<queue>
#include<cmath>
#include<map>
#define LL long long int
#define REP(i,n) for (int i = 1; i <= (n); i++)
#define Redge(u) for (int k = h[u],to; k; k = ed[k].nxt)
#define cls(s,v) memset(s,v,sizeof(s))
#define mp(a,b) make_pair<int,int>(a,b)
#define cp pair<int,int>
using namespace std;
const int maxn = 800005,maxm = 100005,INF = 0x3f3f3f3f;
inline int read(){
	int out = 0,flag = 1; char c = getchar();
	while (c < 48 || c > 57){if (c == '-') flag = 0; c = getchar();}
	while (c >= 48 && c <= 57){out = (out << 1) + (out << 3) + c - 48; c = getchar();}
	return flag ? out : -out;
}
const int G = 3,P = 998244353;
inline int qpow(int a,int b){
	int re = 1;
	for (; b; b >>= 1,a = 1ll * a * a % P)
		if (b & 1) re = 1ll * re * a % P;
	return re;
}
int R[maxn],c[maxn];
void NTT(int* a,int n,int f){
	for (int i = 0; i < n; i++) if (i < R[i]) swap(a[i],a[R[i]]);
	for (int i = 1; i < n; i <<= 1){
		int gn = qpow(G,(P - 1) / (i << 1));
		for (int j = 0; j < n; j += (i << 1)){
			int g = 1,x,y;
			for (int k = 0; k < i; k++,g = 1ll * g * gn % P){
				x = a[j + k],y = 1ll * g * a[j + k + i] % P;
				a[j + k] = (x + y) % P,a[j + k + i] = (x + P - y) % P;
			}
		}
	}
	if (f == 1) return;
	int nv = qpow(n,P - 2); reverse(a + 1,a + n);
	for (int i = 0; i < n; i++) a[i] = 1ll * a[i] * nv % P;
}
void conv(int* a,int* b,int deg1,int deg2){
	int n = 1,L = 0;
	while (n <= (deg1 + deg2)) n <<= 1,L++;
	for (int i = 1; i < n; i++) R[i] = (R[i >> 1] >> 1) | ((i & 1) << (L - 1));
	for (int i = 1; i <= deg2; i++) c[i] = b[i];
	for (int i = deg2 + 1; i < n; i++) c[i] = 0; c[0] = 0;
	NTT(a,n,1); NTT(c,n,1);
	for (int i = 0; i < n; i++) a[i] = 1ll * a[i] * c[i] % P;
	NTT(a,n,-1);
}
int A[4][maxn],B[4][maxn];
char ss[maxn],tt[maxn];
int N,M,K,last[4],S[maxn],T[maxn];
int id(char c){
	if (c == 'A') return 0;
	if (c == 'C') return 1;
	if (c == 'G') return 2;
	return 3;
}
int main(){
	N = read(); M = read(); K = read();
	scanf("%s%s",ss + 1,tt + 1); reverse(tt + 1,tt + 1 + M);
	REP(i,N) S[i] = id(ss[i]);
	REP(i,M) T[i] = id(tt[i]);
	REP(i,N) for (int j = 0; j < 4; j++) A[j][i] = 1; 
	for (int i = 1; i <= N; i++){
		last[S[i]] = i;
		for (int j = 0; j < 4; j++){
			if (last[j] && i - last[j] <= K) A[j][i] = 0;
		}
	}
	for (int j = 0; j < 4; j++) last[j] = 0;
	for (int i = N; i; i--){
		last[S[i]] = i;
		for (int j = 0; j < 4; j++){
			if (last[j] && last[j] - i <= K) A[j][i] = 0;
		}
	}
	for (int i = 1; i <= M; i++){
		for (int j = 0; j < 4; j++){
			if (T[i] == j) B[j][i] = 1;
			else B[j][i] = 0;
		}
	}
	for (int j = 0; j < 4; j++) conv(A[j],B[j],N,M);
	int ans = 0;
	for (int i = 1 + M; i <= N + 1; i++){
		int flag = 1;
		for (int j = 0; j < 4; j++) if (A[j][i]){flag = 0; break;}
		ans += flag;
	}
	printf("%d\n",ans);
	return 0;
}

posted @ 2018-07-14 15:45  Mychael  阅读(265)  评论(0编辑  收藏  举报