Blog of RuSun

\begin {array}{c} \mathfrak {One Problem Is Difficult} \\\\ \mathfrak {Because You Don't Know} \\\\ \mathfrak {Why It Is Diffucult} \end {array}

P4173 残缺的字符串

P4173 残缺的字符串

考虑构造多项式表达是否匹配。
$$
ANS _ {st} = \sum _ {i = st} ^ {st + n - 1} (A _ {i - st} - B _ i) ^ 2 opA_ {i -st} opB_ i
$$
其中
$$
op_c = \begin {cases}
0 , c = * \\
1, else
\end {cases}
$$

注意到 $i - (i - st) = st$ 与 $i$ 无关,考虑将 $B$ 翻转。

展开式子:
$$
\begin {aligned}
ANS _ {st} = & \sum _ {i = st} ^ {st + n - 1} (A _ {i - st} - B _ {n - i - 1}) ^ 2 opA_ {i -st} opB_ {n - i - 1} \\
= & \sum _ {i = st} ^ {st + n - 1} A ^ 2_ {i - st}opA_ {i - st} opB_ {n - i - 1} - 2 A_ {i - st} opA_ {i - st} B_ {n - i - 1} opB _ {n - i - 1} + B ^ 2 _ {n - i - 1} opA_ {i - st} opB_ {n - i - 1}
\end {aligned}
$$
将 $A ^ 2 op A, opB, AopA, BopB, B ^ 2opB, opA$ 分别做 ntt 。最后合起来做逆变换。

查看代码
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
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;
template <class Type>
void read(Type &x)
{
char c;
bool flag = false;
while ((c = getchar()) < '0' || c > '9')
c == '-' && (flag = true);
x = c - '0';
while ((c = getchar()) >= '0' && c <= '9')
x = (x << 3) + (x << 1) + c - '0';
if (flag) x = ~x + 1;
}
template <class Type, class ...rest>
void read(Type &x, rest &...y) { read(x), read(y...); }
template <class Type>
void write(Type x)
{
if (x < 0) putchar('-'), x = ~x + 1;
if (x > 9) write(x / 10);
putchar(x % 10 + '0');
}
typedef long long LL;
const int N = 12e5 + 10, mod = 998244353;
int rev[N], g[N];
int n, m, s[N], t[N], so[N], to[N], s1o[N], t1o[N], s2o[N], t2o[N];
int get ()
{
char c = getchar();
while (c != '*' && !(c >= 'a' && c <= 'z'))
c = getchar();
return c == '*' ? 0 : c - 'a' + 1;
}
int binpow (int b, int k = mod - 2)
{
int res = 1;
for (; k; k >>= 1, b = (LL)b * b % mod)
if (k & 1) res = (LL)res * b % mod;
return res;
}
void ntt (int *x, int bit, int op)
{
int tot = 1 << bit;
for (int i = 0; i < tot; ++i)
if ((rev[i] = rev[i >> 1] >> 1 | ((i & 1) << bit - 1)) > i)
swap(x[i], x[rev[i]]);
for (int mid = 1; mid < tot; mid <<= 1)
{
int w1 = binpow(3, (mod - 1) / (mid << 1));
if (!~op) w1 = binpow(w1);
for (int i = 0; i < tot; i += mid << 1)
for (int j = 0, k = 1; j < mid; ++j, k = (LL)k * w1 % mod)
{
int p = x[i + j], q = (LL)k * x[i + j + mid] % mod;
x[i + j] = (p + q) % mod, x[i + j + mid] = (p - q) % mod;
}
}
if (~op) return;
int itot = binpow(tot);
for (int i = 0; i < tot; ++i) x[i] = (LL)x[i] * itot % mod;
}
int main ()
{
read(n, m);
for (int i = 0; i < n; ++i)
s[i] = get(), so[i] = s[i] > 0, s2o[i] = s[i] * s[i] * so[i];
for (int i = m - 1; ~i; --i)
t[i] = get(), to[i] = t[i] > 0, t2o[i] = t[i] * t[i] * to[i];
int bit = 1;
while (n + m - 1 > 1 << bit) ++bit;
ntt(s, bit, 1), ntt(t, bit, 1), ntt(so, bit, 1), ntt(to, bit, 1), ntt(s2o, bit, 1), ntt(t2o, bit, 1);
for (int i = 0; i < 1 << bit; ++i)
g[i] = ((LL)s2o[i] * to[i] - 2ll * s[i] * t[i] + (LL)t2o[i] * so[i]) % mod;
ntt(g, bit, -1);
vector <int> ans;
for (int i = 0; i + n - 1 < m; ++i)
if (!g[m - i - 1]) ans.push_back(i);
write(ans.size()), puts("");
for (int i : ans) write(i + 1), putchar(' ');
return 0;
}