https://www.luogu.org/problem/P4173
题意:很简单,不说了。
做法:这道题就是FFT在字符串匹配中的应用。
单模式串匹配中的一种。首先简单说一下,不带通配符的单模式串匹配算法。
可以用KMP,或者哈希。
这里我们不用这个。对这道题貌似没有帮助
我们定义一个匹配函数,对于A串的x位和B串的y位:
,如果A串的x位和B串的y位的字符相同,则这个函数为零。
一般我们加一个平方,防止出现负数。
如果B串是模式串长度为l则:
,就是对于A以第x位结束的连续m位,与B匹配,如果等于0则说明完全匹配。我们现在还观察不出什么。我们吧这个多项式打开看看:
这个式子,我们可以得到,前面的一部分可以前缀平方和预处理得出来,中间的一部分直接预处理出来。
而最后面的一部分直接吧B串翻转,就变成了经典的FFT问题了:
,这个不就是卷积吗
所以
我们只需要做两次FFT和一次IFFT就可以了总时间复杂度(虽然没有KMP快,但不过接下来的可以是很有用的)
对于带有通配符的字符串匹配,我们将通配符定义为零。
则匹配函数为:
上面的D(x)则重新变成:
多项式打开看看:
这个和上面一样吧B串翻转,然后就成了卷积了,自己yy吧,难得打公式了
所以直接就是6次FFT然后一次IFFT,然后判断D(x)有没有零就可以了。
总时间复杂度O(nlogn)只不过常数略微大一点而已。
在洛谷上要开O2优化才能过,NTT稍微快一点。
FFT:
// luogu-judger-enable-o2
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
const int N = 2003000 + 10;
const double PI = acos(-1.0);
struct Complex {
double x, y;
Complex(double xx = 0, double yy = 0) { x = xx, y = yy; }
};
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); }
Complex operator/(Complex a, int l) { return Complex(a.x / l, a.y / l); }
Complex A[N], B[N], P[N];
char sa[N], sb[N];
int r[N], tot, lim, a[N], b[N];
void fft(Complex *a, int inv) {
for (int i = 0; i < tot; i++) {
if (i < r[i]) swap(a[i], a[r[i]]);
}
for (int l = 2; l <= tot; l <<= 1) {
int m = l / 2;
Complex wn = Complex(cos(PI / m), inv * sin(PI / m));
for (int j = 0; j < tot; j += l) {
Complex w = Complex(1.0, 0);
for (int i = 0; i < m; i++, w = w * wn) {
Complex tmp = w * a[i + j + m];
a[i + j + m] = a[i + j] - tmp;
a[i + j] = a[i + j] + tmp;
}
}
}
if (inv == -1) {
for (int i = 0; i < tot; i++) a[i] = a[i] / tot;
}
}
vector<int> ans;
void match(int n, int m) {
tot = 1, lim = 0;
while (tot < n + m) tot <<= 1, lim++;
for (int i = 0; i < tot; i++) {
r[i] = (r[i >> 1] >> 1) | ((i & 1) << (lim - 1));
}
for (int i = n - 1; i >= 0; i--) A[n - i - 1] = Complex(a[i] * a[i] * a[i]);
for (int i = 0; i < m; i++) B[i] = Complex(b[i]);
fft(A, 1);
fft(B, 1);
for (int i = 0; i < tot; i++) P[i] = A[i] * B[i];
for (int i = 0; i < tot; i++) A[i] = B[i] = Complex();
for (int i = n - 1; i >= 0; i--) A[n - i - 1] = Complex(a[i]);
for (int i = 0; i < m; i++) B[i] = Complex(b[i] * b[i] * b[i]);
fft(A, 1);
fft(B, 1);
for (int i = 0; i < tot; i++) P[i] = P[i] + A[i] * B[i];
for (int i = 0; i < tot; i++) A[i] = B[i] = Complex();
for (int i = n - 1; i >= 0; i--) A[n - i - 1] = Complex(a[i] * a[i]);
for (int i = 0; i < m; i++) B[i] = Complex(b[i] * b[i]);
fft(A, 1);
fft(B, 1);
for (int i = 0; i < tot; i++) P[i] = P[i] - A[i] * B[i] * 2;
fft(P, -1);
for (int i = n - 1; i < m; i++) {
if (fabs(P[i].x) < 0.5) ans.push_back(i - n + 2);
}
printf("%d\n", ans.size());
for (auto u:ans) {
printf("%d ", u);
}
puts("");
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
scanf("%s%s", sa, sb);
for (int i = 0; i < n; i++) {
if (sa[i] != '*') a[i] = sa[i] - 'a' + 1;
else a[i] = 0;
}
for (int i = 0; i < m; i++) {
if (sb[i] != '*') b[i] = sb[i] - 'a' + 1;
else b[i] = 0;
}
match(n, m);
return 0;
}
NTT:
// luogu-judger-enable-o2
#include "bits/stdc++.h"
using namespace std;
typedef long long ll;
#define reg register
const int N = 1600000 + 10;
const double PI = acos(-1.0);
const ll mod = 998244353;
ll A[N], B[N], P[N], g = 3;
char sa[N], sb[N];
int r[N], tot, lim, a[N], b[N];
ll Mod(ll x) {
if (x >= mod) x -= mod;
return x;
}
ll quick(ll a, ll n) {
ll ans = 1;
while (n) {
if (n & 1) ans = ans * a % mod;
n >>= 1;
a = a * a % mod;
}
return ans;
}
void ntt(ll *a, int inv) {
for (int i = 0; i < tot; i++) {
if (i < r[i]) swap(a[i], a[r[i]]);
}
for (int l = 2; l <= tot; l <<= 1) {
ll tmp = quick(g, (mod - 1) / l);
if (inv == -1) tmp = quick(tmp, mod - 2);
int m = l / 2;
for (int j = 0; j < tot; j += l) {
ll w = 1;
for (int i = 0; i < m; i++) {
ll t = 1LL * a[j + i + m] * w % mod;
a[j + i + m] = Mod(a[j + i] - t + mod);
a[j + i] = Mod(a[j + i] + t);
w = 1LL * w * tmp % mod;
}
}
}
if (inv == -1) {
ll t = quick(tot, mod - 2);
for (int i = 0; i < tot; i++) {
a[i] = 1LL * a[i] * t % mod;
}
}
}
int ans[N], cnt = 0;
void match(int n, int m) {
tot = 1, lim = 0;
while (tot < n + m) tot <<= 1, lim++;
for (int i = 0; i < tot; i++) {
r[i] = (r[i >> 1] >> 1) | ((i & 1) << (lim - 1));
}
for (int i = n - 1; i >= 0; i--) A[n - i - 1] = a[i] * a[i] * a[i];
for (int i = 0; i < m; i++) B[i] = b[i];
ntt(A, 1);
ntt(B, 1);
for (int i = 0; i < tot; i++) P[i] = A[i] * B[i] % mod;
for (int i = 0; i < tot; i++) A[i] = B[i] = 0;
for (int i = n - 1; i >= 0; i--) A[n - i - 1] = a[i];
for (int i = 0; i < m; i++) B[i] = b[i] * b[i] * b[i];
ntt(A, 1);
ntt(B, 1);
for (int i = 0; i < tot; i++) P[i] = Mod(P[i] + A[i] * B[i] % mod);
for (int i = 0; i < tot; i++) A[i] = B[i] = 0;
for (int i = n - 1; i >= 0; i--) A[n - i - 1] = a[i] * a[i];
for (int i = 0; i < m; i++) B[i] = b[i] * b[i];
ntt(A, 1);
ntt(B, 1);
for (int i = 0; i < tot; i++) P[i] = Mod(P[i] - A[i] * B[i] * 2 % mod + mod);
ntt(P, -1);
for (int i = n - 1; i < m; i++) {
if (P[i] == 0) ans[++cnt] = i - n + 2;
}
printf("%d\n", cnt);
for (int i = 1; i <= cnt; i++) printf("%d ", ans[i]);
printf("\n");
}
int main() {
int n, m;
scanf("%d%d", &n, &m);
scanf("%s%s", sa, sb);
for (int i = 0; i < n; i++) {
if (sa[i] != '*') a[i] = sa[i] - 'a' + 1;
}
for (int i = 0; i < m; i++) {
if (sb[i] != '*') b[i] = sb[i] - 'a' + 1;
}
match(n, m);
return 0;
}