「Template」通配符单模式串匹配 - FFT

带通配符单模式串匹配

n300000n \le 300000

Luogu P4173 残缺的字符串

Solution

令通配符=0

若字符a,ba,b匹配,则(ab)2ab=0(a-b)^2ab=0,又因为(ab)2ab(a-b)^2ab为非负整数,所以只需要=0\sum=0

直接把模式串翻转,用ff来判断是否匹配

f[i]=j=0i(A[j]B[ij])2A[j]B[ij]=j=0iA[j]3B[ij]2j=0iA[j]2B[ij]2+j=0iA[j]B[ij]3\begin{aligned} f[i] &= \sum_{j=0}^{i}(A[j] - B[i - j])^2A[j]B[i - j]\\\\ &= \sum_{j=0}^{i}A[j]^3B[i - j] - 2\sum_{j=0}^{i}A[j]^2B[i - j]^2 + \sum_{j=0}^{i}A[j]B[i - j]^3 \end{aligned}

做三次FFT即可

考虑到FFT常数太大,我们只需要先做6次DFT,最后IDFT一次即可

Code

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
#include <bits/stdc++.h>

#define x first
#define y second
#define y1 Y1
#define y2 Y2
#define mp make_pair
#define pb push_back

using namespace std;

typedef long long LL;
typedef pair <int, int> pii;

template <typename T> inline int Chkmax (T &a, T b) { return a < b ? a = b, 1 : 0; }
template <typename T> inline int Chkmin (T &a, T b) { return a > b ? a = b, 1 : 0; }
template <typename T> inline T read ()
{
T sum = 0, fl = 1; char ch = getchar();
for (; !isdigit(ch); ch = getchar()) if (ch == '-') fl = -1;
for (; isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) + ch - '0';
return sum * fl;
}

inline void proc_status ()
{
ifstream t ("/proc/self/status");
cerr << string (istreambuf_iterator <char> (t), istreambuf_iterator <char> ()) << endl;
}

const int Maxn = 1500000;
const double eps = 0.5, PI = acos(-1.0);

int N, M;
double A[Maxn], B[Maxn], C[Maxn], D[Maxn];
char S[Maxn];

struct Complex
{
double x, y;

inline Complex operator + (const Complex &rhs) const { return (Complex){x + rhs.x, y + rhs.y}; }
inline Complex operator - (const Complex &rhs) const { return (Complex){x - rhs.x, y - rhs.y}; }
inline Complex operator * (const Complex &rhs) const { return (Complex){x * rhs.x - y * rhs.y, x * rhs.y + y * rhs.x}; }
inline Complex operator * (const double &rhs) const { return (Complex){x * rhs, y * rhs}; }
} Ans[Maxn];

namespace Poly
{
int n, rev[Maxn];
Complex F[Maxn], G[Maxn];

inline void init (int N, int M)
{

n = 1; while (n < N + M) n <<= 1;
for (int i = 0; i < n; ++i) rev[i] = (rev[i >> 1] >> 1) + (i & 1 ? (n >> 1) : 0);
}

inline void DFT (Complex A[], int flag)
{
for (int i = 0; i < n; ++i) if (rev[i] < i) swap(A[i], A[rev[i]]);

for (int mid = 1; mid < n; mid <<= 1)
{
Complex Wn = (Complex){cos(PI / mid), sin(PI / mid) * flag};

for (int i = 0; i < n; i += (mid << 1))
{
Complex W = (Complex){1, 0};
for (int j = i; j < i + mid; ++j, W = W * Wn)
{
Complex a = A[j], b = W * A[j + mid];
A[j] = a + b, A[j + mid] = a - b;
}
}
}

if (flag < 0) for (int i = 0; i < n; ++i) A[i].x /= n;
}

inline void FFT (double A[], int N, double B[], int M)
{
/**/ for (int i = 0; i < n; ++i) F[i] = (Complex){i < N ? A[i] : 0, 0};
for (int i = 0; i < n; ++i) G[i] = (Complex){i < M ? B[i] : 0, 0};

DFT(F, 1), DFT(G, 1);
for (int i = 0; i < n; ++i) F[i] = F[i] * G[i];
}

inline void Work ()
{
init(N, M);
for (int i = 0; i < N; ++i) C[i] = A[i] * A[i] * A[i];
for (int i = 0; i < M; ++i) D[i] = B[i];
FFT (C, N, D, M);
for (int i = 0; i < n; ++i) Ans[i] = Ans[i] + F[i];

for (int i = 0; i < N; ++i) C[i] = A[i] * A[i];
for (int i = 0; i < M; ++i) D[i] = B[i] * B[i];
FFT (C, N, D, M);
for (int i = 0; i < n; ++i) Ans[i] = Ans[i] - F[i] * 2.0;

for (int i = 0; i < N; ++i) C[i] = A[i];
for (int i = 0; i < M; ++i) D[i] = B[i] * B[i] * B[i];
FFT (C, N, D, M);
for (int i = 0; i < n; ++i) Ans[i] = Ans[i] + F[i];

DFT(Ans, -1);
// for (int i = 0; i < n; ++i) printf("%.0lf ", Ans[i].x); puts("");
}
}

vector <int> p;

inline void Solve ()
{
Poly :: Work ();
for (int i = N - 1; i < M; ++i) if (fabs(Ans[i].x) < eps) p.pb(i - N + 2);

cout<<p.size()<<endl;
for (int i = 0; i < p.size(); ++i) printf("%d ", p[i]);
}

inline void Input ()
{
N = read<int>(), M = read<int>();
scanf("%s", S); for (int i = 0; i < N; ++i) if (S[i] != '*') A[i] = S[i] - 'a' + 1;
scanf("%s", S); for (int i = 0; i < M; ++i) if (S[i] != '*') B[i] = S[i] - 'a' + 1;
reverse(A, A + N);
}

int main()
{
#ifndef ONLINE_JUDGE
freopen("A.in", "r", stdin);
freopen("A.out", "w", stdout);
#endif
Input();
Solve();
return 0;
}

Debug

  • 84L:i < N写成i <= N
感谢你的赞赏!