Program time limit: 2 seconds
Program memory limit: 512 MB
Joceline has a grid of size $N \cdot M$ filled with letters of the alphabet. Each row except for the first can be left-shifted by some value $X$ - that is, replace the $i^{\text{th}}$ value with the $(i + X) \% M^{\text{th}}$ value.
You are given a dictionary of $D$ words, each $N$ long. The wordcount of the grid is the number of columns with valid words.
You are given $Q$ queries of the form:
The grid is reset after each query.
For all test cases:
For each query, output a single line containing the expected wordcount after the shifts as a fraction modulo $998244353$. Specifically, if the expected wordcount is $\frac{P}{Q}$ where $P$ and $Q$ are coprime integers, output $(P \cdot Q^{-1}) \bmod 998244353$ where $Q^{-1}$ is the modular inverse of $Q$ modulo $998244353$.
3 4
abcd
baca
bcdb
6
abb
acb
adb
cbb
aab
bcb
2
2 1
3 2
1
499122177
a
.a
.aa?
-> the only dictionary word that fits is aab
.Row 3 is random; probability it is b
: $2/4 = 1/2$.
Column 2:
b
.c
.bc?
-> the only dictionary word that fits is bcb
.Row 3 is random; probability it is b
: $2/4 = 1/2$.
Column 3:
c
.a
.Required pattern is ca?
-> no dictionary word matches. Probability: $0$.
Column 4:
d
.b
.db?
-> no dictionary word matches. Probability: $0$.Sum of probabilities: $1/2 + 1/2 + 0 + 0 = 1$. Answer: $1$.
a
.d
.Required pattern is a?d
-> no dictionary word matches. Probability: $0$.
Column 2:
b
.b
.b?b
-> the only dictionary word that fits is bcb
.Row 2 is random; probability it is c
: $1/4$.
Column 3:
c
.b
.c?b
→ the only dictionary word that fits is cbb
.Row 2 is random; probability it is b
: $1/4$.
Column 4:
d
.c
.d?c
-> no dictionary word matches. Probability: $0$.Sum of probabilities: $1/4 + 1/4 + 0 + 0 = 1/2$. Answer: $499122177$.
Log in to submit!