DOJ logoDOJ
Problems
Recently added problemsSaved problems
CategoriesDLCContestsGroupsTeamsBoard
Ranking
Solved Problem RankingStreak rankingDOJ Power ranking
Status
Call for TasksLoginRegister
DOJ logoDOJ

Community online judge with OI-style subtask scoring, Elo ratings, and Discord-backed certification.

Problems
ProblemsCategories
Community
ContestsRankingBoardNoticesHost an external contestGuide
Account
LoginRegisterJudge Status
Policy
Terms of ServiceDLC RulesPrivacyAPI documentationLanguage runtimesContest rank rewardsRating and performance
© 2026 DOJ
All rights reserved
Editorial

DA.DA.S

Back to problem

Editorial

First, fix a string XXX. Scan it from left to right, and choose a substring as soon as one of the given strings ends. Then repeat the process using only the characters to the right of the chosen substring. This greedy strategy of choosing the earliest-ending occurrence computes f(X)f(X)f(X).

Represent this process with an Aho--Corasick automaton. Insert the given strings into a trie, then build the failure links and transitions for every letter. Mark a state if a given string ends there, including strings found through failure links. This is a Boolean mark: duplicate given strings do not increase it.

If reading a letter ccc from state uuu reaches a marked state, choose one substring and return to the root. Otherwise, remain in the reached state. These rules determine the next state and whether the number of chosen substrings increases by one for each letter transition. Returning to the root discards the information read through the end of the chosen substring, preventing the next choice from overlapping it.

Since the process never remains in a marked state, exclude such states from the matrix. Let KKK be the number of remaining states, and number them from 000 through K−1K-1K−1, with state 000 being the root.

Now process all strings of a given length together. Let aua_uau​ be the number of strings reaching state uuu, and let zzz be the sum of the numbers of chosen substrings over all these strings. Let Bu,vB_{u,v}B be the number of letters that take state to state under the rules above, and let be the number of letters that cause a substring to be chosen. Appending one letter gives

av′=∑u=0K−1auBu,v,z′=26z+∑u=0K−1auhu.a'_v=\sum_{u=0}^{K-1}a_u B_{u,v},\qquad z'=26z+\sum_{u=0}^{K-1}a_u h_u.av′​=

Every existing string has 262626 extensions, so its previous number of chosen substrings contributes to each extension, giving the term 26z26z26z.

These equations define a (K+1)×(K+1)(K+1)\times(K+1)(K+1)×(K+1) transition matrix for the row vector (a0,a1,…,aK−1,z)(a_0,a_1,\ldots,a_{K-1},z)(a. Put in the upper-left block and in the first entries of the last column. The last row is zero except for its last entry, which is .

The empty string reaches the root and has no chosen substrings, so the initial vector is (1,0,…,0)(1,0,\ldots,0)(1,0,…,0). Compute the MMM-th power of the transition matrix by binary exponentiation, multiply the initial vector by it, and output the last coordinate. Perform all calculations modulo 998 244 353998\ 244\ 353998 244 353.

Let L=∑i=1N∣Si∣L=\sum_{i=1}^{N}|S_i|L=∑i=1N​∣Si​∣. The automaton has at most L states, and at least one state is marked, so . The time complexity is , and the space complexity is .

Proof

Among the valid substrings in the remaining portion of the string, let III be an occurrence ending as early as possible. Let JJJ be the first occurrence in an optimal selection. The end of III is no later than the end of JJJ. Replacing JJJ with II therefore does not overlap any subsequent chosen occurrence. Thus, there is an optimal selection starting with . Repeating this argument on the portion to its right proves the greedy strategy optimal. If no valid occurrence exists, neither strategy chooses anything.

Solution written by GPT6

u,v
​
uuu
vvv
huh_uhu​
u=0
∑
K−1
​
au​
Bu,v​
,
z′
=
26z+
u=0∑K−1​au​hu​.
0
​
,
a1​
,
…
,
aK−1​
,
z
)
BBB
K×KK\times KK×K
huh_uhu​
KKK
262626
+1L+1
L+1
K≤LK\leq LK≤L
O(26L+L3log⁡(M+1))O(26L+L^3\log(M+1))O(26L+L3log(M+1))
O(L2)O(L^2)O(L2)
I
III

The automaton state represents the longest suffix of the characters read since the last choice that is also a trie prefix. Propagating marks through failure links detects every given string ending at the current position. Choosing one occurrence at the first marked state and returning to the root follows exactly the greedy strategy above. If several strings end at the same position, only one is chosen. The remaining portion is the same whichever of those occurrences is chosen.

Assume that aua_uau​ and zzz correctly describe all strings of some length. Each pair consisting of an existing string and a letter produces exactly one next string, so summing the counts over transitions gives av′a'_vav′​. Every extension inherits the previous number of choices, contributing 26z26z26z in total. For each string at state uuu, exactly huh_uhu​ letters add one new choice, contributing ∑uauhu\sum_u a_u h_u∑u​au​hu​ in total. Hence, both transition equations preserve their definitions at the next length.

The initial vector correctly describes the empty string. After MMM transitions, its last coordinate is the sum of the greedy counts over all strings of length MMM. Since the greedy count equals f(X)f(X)f(X) for every string, this coordinate is the required answer.