Reichman University and University of Haifa, [email protected]://orcid.org/0009-0007-8895-4069supported by Israel Science Foundation grant 810/21. Reichman University and University of Haifa, [email protected]://orcid.org/0000-0001-8357-2802supported by Israel Science Foundation grant 810/21. Bar Ilan University, [email protected]://orcid.org/0000-0002-7812-3399supported by the ERC grant MPM under the EU’s Horizon 2020 Research and Innovation Programme (grant no. 683064) and by the State of Israel through the Center for Absorption in Science of the Ministry of Aliyah and Immigration. \CopyrightJane Open Access and Joan R. Public \ccsdesc[500]Theory of computation Pattern matching \hideLIPIcs

String 2-Covers with No Length Restrictions

Itai Boneh    Shay Golan    Arseny Shur
Abstract

A λ𝜆\lambdaitalic_λ-cover of a string S𝑆Sitalic_S is a set of strings {Ci}1λsuperscriptsubscriptsubscript𝐶𝑖1𝜆\{C_{i}\}_{1}^{\lambda}{ italic_C start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT } start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT start_POSTSUPERSCRIPT italic_λ end_POSTSUPERSCRIPT such that every index in S𝑆Sitalic_S is contained in an occurrence of at least one string Cisubscript𝐶𝑖C_{i}italic_C start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT. The existence of a 1111-cover defines a well-known class of quasi-periodic strings. Quasi-periodicity can be decided in linear time, and all 1111-covers of a string can be reported in linear time plus the size of the output. Since in general it is NP-complete to decide whether a string has a λ𝜆\lambdaitalic_λ-cover, the natural next step is the development of efficient algorithms for 2222-covers. Radoszewski and Straszyński [ESA 2020] analysed the particular case where the strings in a 2222-cover must be of the same length. They provided an algorithm that reports all such 2222-covers of S𝑆Sitalic_S in time near-linear in |S|𝑆|S|| italic_S | and in the size of the output.

In this work, we consider 2222-covers in full generality. Since every length-n𝑛nitalic_n string has Ω(n2)Ωsuperscript𝑛2\Omega(n^{2})roman_Ω ( italic_n start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT ) trivial 2222-covers (every prefix and suffix of total length at least n𝑛nitalic_n constitute such a 2222-cover), we state the reporting problem as follows: given a string S𝑆Sitalic_S and a number m𝑚mitalic_m, report all 2222-covers {C1,C2}subscript𝐶1subscript𝐶2\{C_{1},C_{2}\}{ italic_C start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT , italic_C start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT } of S𝑆Sitalic_S with length |C1|+|C2|subscript𝐶1subscript𝐶2|C_{1}|+|C_{2}|| italic_C start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT | + | italic_C start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT | upper bounded by m𝑚mitalic_m. We present an O~(n+𝗈𝗎𝗍𝗉𝗎𝗍)~𝑂𝑛𝗈𝗎𝗍𝗉𝗎𝗍\tilde{O}(n+\mathsf{output})over~ start_ARG italic_O end_ARG ( italic_n + sansserif_output ) time algorithm solving this problem, with 𝗈𝗎𝗍𝗉𝗎𝗍𝗈𝗎𝗍𝗉𝗎𝗍\mathsf{output}sansserif_output being the size of the output. This algorithm admits a simpler modification that finds a 2222-cover of minimum length. We also provide an O~(n)~𝑂𝑛\tilde{O}(n)over~ start_ARG italic_O end_ARG ( italic_n ) time construction of a 2222-cover oracle which, given two substrings C1,C2subscript𝐶1subscript𝐶2C_{1},C_{2}italic_C start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT , italic_C start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT of S𝑆Sitalic_S, reports in poly-logarithmic time whether {C1,C2}subscript𝐶1subscript𝐶2\{C_{1},C_{2}\}{ italic_C start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT , italic_C start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT } is a 2222-cover of S𝑆Sitalic_S.

keywords:
Quasi-periodicity, String cover, Range query
category:
\relatedversion

1 Introduction

For a string S𝑆Sitalic_S, the substring C𝐶Citalic_C of S𝑆Sitalic_S is a cover of S𝑆Sitalic_S if every index of S𝑆Sitalic_S is covered by an occurrence of C𝐶Citalic_C. Since the introduction of covers by Apostolico and Ehrenfeucht [4], many algorithms have been developed for finding covers or variations of covers of a given string.  [4] presented an O(nlog2n)𝑂𝑛superscript2𝑛O(n\log^{2}n)italic_O ( italic_n roman_log start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT italic_n ) time algorithm for finding all covers of an input string of length n𝑛nitalic_n. It was shown by Moore and Smyth [23, 24] that all covers of a string can be reported in O(n)𝑂𝑛O(n)italic_O ( italic_n ) time. Smyth [28] further extended this result by showing that the covers of all prefixes of S𝑆Sitalic_S can be computed in O(n)𝑂𝑛O(n)italic_O ( italic_n ) time. Further works on covers and variants of cover include [1, 19, 7, 2, 3, 16, 6, 25].

A natural generalization of a cover is a λ𝜆\lambdaitalic_λ-cover. A set of strings {C1,C2,,Cλ}subscript𝐶1subscript𝐶2subscript𝐶𝜆\{C_{1},C_{2},\ldots,C_{\lambda}\}{ italic_C start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT , italic_C start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT , … , italic_C start_POSTSUBSCRIPT italic_λ end_POSTSUBSCRIPT } is a λ𝜆\lambdaitalic_λ-cover of S𝑆Sitalic_S if every index in S𝑆Sitalic_S is covered by an occurrence of Cisubscript𝐶𝑖C_{i}italic_C start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT for some i[λ]𝑖delimited-[]𝜆i\in[\lambda]italic_i ∈ [ italic_λ ]. The notion of λ𝜆\lambdaitalic_λ-covers was introduced by Guo, Zhang and Iliopoulos [17, 30], who proposed an O(n2)𝑂superscript𝑛2O(n^{2})italic_O ( italic_n start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT ) time algorithm for computing all λ𝜆\lambdaitalic_λ-covers of a string S𝑆Sitalic_S over a constant size alphabet for a given λ𝜆\lambdaitalic_λ. The running time analysis of their algorithm was later found to be faulty by Czajka and Radoszewski [11], who showed that it has an exponential worst-case running time. Cole et al. [9] justified the lack of a polynomial algorithm for computing all λ𝜆\lambdaitalic_λ-covers by proving that the problem is NP-complete.

In this work, we focus on 2222-covers. Radoszewski and Straszyński [26] have considered a special case of “balanced” 2222-covers, where the two strings composing the 2222-cover have equal length. They proposed an O~(n+𝗈𝗎𝗍𝗉𝗎𝗍)~𝑂𝑛𝗈𝗎𝗍𝗉𝗎𝗍\tilde{O}(n+\mathsf{output})over~ start_ARG italic_O end_ARG ( italic_n + sansserif_output )-time algorithm reporting all balanced 2222-covers of a given length-n𝑛nitalic_n string S𝑆Sitalic_S. They also provide two versions of this algorithm, one of which finds a balanced 2222-cover of each possible length and the other determines the shortest balanced 2222-cover of S𝑆Sitalic_S; both versions work in O~(n)~𝑂𝑛\tilde{O}(n)over~ start_ARG italic_O end_ARG ( italic_n ) time.

Designing efficient algorithms for the same problems on 2222-covers in the general case is posed in [26] as an open problem.

For a 2222-cover {C1,C2}subscript𝐶1subscript𝐶2\{C_{1},C_{2}\}{ italic_C start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT , italic_C start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT }, its length is |C1|+|C2|subscript𝐶1subscript𝐶2|C_{1}|+|C_{2}|| italic_C start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT | + | italic_C start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT |. Following the open problem of Radoszewski and Straszyński, we specify the following problems for 2222-covers in the general case:

  • All_2-covers(S,m)𝑆𝑚(S,m)( italic_S , italic_m ): for a string S𝑆Sitalic_S, report all 2222-covers of length at most m𝑚mitalic_m;

  • Shortest_2-cover(S)𝑆(S)( italic_S ): for a string S𝑆Sitalic_S, find a 2222-cover of minimum length;

  • 2-cover_Oracle(S)𝑆(S)( italic_S ): for a string S𝑆Sitalic_S, build a data structure that answers the queries of the form “do given two substrings of S𝑆Sitalic_S constitute a 2222-cover of S𝑆Sitalic_S?”

Note that every length-n𝑛nitalic_n string S𝑆Sitalic_S trivially has Ω(n2)Ωsuperscript𝑛2\Omega(n^{2})roman_Ω ( italic_n start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT ) 2222-covers. E. g., if C1subscript𝐶1C_{1}italic_C start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT is a prefix of S𝑆Sitalic_S, C2subscript𝐶2C_{2}italic_C start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT is a suffix of S𝑆Sitalic_S, and |C1|+|C2|nsubscript𝐶1subscript𝐶2𝑛|C_{1}|+|C_{2}|\geq n| italic_C start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT | + | italic_C start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT | ≥ italic_n, then {C1,C2}subscript𝐶1subscript𝐶2\{C_{1},C_{2}\}{ italic_C start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT , italic_C start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT } is a 2222-cover of S𝑆Sitalic_S. The length restriction made in the above formulation of the All_2-covers problem allows one to consider instances with smaller outputs, thus returning the running times of type O~(n+𝗈𝗎𝗍𝗉𝗎𝗍)~𝑂𝑛𝗈𝗎𝗍𝗉𝗎𝗍\tilde{O}(n+\mathsf{output})over~ start_ARG italic_O end_ARG ( italic_n + sansserif_output ) into the game.

Our contribution.

In this work, we solve the three above problems in near linear time.

Theorem 1.1.

There exists an algorithm that solves All_2-covers(S,m)𝑆𝑚(S,m)( italic_S , italic_m ) in O(nlog5n+𝗈𝗎𝗍𝗉𝗎𝗍log3n)𝑂𝑛superscript5𝑛𝗈𝗎𝗍𝗉𝗎𝗍superscript3𝑛O(n\log^{5}n+\mathsf{output}\cdot\log^{3}n)italic_O ( italic_n roman_log start_POSTSUPERSCRIPT 5 end_POSTSUPERSCRIPT italic_n + sansserif_output ⋅ roman_log start_POSTSUPERSCRIPT 3 end_POSTSUPERSCRIPT italic_n ) time.

Theorem 1.2.

There exists an algorithm that solves Shortest_2-cover(S)𝑆(S)( italic_S ) in O(nlog4n)𝑂𝑛superscript4𝑛O(n\log^{4}n)italic_O ( italic_n roman_log start_POSTSUPERSCRIPT 4 end_POSTSUPERSCRIPT italic_n ) time.

Theorem 1.3.

There exists an algorithm that solves 2-cover_Oracle(S)𝑆(S)( italic_S ) in O(nlog5n)𝑂𝑛superscript5𝑛O(n\log^{5}n)italic_O ( italic_n roman_log start_POSTSUPERSCRIPT 5 end_POSTSUPERSCRIPT italic_n ) preprocessing time and O(log3n)𝑂superscript3𝑛O(\log^{3}n)italic_O ( roman_log start_POSTSUPERSCRIPT 3 end_POSTSUPERSCRIPT italic_n ) query time.

Techniques and Ideas.

The main idea of our algorithms is the formulation of the property “an index i𝑖iitalic_i in S𝑆Sitalic_S is covered by an occurrence of a substring U𝑈Uitalic_U of S𝑆Sitalic_S” in terms of point location. At a high level, each index i𝑖iitalic_i is assigned a compactly representable area 𝒜isubscript𝒜𝑖\mathcal{A}_{i}caligraphic_A start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT in the plane, and every substring U𝑈Uitalic_U that is not highly periodic corresponds to a point pUsubscript𝑝𝑈p_{U}italic_p start_POSTSUBSCRIPT italic_U end_POSTSUBSCRIPT in the plane such that pU𝒜isubscript𝑝𝑈subscript𝒜𝑖p_{U}\in\mathcal{A}_{i}italic_p start_POSTSUBSCRIPT italic_U end_POSTSUBSCRIPT ∈ caligraphic_A start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT if and only if the index i𝑖iitalic_i is covered by some occurrence of U𝑈Uitalic_U. The same idea, implemented in three dimensions instead of two, covers the case of highly periodic substrings.

Given such a geometric representation, our algorithms make use of multi-dimensional range-reporting and range-stabbing data structures to retrieve and organize the areas associated with each index in S𝑆Sitalic_S. This organization facilitates the computation of a core set of the 2222-covers, which consists of pairs of strings that are not highly periodic. This set provides a solution to the Shortest_2-cover problem. Besides that, we create the oracle solving the 2-cover_Oracle problem and utilize the core set to finalize our solution to the All_2-covers problem with a small number of queries to this oracle.

Organization.

In Section 2 we present notation, auxiliary lemmas, and pre-existing data structures that are used in our algorithms. In Section 3 we formalize and prove the connection between covering an index in a string by an occurrence of a substring and multidimensional point location. In Section 4 we build upon the insights presented in Section 3 to design the 2222-cover oracle and prove Theorem 1.3. Finally, in Section 5 we present the reporting algorithms proving Theorem 1.2 and Theorem 1.1 (the latter one executes the oracle). All details omitted due to space constraints are put into Appendix.

2 Preliminaries

Here we present definitions, notation, and auxiliary lemmas. For completeness, proofs of the statements appearing in this section are given in Appendix B.

We assume in this paper that 000\in\mathbb{N}0 ∈ blackboard_N. We denote [x..y]={ixiy}[x..y]=\{i\in\mathbb{N}\mid x\leq i\leq y\}[ italic_x . . italic_y ] = { italic_i ∈ blackboard_N ∣ italic_x ≤ italic_i ≤ italic_y } for any real numbers x,y𝑥𝑦x,yitalic_x , italic_y, possibly negative. We also denote [x]=[1..x][x]=[1..x][ italic_x ] = [ 1 . . italic_x ]. The notation 𝗈𝗎𝗍𝗉𝗎𝗍𝗈𝗎𝗍𝗉𝗎𝗍\mathsf{output}sansserif_output stands for the size of output of a reporting algorithm.

All strings in the paper are over an alphabet Σ={1,2,O(nc)}Σ12𝑂superscript𝑛𝑐\Sigma=\{1,2,\ldots O(n^{c})\}roman_Σ = { 1 , 2 , … italic_O ( italic_n start_POSTSUPERSCRIPT italic_c end_POSTSUPERSCRIPT ) } for some constant c𝑐citalic_c. The letters of a string S𝑆Sitalic_S are indexed from 1 to |S|𝑆|S|| italic_S |. If X=S[i..j]X=S[i..j]italic_X = italic_S [ italic_i . . italic_j ], X𝑋Xitalic_X is called a substring of S𝑆Sitalic_S (a prefix of S𝑆Sitalic_S if i=1𝑖1i=1italic_i = 1, a suffix of S𝑆Sitalic_S if j=|S|𝑗𝑆j=|S|italic_j = | italic_S |, and an empty string if i>j𝑖𝑗i>jitalic_i > italic_j). We also say that S[i..j]S[i..j]italic_S [ italic_i . . italic_j ] specifies an occurrence of X𝑋Xitalic_X at position i𝑖iitalic_i. If X𝑋Xitalic_X is a substring of S𝑆Sitalic_S, then S𝑆Sitalic_S is a superstring of X𝑋Xitalic_X. A string X𝑋Xitalic_X that occurs both as a prefix and as a suffix of S𝑆Sitalic_S is a border of S𝑆Sitalic_S. A string S𝑆Sitalic_S has period ρ𝜌\rhoitalic_ρ if S[i]=S[i+ρ]𝑆delimited-[]𝑖𝑆delimited-[]𝑖𝜌S[i]=S[i+\rho]italic_S [ italic_i ] = italic_S [ italic_i + italic_ρ ] for all i[|S|ρ]𝑖delimited-[]𝑆𝜌i\in[|S|-\rho]italic_i ∈ [ | italic_S | - italic_ρ ]. Clearly, S𝑆Sitalic_S has period ρ𝜌\rhoitalic_ρ if and only if S[1..|S|ρ]S[1..|S|-\rho]italic_S [ 1 . . | italic_S | - italic_ρ ] is a border of S𝑆Sitalic_S. The minimal period of S𝑆Sitalic_S is denoted by 𝗉𝖾𝗋(S)𝗉𝖾𝗋𝑆\mathsf{per}(S)sansserif_per ( italic_S ). Let 𝗉𝖾𝗋(S)=ρ𝗉𝖾𝗋𝑆𝜌\mathsf{per}(S)=\rhosansserif_per ( italic_S ) = italic_ρ. We say that S𝑆Sitalic_S is aperiodic if |S|<2ρ𝑆2𝜌|S|<2\rho| italic_S | < 2 italic_ρ, (ρ𝜌\rhoitalic_ρ-)periodic if |S|2ρ𝑆2𝜌|S|\geq 2\rho| italic_S | ≥ 2 italic_ρ, and highly (ρ𝜌\rhoitalic_ρ-)periodic if |S|3ρ𝑆3𝜌|S|\geq 3\rho| italic_S | ≥ 3 italic_ρ. We say that S𝑆Sitalic_S is short (ρ𝜌\rhoitalic_ρ)-periodic if |S|[2ρ..3ρ1]𝑆delimited-[]2𝜌..3𝜌1|S|\in[2\rho..3\rho-1]| italic_S | ∈ [ 2 italic_ρ ..3 italic_ρ - 1 ]. Note that if a string X𝑋Xitalic_X occurs in S𝑆Sitalic_S at positions i𝑖iitalic_i and j𝑗jitalic_j, then |ji|𝗉𝖾𝗋(X)𝑗𝑖𝗉𝖾𝗋𝑋|j-i|\geq\mathsf{per}(X)| italic_j - italic_i | ≥ sansserif_per ( italic_X ).

The following two lemmas specify some useful structure of periodic prefixes and borders.

Lemma 2.1.

The prefixes of a length-n𝑛nitalic_n string have, in total, O(logn)𝑂𝑛O(\log n)italic_O ( roman_log italic_n ) different periods.

Lemma 2.2.

Every string of length n𝑛nitalic_n has O(logn)𝑂𝑛O(\log n)italic_O ( roman_log italic_n ) aperiodic borders and O(logn)𝑂𝑛O(\log n)italic_O ( roman_log italic_n ) short periodic borders.

We use well known notion of the longest common prefix (𝖫𝖢𝖯𝖫𝖢𝖯\mathsf{LCP}sansserif_LCP).

Definition 2.3.

For two strings S𝑆Sitalic_S and T𝑇Titalic_T, 𝖫𝖢𝖯(S,T)=max{S[1..]=T[1..]}\mathsf{LCP}(S,T)=\max\{\ell\mid S[1..\ell]=T[1..\ell]\}sansserif_LCP ( italic_S , italic_T ) = roman_max { roman_ℓ ∣ italic_S [ 1 . . roman_ℓ ] = italic_T [ 1 . . roman_ℓ ] } is the length of their longest common prefix and 𝖫𝖢𝖯R(S,T)=max{S[|S|+1..|S|]=T[|T|+1..|T|]}\mathsf{LCP}^{R}(S,T)=\max\{\ell\mid S[|S|-\ell+1..|S|]=T[|T|-\ell+1..|T|]\}sansserif_LCP start_POSTSUPERSCRIPT italic_R end_POSTSUPERSCRIPT ( italic_S , italic_T ) = roman_max { roman_ℓ ∣ italic_S [ | italic_S | - roman_ℓ + 1 . . | italic_S | ] = italic_T [ | italic_T | - roman_ℓ + 1 . . | italic_T | ] } is the length of their longest common suffix.

Covers.

Given a string S𝑆Sitalic_S, we say that a substring X𝑋Xitalic_X covers an index i𝑖iitalic_i if for some indices j1,j2[|S|]subscript𝑗1subscript𝑗2delimited-[]𝑆j_{1},j_{2}\in[|S|]italic_j start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT , italic_j start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ∈ [ | italic_S | ] we have X=S[j1..j2]X=S[j_{1}..j_{2}]italic_X = italic_S [ italic_j start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT . . italic_j start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ] and i[j1..j2]i\in[j_{1}..j_{2}]italic_i ∈ [ italic_j start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT . . italic_j start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ]. We also say that the occurrence of X𝑋Xitalic_X at j1subscript𝑗1j_{1}italic_j start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT covers i𝑖iitalic_i. If X𝑋Xitalic_X covers every i[|S|]𝑖delimited-[]𝑆i\in[|S|]italic_i ∈ [ | italic_S | ], we call X𝑋Xitalic_X a 1111-cover of S𝑆Sitalic_S. A pair of substrings (X,Y)𝑋𝑌(X,Y)( italic_X , italic_Y ) is said to cover i𝑖iitalic_i if X𝑋Xitalic_X or Y𝑌Yitalic_Y covers i𝑖iitalic_i. If a pair (X,Y)𝑋𝑌(X,Y)( italic_X , italic_Y ) covers every i[|S|]𝑖delimited-[]𝑆i\in[|S|]italic_i ∈ [ | italic_S | ], we call (X,Y)𝑋𝑌(X,Y)( italic_X , italic_Y ) a 2222-cover of S𝑆Sitalic_S. (It will be convenient to consider 2-covers as ordered pairs, though the notion of 2-cover is symmetric with respect to X𝑋Xitalic_X and Y𝑌Yitalic_Y.) We say that (X,Y)𝑋𝑌(X,Y)( italic_X , italic_Y ) is highly periodic if either X𝑋Xitalic_X or Y𝑌Yitalic_Y is highly periodic, otherwise (X,Y)𝑋𝑌(X,Y)( italic_X , italic_Y ) is non-highly periodic. The following lemma considers a periodic string that covers index i𝑖iitalic_i.

Lemma 2.4.

Let S𝑆Sitalic_S be a string, and let X𝑋Xitalic_X be a ρ𝜌\rhoitalic_ρ-periodic substring. If the string X𝑋Xitalic_X covers an index i𝑖iitalic_i, then the string X[1..|X|ρ]]X[1..|X|-\rho]]italic_X [ 1 . . | italic_X | - italic_ρ ] ] (= X[ρ+1..|X|]X[\rho+1..|X|]italic_X [ italic_ρ + 1 . . | italic_X | ]) also covers i𝑖iitalic_i.

Runs.

A ρ𝜌\rhoitalic_ρ-periodic substring of S𝑆Sitalic_S is a run if it is not contained in a longer ρ𝜌\rhoitalic_ρ-periodic substring. We use the following lemmas regarding runs.

Lemma 2.5.

Let S𝑆Sitalic_S be a string, ρ[|S|]𝜌delimited-[]𝑆\rho\in[|S|]italic_ρ ∈ [ | italic_S | ]. Every index i𝑖iitalic_i in S𝑆Sitalic_S is covered by at most two ρ𝜌\rhoitalic_ρ-periodic runs and by O(logn)𝑂𝑛O(\log n)italic_O ( roman_log italic_n ) highly-periodic runs.

Lemma 2.6.

Let S𝑆Sitalic_S be a string. For every ρ𝜌\rhoitalic_ρ-periodic substring S[i..j]S[i..j]italic_S [ italic_i . . italic_j ], there is a unique ρ𝜌\rhoitalic_ρ-periodic run containing S[i..j]S[i..j]italic_S [ italic_i . . italic_j ].

Lemma 2.7.

If there is an integer ρ𝜌\rhoitalic_ρ such that S[x..y]S[x..y]italic_S [ italic_x . . italic_y ] is ρ𝜌\rhoitalic_ρ-periodic and S[x..y+1]S[x..y+1]italic_S [ italic_x . . italic_y + 1 ] is not ρ𝜌\rhoitalic_ρ-periodic, then S[x..y+1]S[x..y+1]italic_S [ italic_x . . italic_y + 1 ] is aperiodic.

Lemma 2.8 ([5, Theorem 9]).

The number of runs in any string S𝑆Sitalic_S is smaller than |S|𝑆|S|| italic_S |.

2.1 Range Data Structures

Our algorithms use data structures for orthogonal range queries. Such a data structure is associated with a positive integer dimension d𝑑ditalic_d and deals with d𝑑ditalic_d-dimensional points and d𝑑ditalic_d-dimensional ranges. A d𝑑ditalic_d-dimensional point is a d𝑑ditalic_d-tuple p=(x1,x2,,xd)𝑝subscript𝑥1subscript𝑥2subscript𝑥𝑑p=(x_{1},x_{2},\ldots,x_{d})italic_p = ( italic_x start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT , italic_x start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT , … , italic_x start_POSTSUBSCRIPT italic_d end_POSTSUBSCRIPT ) and a d𝑑ditalic_d-dimensional range is the cartesian product R=[a1..b1]×[a2..b2]××[ad..bd]R=[a_{1}..b_{1}]\times[a_{2}..b_{2}]\times\ldots\times[a_{d}..b_{d}]italic_R = [ italic_a start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT . . italic_b start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT ] × [ italic_a start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT . . italic_b start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ] × … × [ italic_a start_POSTSUBSCRIPT italic_d end_POSTSUBSCRIPT . . italic_b start_POSTSUBSCRIPT italic_d end_POSTSUBSCRIPT ] of d𝑑ditalic_d ranges. We call a 2222-dimensional range a rectangle and a 3333-dimensional range a cuboid. We say that a point p𝑝pitalic_p is contained in the range R𝑅Ritalic_R (denoted by pR𝑝𝑅p\in Ritalic_p ∈ italic_R) if xi[ai..bi]x_{i}\in[a_{i}..b_{i}]italic_x start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT ∈ [ italic_a start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT . . italic_b start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT ] for every i[d]𝑖delimited-[]𝑑i\in[d]italic_i ∈ [ italic_d ].

We make use of the following range data structures.

Lemma 2.9 (Range Query Data Structure [29, 8]).

For any integer d𝑑ditalic_d, a set P𝑃Pitalic_P of n𝑛nitalic_n points in dsuperscript𝑑\mathbb{R}^{d}blackboard_R start_POSTSUPERSCRIPT italic_d end_POSTSUPERSCRIPT can be preprocessed in O(nlogd1n)𝑂𝑛superscript𝑑1𝑛O(n\log^{d-1}n)italic_O ( italic_n roman_log start_POSTSUPERSCRIPT italic_d - 1 end_POSTSUPERSCRIPT italic_n ) time to support the following queries.

  • Reporting: Given a d𝑑ditalic_d-dimensional range R𝑅Ritalic_R, output all points in the set PR𝑃𝑅P\cap Ritalic_P ∩ italic_R.

  • Emptiness: Given a d𝑑ditalic_d-dimensional range R𝑅Ritalic_R, report if PR=𝑃𝑅P\cap R=\emptysetitalic_P ∩ italic_R = ∅ or not.

The query time is O(logd1n)𝑂superscript𝑑1𝑛O(\log^{d-1}n)italic_O ( roman_log start_POSTSUPERSCRIPT italic_d - 1 end_POSTSUPERSCRIPT italic_n ) for Emptiness and O(logd1n+𝗈𝗎𝗍𝗉𝗎𝗍)𝑂superscript𝑑1𝑛𝗈𝗎𝗍𝗉𝗎𝗍O(\log^{d-1}n+\mathsf{output})italic_O ( roman_log start_POSTSUPERSCRIPT italic_d - 1 end_POSTSUPERSCRIPT italic_n + sansserif_output ) for Reporting.

Lemma 2.10 (Range stabbing queries [8, Theorems 5 and 7]).

For any integer d𝑑ditalic_d, a set of d𝑑ditalic_d-dimensional ranges R1,R2,Rnsubscript𝑅1subscript𝑅2subscript𝑅𝑛R_{1},R_{2},\ldots R_{n}italic_R start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT , italic_R start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT , … italic_R start_POSTSUBSCRIPT italic_n end_POSTSUBSCRIPT can be prepossessed in O(nlogd1n)𝑂𝑛superscript𝑑1𝑛O(n\log^{d-1}n)italic_O ( italic_n roman_log start_POSTSUPERSCRIPT italic_d - 1 end_POSTSUPERSCRIPT italic_n ) time to support the following queries.

  • Stabbing: Given a d𝑑ditalic_d-dimensional point p𝑝pitalic_p, report all ranges Risubscript𝑅𝑖R_{i}italic_R start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT such that pRi𝑝subscript𝑅𝑖p\in R_{i}italic_p ∈ italic_R start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT.

  • Existence: Given a d𝑑ditalic_d-dimensional point p𝑝pitalic_p, report if no ranges Risubscript𝑅𝑖R_{i}italic_R start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT satisfy pRi𝑝subscript𝑅𝑖p\in R_{i}italic_p ∈ italic_R start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT.

The query time is O(logd1n)𝑂superscript𝑑1𝑛O(\log^{d-1}n)italic_O ( roman_log start_POSTSUPERSCRIPT italic_d - 1 end_POSTSUPERSCRIPT italic_n ) for Existence and O(logd1n+𝗈𝗎𝗍𝗉𝗎𝗍)𝑂superscript𝑑1𝑛𝗈𝗎𝗍𝗉𝗎𝗍O(\log^{d-1}n+\mathsf{output})italic_O ( roman_log start_POSTSUPERSCRIPT italic_d - 1 end_POSTSUPERSCRIPT italic_n + sansserif_output ) for Stabbing.

2.2 Stringology Algorithms and Data Structures

Throughout the paper, we make use of the following string algorithms and data structures.

Lemma 2.11 (Pattern Matching [18]).

There exists an algorithm that, given a string T𝑇Titalic_T of length n𝑛nitalic_n and a string P𝑃Pitalic_P of length mn𝑚𝑛m\leq nitalic_m ≤ italic_n, reports in O(n)𝑂𝑛O(n)italic_O ( italic_n ) time all the occurrences of P𝑃Pitalic_P in T𝑇Titalic_T.

Lemma 2.12 (𝖫𝖢𝖯Ssubscript𝖫𝖢𝖯𝑆\mathsf{LCP}_{S}sansserif_LCP start_POSTSUBSCRIPT italic_S end_POSTSUBSCRIPT Data Structure [21, 15]).

There exists a data structure 𝖫𝖢𝖯Ssubscript𝖫𝖢𝖯𝑆\mathsf{LCP}_{S}sansserif_LCP start_POSTSUBSCRIPT italic_S end_POSTSUBSCRIPT that preprocesses an arbitrary string SΣ𝑆superscriptΣS\in\Sigma^{*}italic_S ∈ roman_Σ start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT of length n𝑛nitalic_n in O(n)𝑂𝑛O(n)italic_O ( italic_n ) time and supports constant-time queries 𝖫𝖢𝖯S(i,j)=𝖫𝖢𝖯(S[i..n],S[j..n])\mathsf{LCP}_{S}(i,j)=\mathsf{LCP}(S[i..n],S[j..n])sansserif_LCP start_POSTSUBSCRIPT italic_S end_POSTSUBSCRIPT ( italic_i , italic_j ) = sansserif_LCP ( italic_S [ italic_i . . italic_n ] , italic_S [ italic_j . . italic_n ] ) and 𝖫𝖢𝖯SR(i,j)=𝖫𝖢𝖯R(S[1..i],S[1..j])\mathsf{LCP}^{R}_{S}(i,j)=\mathsf{LCP}^{R}(S[1..i],S[1..j])sansserif_LCP start_POSTSUPERSCRIPT italic_R end_POSTSUPERSCRIPT start_POSTSUBSCRIPT italic_S end_POSTSUBSCRIPT ( italic_i , italic_j ) = sansserif_LCP start_POSTSUPERSCRIPT italic_R end_POSTSUPERSCRIPT ( italic_S [ 1 . . italic_i ] , italic_S [ 1 . . italic_j ] ).

When S𝑆Sitalic_S is clear from context, we simply write 𝖫𝖢𝖯(i,j)𝖫𝖢𝖯𝑖𝑗\mathsf{LCP}(i,j)sansserif_LCP ( italic_i , italic_j ) and 𝖫𝖢𝖯R(i,j)superscript𝖫𝖢𝖯𝑅𝑖𝑗\mathsf{LCP}^{R}(i,j)sansserif_LCP start_POSTSUPERSCRIPT italic_R end_POSTSUPERSCRIPT ( italic_i , italic_j ).

Lemma 2.13 (Internal Pattern Matching (𝖨𝖯𝖬𝖨𝖯𝖬\mathsf{IPM}sansserif_IPM[20]).

There exists a data structure 𝖨𝖯𝖬Ssubscript𝖨𝖯𝖬𝑆\mathsf{IPM}_{S}sansserif_IPM start_POSTSUBSCRIPT italic_S end_POSTSUBSCRIPT that preprocesses an arbitrary string SΣ𝑆superscriptΣS\in\Sigma^{*}italic_S ∈ roman_Σ start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT of length n𝑛nitalic_n in O(n)𝑂𝑛O(n)italic_O ( italic_n ) time and supports the following constant-time queries.

  • Periodic: given a substring X𝑋Xitalic_X, return 𝗉𝖾𝗋(X)𝗉𝖾𝗋𝑋\mathsf{per}(X)sansserif_per ( italic_X ) if X𝑋Xitalic_X is periodic, and “aperiodic” otherwise.

  • Internal Matching: Given two substrings X𝑋Xitalic_X and Y𝑌Yitalic_Y such that |Y|=O(|X|)𝑌𝑂𝑋|Y|=O(|X|)| italic_Y | = italic_O ( | italic_X | ), return all occurrences of X𝑋Xitalic_X in Y𝑌Yitalic_Y represented as O(1)𝑂1O(1)italic_O ( 1 ) arithmetic progressions.

Lemma 2.14 (Finding all Substrings, see [22]).

There is an algorithm that reports all distinct substrings of a string S[1..n]S[1..n]italic_S [ 1 . . italic_n ] in time O(n+𝗈𝗎𝗍𝗉𝗎𝗍)𝑂𝑛𝗈𝗎𝗍𝗉𝗎𝗍O(n+\mathsf{output})italic_O ( italic_n + sansserif_output ).

Lemma 2.15 (Finding all Runs [13, Theorem 1.4]).

There is algorithm that computes all runs of a string SΣ𝑆superscriptΣS\in\Sigma^{*}italic_S ∈ roman_Σ start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT of length n𝑛nitalic_n in O(n)𝑂𝑛O(n)italic_O ( italic_n ) time.

3 Range Characterization of Covering an Index

In this section we translate the property “an index is covered by an occurrence of a given substring” to the language of d𝑑ditalic_d-dimensional points and ranges. Then this property can be checked with the queries described in Lemmas 2.9 and 2.10. We distinguish between the 2222-dimensional case of not highly periodic substrings (Lemma 3.1) and 3-dimensional case of highly periodic substrings (Lemma 3.2). Given a point p𝑝pitalic_p and a set \mathcal{R}caligraphic_R of ranges (both in d𝑑ditalic_d dimensions), we slightly abuse the notation, writing p𝑝p\in\mathcal{R}italic_p ∈ caligraphic_R instead of pRR𝑝subscript𝑅𝑅p\in\bigcup_{R\in\mathcal{R}}Ritalic_p ∈ ⋃ start_POSTSUBSCRIPT italic_R ∈ caligraphic_R end_POSTSUBSCRIPT italic_R.

To present the algorithms that prove these lemmas, we first describe an O(nlog2n)𝑂𝑛superscript2𝑛O(n\log^{2}n)italic_O ( italic_n roman_log start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT italic_n ) time preprocessing phase. Throughout the rest of the paper, we assume that this preprocessing has already been executed.

Preprocessing.

The algorithm computes 𝖫𝖢𝖯Ssubscript𝖫𝖢𝖯𝑆\mathsf{LCP}_{S}sansserif_LCP start_POSTSUBSCRIPT italic_S end_POSTSUBSCRIPT data structure of Lemma 2.12 and 𝖨𝖯𝖬Ssubscript𝖨𝖯𝖬𝑆\mathsf{IPM}_{S}sansserif_IPM start_POSTSUBSCRIPT italic_S end_POSTSUBSCRIPT data structure of Lemma 2.13. In addition, the algorithm computes all runs of S𝑆Sitalic_S using Lemma 2.15. For every ρ[n]𝜌delimited-[]𝑛\rho\in[n]italic_ρ ∈ [ italic_n ], the algorithm stores all ρ𝜌\rhoitalic_ρ-periodic runs of S𝑆Sitalic_S in a 3333-dimensional range reporting data structure D𝗋𝗎𝗇ρsubscriptsuperscript𝐷𝜌𝗋𝗎𝗇D^{\rho}_{\mathsf{run}}italic_D start_POSTSUPERSCRIPT italic_ρ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT of Lemma 2.9 as follows. For every ρ𝜌\rhoitalic_ρ-periodic run S[..r]S[\ell..r]italic_S [ roman_ℓ . . italic_r ], the data structure D𝗋𝗎𝗇ρsubscriptsuperscript𝐷𝜌𝗋𝗎𝗇D^{\rho}_{\mathsf{run}}italic_D start_POSTSUPERSCRIPT italic_ρ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT contains the point p=(,r,r+1)𝑝𝑟𝑟1p=(\ell,r,r-\ell+1)italic_p = ( roman_ℓ , italic_r , italic_r - roman_ℓ + 1 ). By Lemma 2.8, the total number of runs stored in the structures D𝗋𝗎𝗇ρsubscriptsuperscript𝐷𝜌𝗋𝗎𝗇D^{\rho}_{\mathsf{run}}italic_D start_POSTSUPERSCRIPT italic_ρ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT over all ρ[n]𝜌delimited-[]𝑛\rho\in[n]italic_ρ ∈ [ italic_n ] is at most n𝑛nitalic_n. It follows from Lemmas 2.12, 2.13, 2.15 and 2.9 that the preprocessing time is O(nlog2n)𝑂𝑛superscript2𝑛O(n\log^{2}n)italic_O ( italic_n roman_log start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT italic_n ).

3.1 The Not Highly Periodic Case

In this section we prove the following lemma.

Lemma 3.1.

Let f,i[n]𝑓𝑖delimited-[]𝑛f,i\in[n]italic_f , italic_i ∈ [ italic_n ] be two indices and let k𝑘k\in\mathbb{N}italic_k ∈ blackboard_N. There exists a set \mathcal{R}caligraphic_R of O(1)𝑂1O(1)italic_O ( 1 ) rectangles such that for any ,r𝑟\ell,r\in\mathbb{N}roman_ℓ , italic_r ∈ blackboard_N with +r+1[1.5k..1.5k+1]𝑟1delimited-[]superscript1.5𝑘superscript..1.5𝑘1\ell+r+1\in[1.5^{k}..1.5^{k+1}]roman_ℓ + italic_r + 1 ∈ [ 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT ..1.5 start_POSTSUPERSCRIPT italic_k + 1 end_POSTSUPERSCRIPT ] the string 𝗌𝗎𝖻=S[f..f+r]\mathsf{sub}=S[f-\ell..f+r]sansserif_sub = italic_S [ italic_f - roman_ℓ . . italic_f + italic_r ] satisfies the following conditions:

  1. 1.

    If (,r)𝑟(\ell,r)\in\mathcal{R}( roman_ℓ , italic_r ) ∈ caligraphic_R, then 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub covers i𝑖iitalic_i and 𝗉𝖾𝗋(𝗌𝗎𝖻)1.5k4𝗉𝖾𝗋𝗌𝗎𝖻superscript1.5𝑘4\mathsf{per}(\mathsf{sub})\geq\frac{1.5^{k}}{4}sansserif_per ( sansserif_sub ) ≥ divide start_ARG 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT end_ARG start_ARG 4 end_ARG.

  2. 2.

    If 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub covers i𝑖iitalic_i and is not highly periodic, then (,r)𝑟(\ell,r)\in\mathcal{R}( roman_ℓ , italic_r ) ∈ caligraphic_R.

Moreover, \mathcal{R}caligraphic_R can be computed in O(log2n)𝑂superscript2𝑛O(\log^{2}n)italic_O ( roman_log start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT italic_n ) time.

For f[n]𝑓delimited-[]𝑛f\in[n]italic_f ∈ [ italic_n ] and k𝑘k\in\mathbb{N}italic_k ∈ blackboard_N, let 𝗌𝗎𝖻𝗅𝖾𝖿𝗍=S[f1.5k2..f]\mathsf{sub}_{\mathsf{left}}=S[f-\left\lfloor{\frac{1.5^{k}}{2}}\right\rfloor.% .f]sansserif_sub start_POSTSUBSCRIPT sansserif_left end_POSTSUBSCRIPT = italic_S [ italic_f - ⌊ divide start_ARG 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT end_ARG start_ARG 2 end_ARG ⌋ . . italic_f ], and 𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍=S[f..f+1.5k2]\mathsf{sub}_{\mathsf{right}}=S[f..f+\left\lfloor{\frac{1.5^{k}}{2}}\right\rfloor]sansserif_sub start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT = italic_S [ italic_f . . italic_f + ⌊ divide start_ARG 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT end_ARG start_ARG 2 end_ARG ⌋ ]. If an endpoint of a substring is outside S𝑆Sitalic_S, the substring is undefined.

{observation}

Let f[n]𝑓delimited-[]𝑛f\in[n]italic_f ∈ [ italic_n ] be an index and k𝑘k\in\mathbb{N}italic_k ∈ blackboard_N. For every 𝗌𝗎𝖻=S[f..f+r]\mathsf{sub}=S[f-\ell..f+r]sansserif_sub = italic_S [ italic_f - roman_ℓ . . italic_f + italic_r ] such that ,r𝑟\ell,r\in\mathbb{N}roman_ℓ , italic_r ∈ blackboard_N and |𝗌𝗎𝖻|[1.5k..1.5k+1]𝗌𝗎𝖻delimited-[]superscript1.5𝑘superscript..1.5𝑘1|\mathsf{sub}|\in[1.5^{k}..1.5^{k+1}]| sansserif_sub | ∈ [ 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT ..1.5 start_POSTSUPERSCRIPT italic_k + 1 end_POSTSUPERSCRIPT ], 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub is a superstring of either 𝗌𝗎𝖻𝗅𝖾𝖿𝗍subscript𝗌𝗎𝖻𝗅𝖾𝖿𝗍\mathsf{sub}_{\mathsf{left}}sansserif_sub start_POSTSUBSCRIPT sansserif_left end_POSTSUBSCRIPT or 𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍subscript𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍\mathsf{sub}_{\mathsf{right}}sansserif_sub start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT.

Section 3.1 allows us to prove Lemma 3.1 as follows. First we find a set 1subscript1\mathcal{R}_{1}caligraphic_R start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT that satisfies the conditions of the lemma for all pairs (,r)𝑟(\ell,r)( roman_ℓ , italic_r ) such that 𝗌𝗎𝖻=S[f..f+r]\mathsf{sub}=S[f-\ell..f+r]sansserif_sub = italic_S [ italic_f - roman_ℓ . . italic_f + italic_r ] is a superstring of 𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍subscript𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍\mathsf{sub}_{\mathsf{right}}sansserif_sub start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT. Similarly, we find a set 2subscript2\mathcal{R}_{2}caligraphic_R start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT for the case where 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub is a superstring of 𝗌𝗎𝖻𝗅𝖾𝖿𝗍subscript𝗌𝗎𝖻𝗅𝖾𝖿𝗍\mathsf{sub}_{\mathsf{left}}sansserif_sub start_POSTSUBSCRIPT sansserif_left end_POSTSUBSCRIPT. Then 12subscript1subscript2\mathcal{R}_{1}\cup\mathcal{R}_{2}caligraphic_R start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT ∪ caligraphic_R start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT is the set required by the lemma.

In the rest of the section we show how to find the set 1subscript1\mathcal{R}_{1}caligraphic_R start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT. (The argument for the set 2subscript2\mathcal{R}_{2}caligraphic_R start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT is similar, so we omit it.) Let us fix f,𝑓f,\ellitalic_f , roman_ℓ, and r1.5k2=|𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍|1𝑟superscript1.5𝑘2subscript𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍1r\geq\left\lfloor{\frac{1.5^{k}}{2}}\right\rfloor=|\mathsf{sub}_{\mathsf{right% }}|-1italic_r ≥ ⌊ divide start_ARG 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT end_ARG start_ARG 2 end_ARG ⌋ = | sansserif_sub start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT | - 1. Let i𝗋𝗂𝗀𝗁𝗍subscript𝑖𝗋𝗂𝗀𝗁𝗍i_{\mathsf{right}}italic_i start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT denote the starting index of an occurrence of 𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍subscript𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍\mathsf{sub}_{\mathsf{right}}sansserif_sub start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT. We make the following claim.

Claim 1.

There exists a rectangle R𝑅Ritalic_R such that for any ,r𝑟\ell,r\in\mathbb{N}roman_ℓ , italic_r ∈ blackboard_N with +r+1[1.5k..1.5k+1]𝑟1delimited-[]superscript1.5𝑘superscript..1.5𝑘1\ell+r+1\in[1.5^{k}..1.5^{k+1}]roman_ℓ + italic_r + 1 ∈ [ 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT ..1.5 start_POSTSUPERSCRIPT italic_k + 1 end_POSTSUPERSCRIPT ] and r1.5k2𝑟superscript1.5𝑘2r\geq\left\lfloor{\frac{1.5^{k}}{2}}\right\rflooritalic_r ≥ ⌊ divide start_ARG 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT end_ARG start_ARG 2 end_ARG ⌋ the substring 𝗌𝗎𝖻=S[f..f+r]\mathsf{sub}=S[f-\ell..f+r]sansserif_sub = italic_S [ italic_f - roman_ℓ . . italic_f + italic_r ] covers the index i𝑖iitalic_i with the occurrence at position i𝗋𝗂𝗀𝗁𝗍subscripti𝗋𝗂𝗀𝗁𝗍i_{\mathsf{right}}-\ellitalic_i start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT - roman_ℓ if and only if (,r)R𝑟𝑅(\ell,r)\in R( roman_ℓ , italic_r ) ∈ italic_R. Moreover, R𝑅Ritalic_R can be computed in O(1)𝑂1O(1)italic_O ( 1 ) time.

{claimproof}

Let er=𝖫𝖢𝖯(f,i𝗋𝗂𝗀𝗁𝗍)subscript𝑒𝑟𝖫𝖢𝖯𝑓subscript𝑖𝗋𝗂𝗀𝗁𝗍e_{r}=\mathsf{LCP}(f,i_{\mathsf{right}})italic_e start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT = sansserif_LCP ( italic_f , italic_i start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT ), e=𝖫𝖢𝖯R(f,i𝗋𝗂𝗀𝗁𝗍)subscript𝑒superscript𝖫𝖢𝖯𝑅𝑓subscript𝑖𝗋𝗂𝗀𝗁𝗍e_{\ell}=\mathsf{LCP}^{R}(f,i_{\mathsf{right}})italic_e start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT = sansserif_LCP start_POSTSUPERSCRIPT italic_R end_POSTSUPERSCRIPT ( italic_f , italic_i start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT ). Using 𝖫𝖢𝖯Ssubscript𝖫𝖢𝖯𝑆\mathsf{LCP}_{S}sansserif_LCP start_POSTSUBSCRIPT italic_S end_POSTSUBSCRIPT, we compute in O(1)𝑂1O(1)italic_O ( 1 ) time the rectangle R=[i𝗋𝗂𝗀𝗁𝗍i..e1]×[ii𝗋𝗂𝗀𝗁𝗍..er1]R=[i_{\mathsf{right}}-i\>..\>e_{\ell}-1]\times[i-i_{\mathsf{right}}\>..\>e_{r}% -1]italic_R = [ italic_i start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT - italic_i . . italic_e start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT - 1 ] × [ italic_i - italic_i start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT . . italic_e start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT - 1 ] and check the required conditions.

First assume (,r)R𝑟𝑅(\ell,r)\in R( roman_ℓ , italic_r ) ∈ italic_R. Since e1subscript𝑒1\ell\leq e_{\ell}-1roman_ℓ ≤ italic_e start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT - 1 and rer1𝑟subscript𝑒𝑟1r\leq e_{r}-1italic_r ≤ italic_e start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT - 1, one has 𝗌𝗎𝖻=S[f..f+r]=S[i𝗋𝗂𝗀𝗁𝗍..i𝗋𝗂𝗀𝗁𝗍+r]\mathsf{sub}=S[f-\ell..f+r]=S[i_{\mathsf{right}}-\ell..i_{\mathsf{right}}+r]sansserif_sub = italic_S [ italic_f - roman_ℓ . . italic_f + italic_r ] = italic_S [ italic_i start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT - roman_ℓ . . italic_i start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT + italic_r ], so 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub occurs at i𝗋𝗂𝗀𝗁𝗍subscript𝑖𝗋𝗂𝗀𝗁𝗍i_{\mathsf{right}}-\ellitalic_i start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT - roman_ℓ. Since i𝗋𝗂𝗀𝗁𝗍isubscript𝑖𝗋𝗂𝗀𝗁𝗍𝑖\ell\geq i_{\mathsf{right}}-iroman_ℓ ≥ italic_i start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT - italic_i and rii𝗋𝗂𝗀𝗁𝗍𝑟𝑖subscript𝑖𝗋𝗂𝗀𝗁𝗍r\geq i-i_{\mathsf{right}}italic_r ≥ italic_i - italic_i start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT, we have i[i𝗋𝗂𝗀𝗁𝗍..i𝗋𝗂𝗀𝗁𝗍+r]i\in[i_{\mathsf{right}}-\ell..i_{\mathsf{right}}+r]italic_i ∈ [ italic_i start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT - roman_ℓ . . italic_i start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT + italic_r ], so this occurrence covers i𝑖iitalic_i.

Now assume that 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub covers i𝑖iitalic_i with the occurrence at i𝗋𝗂𝗀𝗁𝗍subscript𝑖𝗋𝗂𝗀𝗁𝗍i_{\mathsf{right}}-\ellitalic_i start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT - roman_ℓ. Since 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub occurs at i𝗋𝗂𝗀𝗁𝗍subscript𝑖𝗋𝗂𝗀𝗁𝗍i_{\mathsf{right}}-\ellitalic_i start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT - roman_ℓ, one has e1subscript𝑒1\ell\leq e_{\ell}-1roman_ℓ ≤ italic_e start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT - 1 and rer1𝑟subscript𝑒𝑟1r\leq e_{r}-1italic_r ≤ italic_e start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT - 1. Since this occurrence covers i𝑖iitalic_i, one also has i[i𝗋𝗂𝗀𝗁𝗍..i𝗋𝗂𝗀𝗁𝗍+|𝗌𝗎𝖻|1]=[i𝗋𝗂𝗀𝗁𝗍..i𝗋𝗂𝗀𝗁𝗍+r]i\in[i_{\mathsf{right}}-\ell..i_{\mathsf{right}}-\ell+|\mathsf{sub}|-1]=[i_{% \mathsf{right}}-\ell..i_{\mathsf{right}}+r]italic_i ∈ [ italic_i start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT - roman_ℓ . . italic_i start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT - roman_ℓ + | sansserif_sub | - 1 ] = [ italic_i start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT - roman_ℓ . . italic_i start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT + italic_r ]. Then i𝗋𝗂𝗀𝗁𝗍isubscript𝑖𝗋𝗂𝗀𝗁𝗍𝑖\ell\geq i_{\mathsf{right}}-iroman_ℓ ≥ italic_i start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT - italic_i and rii𝗋𝗂𝗀𝗁𝗍𝑟𝑖subscript𝑖𝗋𝗂𝗀𝗁𝗍r\geq i-i_{\mathsf{right}}italic_r ≥ italic_i - italic_i start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT, which finally proves (,r)R𝑟𝑅(\ell,r)\in R( roman_ℓ , italic_r ) ∈ italic_R.

If 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub covers index i𝑖iitalic_i, its substring 𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍subscript𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍\mathsf{sub}_{\mathsf{right}}sansserif_sub start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT must occur close to i𝑖iitalic_i. Since |𝗌𝗎𝖻|1.5k+1𝗌𝗎𝖻superscript1.5𝑘1|\mathsf{sub}|\leq 1.5^{k+1}| sansserif_sub | ≤ 1.5 start_POSTSUPERSCRIPT italic_k + 1 end_POSTSUPERSCRIPT, if an occurrence of 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub at i𝗋𝗂𝗀𝗁𝗍subscript𝑖𝗋𝗂𝗀𝗁𝗍i_{\mathsf{right}}-\ellitalic_i start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT - roman_ℓ covers i𝑖iitalic_i, then the position i𝗋𝗂𝗀𝗁𝗍subscript𝑖𝗋𝗂𝗀𝗁𝗍i_{\mathsf{right}}italic_i start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT, at which 𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍subscript𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍\mathsf{sub}_{\mathsf{right}}sansserif_sub start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT occurs, is inside the range [i1.5k+1..i+1.5k+1][i-1.5^{k+1}..i+1.5^{k+1}][ italic_i - 1.5 start_POSTSUPERSCRIPT italic_k + 1 end_POSTSUPERSCRIPT . . italic_i + 1.5 start_POSTSUPERSCRIPT italic_k + 1 end_POSTSUPERSCRIPT ]. Let 𝗈𝖼𝖼𝗋𝗂𝗀𝗁𝗍subscript𝗈𝖼𝖼𝗋𝗂𝗀𝗁𝗍\mathsf{occ}_{\mathsf{right}}sansserif_occ start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT be the set of all such indices i𝗋𝗂𝗀𝗁𝗍subscript𝑖𝗋𝗂𝗀𝗁𝗍i_{\mathsf{right}}italic_i start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT from this range. We distinguish between two cases, regarding the period of 𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍subscript𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍\mathsf{sub}_{\mathsf{right}}sansserif_sub start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT.

Case 1: 𝗉𝖾𝗋(𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍)1.5k4𝗉𝖾𝗋subscript𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍superscript1.5𝑘4\mathsf{per}(\mathsf{sub}_{\mathsf{right}})\geq\frac{1.5^{k}}{4}sansserif_per ( sansserif_sub start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT ) ≥ divide start_ARG 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT end_ARG start_ARG 4 end_ARG.

The following claim is easy.

Claim 2.

If 𝗉𝖾𝗋(𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍)1.5k4𝗉𝖾𝗋subscript𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍superscript1.5𝑘4\mathsf{per}(\mathsf{sub}_{\mathsf{right}})\geq\frac{1.5^{k}}{4}sansserif_per ( sansserif_sub start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT ) ≥ divide start_ARG 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT end_ARG start_ARG 4 end_ARG, then |𝗈𝖼𝖼𝗋𝗂𝗀𝗁𝗍|=O(1)subscript𝗈𝖼𝖼𝗋𝗂𝗀𝗁𝗍𝑂1|\mathsf{occ}_{\mathsf{right}}|=O(1)| sansserif_occ start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT | = italic_O ( 1 ).

{claimproof}

The distance between two consecutive occurrences of 𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍subscript𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍\mathsf{sub}_{\mathsf{right}}sansserif_sub start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT is at least 1.5k4superscript1.5𝑘4\frac{1.5^{k}}{4}divide start_ARG 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT end_ARG start_ARG 4 end_ARG. Since a range of length 21.5k+12superscript1.5𝑘12\cdot 1.5^{k+1}2 ⋅ 1.5 start_POSTSUPERSCRIPT italic_k + 1 end_POSTSUPERSCRIPT contains O(1)𝑂1O(1)italic_O ( 1 ) disjoint ranges of length 1.5k4superscript1.5𝑘4\frac{1.5^{k}}{4}divide start_ARG 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT end_ARG start_ARG 4 end_ARG, the claim follows.

Now we build the set 1subscript1\mathcal{R}_{1}caligraphic_R start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT. We compute 𝗈𝖼𝖼𝗋𝗂𝗀𝗁𝗍subscript𝗈𝖼𝖼𝗋𝗂𝗀𝗁𝗍\mathsf{occ}_{\mathsf{right}}sansserif_occ start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT in O(1)𝑂1O(1)italic_O ( 1 ) time using the 𝖨𝖯𝖬Ssubscript𝖨𝖯𝖬𝑆\mathsf{IPM}_{S}sansserif_IPM start_POSTSUBSCRIPT italic_S end_POSTSUBSCRIPT data structure. For every j𝗈𝖼𝖼𝗋𝗂𝗀𝗁𝗍𝑗subscript𝗈𝖼𝖼𝗋𝗂𝗀𝗁𝗍j\in\mathsf{occ}_{\mathsf{right}}italic_j ∈ sansserif_occ start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT, we take the rectangle Rjsubscript𝑅𝑗R_{j}italic_R start_POSTSUBSCRIPT italic_j end_POSTSUBSCRIPT from Claim 1. Let 1={Rjj𝗈𝖼𝖼𝗋𝗂𝗀𝗁𝗍}subscript1conditional-setsubscript𝑅𝑗𝑗subscript𝗈𝖼𝖼𝗋𝗂𝗀𝗁𝗍\mathcal{R}_{1}=\{R_{j}\mid j\in\mathsf{occ}_{\mathsf{right}}\}caligraphic_R start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT = { italic_R start_POSTSUBSCRIPT italic_j end_POSTSUBSCRIPT ∣ italic_j ∈ sansserif_occ start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT }. By Claim 2, |1|=O(1)subscript1𝑂1|\mathcal{R}_{1}|=O(1)| caligraphic_R start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT | = italic_O ( 1 ). Consider a pair (,r)𝑟(\ell,r)( roman_ℓ , italic_r ) such that +r+1[1.5k..1.5k+1]𝑟1delimited-[]superscript1.5𝑘superscript..1.5𝑘1\ell+r+1\in[1.5^{k}..1.5^{k+1}]roman_ℓ + italic_r + 1 ∈ [ 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT ..1.5 start_POSTSUPERSCRIPT italic_k + 1 end_POSTSUPERSCRIPT ] and r1.5k2𝑟superscript1.5𝑘2r\geq\left\lfloor{\frac{1.5^{k}}{2}}\right\rflooritalic_r ≥ ⌊ divide start_ARG 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT end_ARG start_ARG 2 end_ARG ⌋, and let 𝗌𝗎𝖻=S[f..f+r]\mathsf{sub}=S[f-\ell..f+r]sansserif_sub = italic_S [ italic_f - roman_ℓ . . italic_f + italic_r ]. If (,r)1𝑟subscript1(\ell,r)\in\mathcal{R}_{1}( roman_ℓ , italic_r ) ∈ caligraphic_R start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT, then (,r)Rj𝑟subscript𝑅𝑗(\ell,r)\in R_{j}( roman_ℓ , italic_r ) ∈ italic_R start_POSTSUBSCRIPT italic_j end_POSTSUBSCRIPT for some j𝗈𝖼𝖼𝗋𝗂𝗀𝗁𝗍𝑗subscript𝗈𝖼𝖼𝗋𝗂𝗀𝗁𝗍j\in\mathsf{occ}_{\mathsf{right}}italic_j ∈ sansserif_occ start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT. Hence by Claim 1 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub covers i𝑖iitalic_i with an occurrence at j𝑗j-\ellitalic_j - roman_ℓ and 𝗉𝖾𝗋(𝗌𝗎𝖻)𝗉𝖾𝗋(𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍)1.5k4𝗉𝖾𝗋𝗌𝗎𝖻𝗉𝖾𝗋subscript𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍superscript1.5𝑘4\mathsf{per}(\mathsf{sub})\geq\mathsf{per}(\mathsf{sub}_{\mathsf{right}})\geq% \frac{1.5^{k}}{4}sansserif_per ( sansserif_sub ) ≥ sansserif_per ( sansserif_sub start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT ) ≥ divide start_ARG 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT end_ARG start_ARG 4 end_ARG, as required. Conversely, if 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub covers i𝑖iitalic_i with an occurrence at j𝑗jitalic_j, then j=j+superscript𝑗𝑗j^{\prime}=j+\ellitalic_j start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT = italic_j + roman_ℓ belongs to 𝗈𝖼𝖼𝗋𝗂𝗀𝗁𝗍subscript𝗈𝖼𝖼𝗋𝗂𝗀𝗁𝗍\mathsf{occ}_{\mathsf{right}}sansserif_occ start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT. Then by Claim 1 (,r)Rj𝑟subscript𝑅superscript𝑗(\ell,r)\in R_{j^{\prime}}( roman_ℓ , italic_r ) ∈ italic_R start_POSTSUBSCRIPT italic_j start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT end_POSTSUBSCRIPT and thus (,r)1𝑟subscript1(\ell,r)\in\mathcal{R}_{1}( roman_ℓ , italic_r ) ∈ caligraphic_R start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT. This concludes the proof of Lemma 3.1 in the case 𝗉𝖾𝗋(𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍)1.5k4𝗉𝖾𝗋subscript𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍superscript1.5𝑘4\mathsf{per}(\mathsf{sub}_{\mathsf{right}})\geq\frac{1.5^{k}}{4}sansserif_per ( sansserif_sub start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT ) ≥ divide start_ARG 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT end_ARG start_ARG 4 end_ARG.

Case 2: ρ=𝗉𝖾𝗋(𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍)<1.5k4𝜌𝗉𝖾𝗋subscript𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍superscript1.5𝑘4\rho=\mathsf{per}(\mathsf{sub}_{\mathsf{right}})<\frac{1.5^{k}}{4}italic_ρ = sansserif_per ( sansserif_sub start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT ) < divide start_ARG 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT end_ARG start_ARG 4 end_ARG.

Let 𝗋𝗎𝗇f=S[f𝗋𝗎𝗇f..f+r𝗋𝗎𝗇f]\mathsf{run}_{f}=S[f-\ell^{f}_{\mathsf{run}}..f+r^{f}_{\mathsf{run}}]sansserif_run start_POSTSUBSCRIPT italic_f end_POSTSUBSCRIPT = italic_S [ italic_f - roman_ℓ start_POSTSUPERSCRIPT italic_f end_POSTSUPERSCRIPT start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT . . italic_f + italic_r start_POSTSUPERSCRIPT italic_f end_POSTSUPERSCRIPT start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT ] be the ρ𝜌\rhoitalic_ρ-periodic run containing S[f..f+|𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍|1]S[f..f+|\mathsf{sub}_{\mathsf{right}}|-1]italic_S [ italic_f . . italic_f + | sansserif_sub start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT | - 1 ] (such a run exists by Lemma 2.6). Consider a pair (,r)𝑟(\ell,r)( roman_ℓ , italic_r ) such that +r+1[1.5k..1.5k+1]𝑟1delimited-[]superscript1.5𝑘superscript..1.5𝑘1\ell+r+1\in[1.5^{k}..1.5^{k+1}]roman_ℓ + italic_r + 1 ∈ [ 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT ..1.5 start_POSTSUPERSCRIPT italic_k + 1 end_POSTSUPERSCRIPT ] and r1.5k2𝑟superscript1.5𝑘2r\geq\left\lfloor{\frac{1.5^{k}}{2}}\right\rflooritalic_r ≥ ⌊ divide start_ARG 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT end_ARG start_ARG 2 end_ARG ⌋, and let the string 𝗌𝗎𝖻=S[f..f+r]\mathsf{sub}=S[f-\ell..f+r]sansserif_sub = italic_S [ italic_f - roman_ℓ . . italic_f + italic_r ] be not highly periodic. Then 𝗉𝖾𝗋(𝗌𝗎𝖻)>1.5k3>ρ𝗉𝖾𝗋𝗌𝗎𝖻superscript1.5𝑘3𝜌\mathsf{per}(\mathsf{sub})>\frac{1.5^{k}}{3}>\rhosansserif_per ( sansserif_sub ) > divide start_ARG 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT end_ARG start_ARG 3 end_ARG > italic_ρ. Hence 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub is not a substring of 𝗋𝗎𝗇fsubscript𝗋𝗎𝗇𝑓\mathsf{run}_{f}sansserif_run start_POSTSUBSCRIPT italic_f end_POSTSUBSCRIPT, which means that either >𝗋𝗎𝗇fsubscriptsuperscript𝑓𝗋𝗎𝗇\ell>\ell^{f}_{\mathsf{run}}roman_ℓ > roman_ℓ start_POSTSUPERSCRIPT italic_f end_POSTSUPERSCRIPT start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT or r>r𝗋𝗎𝗇f𝑟subscriptsuperscript𝑟𝑓𝗋𝗎𝗇r>r^{f}_{\mathsf{run}}italic_r > italic_r start_POSTSUPERSCRIPT italic_f end_POSTSUPERSCRIPT start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT. Below we assume r>r𝗋𝗎𝗇f𝑟subscriptsuperscript𝑟𝑓𝗋𝗎𝗇r>r^{f}_{\mathsf{run}}italic_r > italic_r start_POSTSUPERSCRIPT italic_f end_POSTSUPERSCRIPT start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT; the other case is symmetric. We first observe that this inequality guarantees that 𝗉𝖾𝗋(𝗌𝗎𝖻)𝗉𝖾𝗋𝗌𝗎𝖻\mathsf{per}(\mathsf{sub})sansserif_per ( sansserif_sub ) is big enough.

Claim 3.

For every 𝗌𝗎𝖻=S[f..f+r]\mathsf{sub}=S[f-\ell..f+r]sansserif_sub = italic_S [ italic_f - roman_ℓ . . italic_f + italic_r ] with r>r𝗋𝗎𝗇f𝑟subscriptsuperscript𝑟𝑓𝗋𝗎𝗇r>r^{f}_{\mathsf{run}}italic_r > italic_r start_POSTSUPERSCRIPT italic_f end_POSTSUPERSCRIPT start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT one has 𝗉𝖾𝗋(𝗌𝗎𝖻)1.5k4𝗉𝖾𝗋𝗌𝗎𝖻superscript1.5𝑘4\mathsf{per}(\mathsf{sub})\geq\frac{1.5^{k}}{4}sansserif_per ( sansserif_sub ) ≥ divide start_ARG 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT end_ARG start_ARG 4 end_ARG.

{claimproof}

The substring S[f..f+r𝗋𝗎𝗇f]S[f..f+r^{f}_{\mathsf{run}}]italic_S [ italic_f . . italic_f + italic_r start_POSTSUPERSCRIPT italic_f end_POSTSUPERSCRIPT start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT ] is ρ𝜌\rhoitalic_ρ-periodic and u=S[f..f+r𝗋𝗎𝗇f+1]u=S[f..f+r^{f}_{\mathsf{run}}+1]italic_u = italic_S [ italic_f . . italic_f + italic_r start_POSTSUPERSCRIPT italic_f end_POSTSUPERSCRIPT start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT + 1 ] is not (otherwise, 𝗋𝗎𝗇fsubscript𝗋𝗎𝗇𝑓\mathsf{run}_{f}sansserif_run start_POSTSUBSCRIPT italic_f end_POSTSUBSCRIPT is not a run). By Lemma 2.7, u𝑢uitalic_u is aperiodic. Then 𝗉𝖾𝗋(u)>|u|21.5k4𝗉𝖾𝗋𝑢𝑢2superscript1.5𝑘4\mathsf{per}(u)>\frac{|u|}{2}\geq\frac{1.5^{k}}{4}sansserif_per ( italic_u ) > divide start_ARG | italic_u | end_ARG start_ARG 2 end_ARG ≥ divide start_ARG 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT end_ARG start_ARG 4 end_ARG. It remains to note that u𝑢uitalic_u is a substring of 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub.

Let 𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍=S[f..f+r𝗋𝗎𝗇f]\mathsf{sub}_{\mathsf{right}}^{\to}=S[f..f+r_{\mathsf{run}}^{f}]sansserif_sub start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT start_POSTSUPERSCRIPT → end_POSTSUPERSCRIPT = italic_S [ italic_f . . italic_f + italic_r start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT start_POSTSUPERSCRIPT italic_f end_POSTSUPERSCRIPT ]. Note that 𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍superscriptsubscript𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍\mathsf{sub}_{\mathsf{right}}^{\to}sansserif_sub start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT start_POSTSUPERSCRIPT → end_POSTSUPERSCRIPT is a ρ𝜌\rhoitalic_ρ-periodic suffix of the ρ𝜌\rhoitalic_ρ-periodic run 𝗋𝗎𝗇fsuperscript𝗋𝗎𝗇𝑓\mathsf{run}^{f}sansserif_run start_POSTSUPERSCRIPT italic_f end_POSTSUPERSCRIPT and 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub contains 𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍superscriptsubscript𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍\mathsf{sub}_{\mathsf{right}}^{\to}sansserif_sub start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT start_POSTSUPERSCRIPT → end_POSTSUPERSCRIPT followed by a letter that breaks the period ρ𝜌\rhoitalic_ρ. This means that if 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub covers i𝑖iitalic_i, then S𝑆Sitalic_S contains, close to i𝑖iitalic_i, a ρ𝜌\rhoitalic_ρ-periodic run with the suffix 𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍superscriptsubscript𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍\mathsf{sub}_{\mathsf{right}}^{\to}sansserif_sub start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT start_POSTSUPERSCRIPT → end_POSTSUPERSCRIPT. Let us say that a ρ𝜌\rhoitalic_ρ-periodic run S[a𝗋𝗎𝗇..b𝗋𝗎𝗇]S[a_{\mathsf{run}}\>..\>b_{\mathsf{run}}]italic_S [ italic_a start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT . . italic_b start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT ] is close to i𝑖iitalic_i if a𝗋𝗎𝗇i+1.5k+1subscript𝑎𝗋𝗎𝗇𝑖superscript1.5𝑘1a_{\mathsf{run}}\leq i+1.5^{k+1}italic_a start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT ≤ italic_i + 1.5 start_POSTSUPERSCRIPT italic_k + 1 end_POSTSUPERSCRIPT and b𝗋𝗎𝗇i1.5k+1subscript𝑏𝗋𝗎𝗇𝑖superscript1.5𝑘1b_{\mathsf{run}}\geq i-1.5^{k+1}italic_b start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT ≥ italic_i - 1.5 start_POSTSUPERSCRIPT italic_k + 1 end_POSTSUPERSCRIPT. Clearly, if 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub covers i𝑖iitalic_i, it contains the suffix 𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍superscriptsubscript𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍\mathsf{sub}_{\mathsf{right}}^{\to}sansserif_sub start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT start_POSTSUPERSCRIPT → end_POSTSUPERSCRIPT of a run close to i𝑖iitalic_i.

Let 𝖱𝗎𝗇𝖼𝗅𝗈𝗌𝖾subscript𝖱𝗎𝗇𝖼𝗅𝗈𝗌𝖾\mathsf{Run}_{\mathsf{close}}sansserif_Run start_POSTSUBSCRIPT sansserif_close end_POSTSUBSCRIPT be the set of ρ𝜌\rhoitalic_ρ-periodic runs close to i𝑖iitalic_i with length at least |𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍|superscriptsubscript𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍|\mathsf{sub}_{\mathsf{right}}^{\to}|| sansserif_sub start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT start_POSTSUPERSCRIPT → end_POSTSUPERSCRIPT |.

Claim 4.

|𝖱𝗎𝗇𝖼𝗅𝗈𝗌𝖾|=O(1)subscript𝖱𝗎𝗇𝖼𝗅𝗈𝗌𝖾𝑂1|\mathsf{Run}_{\mathsf{close}}|=O(1)| sansserif_Run start_POSTSUBSCRIPT sansserif_close end_POSTSUBSCRIPT | = italic_O ( 1 ). Moreover, 𝖱𝗎𝗇𝖼𝗅𝗈𝗌𝖾subscript𝖱𝗎𝗇𝖼𝗅𝗈𝗌𝖾\mathsf{Run}_{\mathsf{close}}sansserif_Run start_POSTSUBSCRIPT sansserif_close end_POSTSUBSCRIPT can be computed in O(log2n)𝑂superscript2𝑛O(\log^{2}n)italic_O ( roman_log start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT italic_n ) time.

{claimproof}

Assume that 𝖱𝗎𝗇𝖼𝗅𝗈𝗌𝖾subscript𝖱𝗎𝗇𝖼𝗅𝗈𝗌𝖾\mathsf{Run}_{\mathsf{close}}sansserif_Run start_POSTSUBSCRIPT sansserif_close end_POSTSUBSCRIPT is ordered by the positions of runs. Each of these runs has length at least 1.5k2superscript1.5𝑘2\frac{1.5^{k}}{2}divide start_ARG 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT end_ARG start_ARG 2 end_ARG and any two ρ𝜌\rhoitalic_ρ-periodic runs overlap by less than ρ<1.5k4𝜌superscript1.5𝑘4\rho<\frac{1.5^{k}}{4}italic_ρ < divide start_ARG 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT end_ARG start_ARG 4 end_ARG positions. Then the positions of any two consecutive runs from 𝖱𝗎𝗇𝖼𝗅𝗈𝗌𝖾subscript𝖱𝗎𝗇𝖼𝗅𝗈𝗌𝖾\mathsf{Run}_{\mathsf{close}}sansserif_Run start_POSTSUBSCRIPT sansserif_close end_POSTSUBSCRIPT differ by more than 1.5k4superscript1.5𝑘4\frac{1.5^{k}}{4}divide start_ARG 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT end_ARG start_ARG 4 end_ARG and any two non-consecutive runs are disjoint. Since the first run ends no later than the position i1.5k+1𝑖superscript1.5𝑘1i-1.5^{k+1}italic_i - 1.5 start_POSTSUPERSCRIPT italic_k + 1 end_POSTSUPERSCRIPT by definition of being close to i𝑖iitalic_i, the third and all subsequent runs start after this position. Again by definition, all runs start before the position i+1.5k+1𝑖superscript1.5𝑘1i+1.5^{k+1}italic_i + 1.5 start_POSTSUPERSCRIPT italic_k + 1 end_POSTSUPERSCRIPT. The range [i1.5k+1..i+1.5k+1][i-1.5^{k+1}..i+1.5^{k+1}][ italic_i - 1.5 start_POSTSUPERSCRIPT italic_k + 1 end_POSTSUPERSCRIPT . . italic_i + 1.5 start_POSTSUPERSCRIPT italic_k + 1 end_POSTSUPERSCRIPT ] contains O(1)𝑂1O(1)italic_O ( 1 ) positions such that any two of them differ by more than 1.5k4superscript1.5𝑘4\frac{1.5^{k}}{4}divide start_ARG 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT end_ARG start_ARG 4 end_ARG. Hence we get |𝖱𝗎𝗇𝖼𝗅𝗈𝗌𝖾|=O(1)subscript𝖱𝗎𝗇𝖼𝗅𝗈𝗌𝖾𝑂1|\mathsf{Run}_{\mathsf{close}}|=O(1)| sansserif_Run start_POSTSUBSCRIPT sansserif_close end_POSTSUBSCRIPT | = italic_O ( 1 ).

Querying D𝗋𝗎𝗇ρsubscriptsuperscript𝐷𝜌𝗋𝗎𝗇D^{\rho}_{\mathsf{run}}italic_D start_POSTSUPERSCRIPT italic_ρ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT with the range [..i+1.5k+1]×[i1.5k+1..]×[|𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍|..][-\infty..i+1.5^{k+1}]\times[i-1.5^{k+1}..\infty]\times[|\mathsf{sub}_{\mathsf% {right}}^{\to}|..\infty][ - ∞ . . italic_i + 1.5 start_POSTSUPERSCRIPT italic_k + 1 end_POSTSUPERSCRIPT ] × [ italic_i - 1.5 start_POSTSUPERSCRIPT italic_k + 1 end_POSTSUPERSCRIPT . . ∞ ] × [ | sansserif_sub start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT start_POSTSUPERSCRIPT → end_POSTSUPERSCRIPT | . . ∞ ] we get all ρ𝜌\rhoitalic_ρ-periodic runs that are close to i𝑖iitalic_i (due to the first two coordinates) and have length at least |𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍|superscriptsubscript𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍|\mathsf{sub}_{\mathsf{right}}^{\to}|| sansserif_sub start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT start_POSTSUPERSCRIPT → end_POSTSUPERSCRIPT | (due to the last coordinate); i.e., what we get is 𝖱𝗎𝗇𝖼𝗅𝗈𝗌𝖾subscript𝖱𝗎𝗇𝖼𝗅𝗈𝗌𝖾\mathsf{Run}_{\mathsf{close}}sansserif_Run start_POSTSUBSCRIPT sansserif_close end_POSTSUBSCRIPT. The query time is O(log2n)𝑂superscript2𝑛O(\log^{2}n)italic_O ( roman_log start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT italic_n ) by Lemma 2.9.

Now we construct the set 1subscript1\mathcal{R}_{1}caligraphic_R start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT. We query D𝗋𝗎𝗇ρsubscriptsuperscript𝐷𝜌𝗋𝗎𝗇D^{\rho}_{\mathsf{run}}italic_D start_POSTSUPERSCRIPT italic_ρ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT with [..f]×[f+1.5k2..]×[..][-\infty..f]\times[f+\left\lfloor{\frac{1.5^{k}}{2}}\right\rfloor..\infty]% \times[-\infty..\infty][ - ∞ . . italic_f ] × [ italic_f + ⌊ divide start_ARG 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT end_ARG start_ARG 2 end_ARG ⌋ . . ∞ ] × [ - ∞ . . ∞ ] to get the unique ρ𝜌\rhoitalic_ρ-periodic run 𝗋𝗎𝗇f=[f𝗋𝗎𝗇f..f+r𝗋𝗎𝗇f]\mathsf{run}_{f}=[f-\ell^{f}_{\mathsf{run}}..f+r^{f}_{\mathsf{run}}]sansserif_run start_POSTSUBSCRIPT italic_f end_POSTSUBSCRIPT = [ italic_f - roman_ℓ start_POSTSUPERSCRIPT italic_f end_POSTSUPERSCRIPT start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT . . italic_f + italic_r start_POSTSUPERSCRIPT italic_f end_POSTSUPERSCRIPT start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT ] containing the substring 𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍=S[f..f+1.5k2]\mathsf{sub}_{\mathsf{right}}=S[f..f+\left\lfloor{\frac{1.5^{k}}{2}}\right\rfloor]sansserif_sub start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT = italic_S [ italic_f . . italic_f + ⌊ divide start_ARG 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT end_ARG start_ARG 2 end_ARG ⌋ ]. Then we compute the O(1)𝑂1O(1)italic_O ( 1 )-size set 𝖱𝗎𝗇𝖼𝗅𝗈𝗌𝖾subscript𝖱𝗎𝗇𝖼𝗅𝗈𝗌𝖾\mathsf{Run}_{\mathsf{close}}sansserif_Run start_POSTSUBSCRIPT sansserif_close end_POSTSUBSCRIPT (Claim 4). For every 𝗋𝗎𝗇𝖱𝗎𝗇𝖼𝗅𝗈𝗌𝖾𝗋𝗎𝗇subscript𝖱𝗎𝗇𝖼𝗅𝗈𝗌𝖾\mathsf{run}\in\mathsf{Run}_{\mathsf{close}}sansserif_run ∈ sansserif_Run start_POSTSUBSCRIPT sansserif_close end_POSTSUBSCRIPT we check, with an 𝖫𝖢𝖯𝖫𝖢𝖯\mathsf{LCP}sansserif_LCP query, whether 𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍superscriptsubscript𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍\mathsf{sub}_{\mathsf{right}}^{\to}sansserif_sub start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT start_POSTSUPERSCRIPT → end_POSTSUPERSCRIPT is a suffix of 𝗋𝗎𝗇𝗋𝗎𝗇\mathsf{run}sansserif_run. If yes, we compute the position i𝗋𝗂𝗀𝗁𝗍subscript𝑖𝗋𝗂𝗀𝗁𝗍i_{\mathsf{right}}italic_i start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT of this suffix from the parameters of the run. Since i𝗋𝗂𝗀𝗁𝗍subscript𝑖𝗋𝗂𝗀𝗁𝗍i_{\mathsf{right}}italic_i start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT is the position of an occurrence of 𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍subscript𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍\mathsf{sub}_{\mathsf{right}}sansserif_sub start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT, we apply Claim 1 to obtain a rectangle R=[1,2]×[r1,r2]𝑅subscript1subscript2subscript𝑟1subscript𝑟2R=[\ell_{1},\ell_{2}]\times[r_{1},r_{2}]italic_R = [ roman_ℓ start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT , roman_ℓ start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ] × [ italic_r start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT , italic_r start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ]. Since in our argument we assume r>r𝗋𝗎𝗇f𝑟superscriptsubscript𝑟𝗋𝗎𝗇𝑓r>r_{\mathsf{run}}^{f}italic_r > italic_r start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT start_POSTSUPERSCRIPT italic_f end_POSTSUPERSCRIPT, we replace r1subscript𝑟1r_{1}italic_r start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT with max{r1,r𝗋𝗎𝗇f+1}subscript𝑟1superscriptsubscript𝑟𝗋𝗎𝗇𝑓1\max\{r_{1},r_{\mathsf{run}}^{f}+1\}roman_max { italic_r start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT , italic_r start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT start_POSTSUPERSCRIPT italic_f end_POSTSUPERSCRIPT + 1 }. If the range for r𝑟ritalic_r remains nonempty, we denoted the obtained rectangle by R𝗋𝗎𝗇subscript𝑅𝗋𝗎𝗇R_{\mathsf{run}}italic_R start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT.

Let 𝗋𝗂𝗀𝗁𝗍={R𝗋𝗎𝗇𝗋𝗎𝗇𝖱𝗎𝗇𝖼𝗅𝗈𝗌𝖾}subscript𝗋𝗂𝗀𝗁𝗍conditional-setsubscript𝑅𝗋𝗎𝗇𝗋𝗎𝗇subscript𝖱𝗎𝗇𝖼𝗅𝗈𝗌𝖾\mathcal{R}_{\mathsf{right}}=\{R_{\mathsf{run}}\mid\mathsf{run}\in\mathsf{Run}% _{\mathsf{close}}\}caligraphic_R start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT = { italic_R start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT ∣ sansserif_run ∈ sansserif_Run start_POSTSUBSCRIPT sansserif_close end_POSTSUBSCRIPT }. In a symmetric way, we consider the case >𝗋𝗎𝗇fsuperscriptsubscript𝗋𝗎𝗇𝑓\ell>\ell_{\mathsf{run}}^{f}roman_ℓ > roman_ℓ start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT start_POSTSUPERSCRIPT italic_f end_POSTSUPERSCRIPT and build the set 𝗅𝖾𝖿𝗍subscript𝗅𝖾𝖿𝗍\mathcal{R}_{\mathsf{left}}caligraphic_R start_POSTSUBSCRIPT sansserif_left end_POSTSUBSCRIPT. Finally we set 1=𝗋𝗂𝗀𝗁𝗍𝗅𝖾𝖿𝗍subscript1subscript𝗋𝗂𝗀𝗁𝗍subscript𝗅𝖾𝖿𝗍\mathcal{R}_{1}=\mathcal{R}_{\mathsf{right}}\cup\mathcal{R}_{\mathsf{left}}caligraphic_R start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT = caligraphic_R start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT ∪ caligraphic_R start_POSTSUBSCRIPT sansserif_left end_POSTSUBSCRIPT. The time complexity is dominated by O(1)𝑂1O(1)italic_O ( 1 ) queries to D𝗋𝗎𝗇ρsubscriptsuperscript𝐷𝜌𝗋𝗎𝗇D^{\rho}_{\mathsf{run}}italic_D start_POSTSUPERSCRIPT italic_ρ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT, which take O(log2n)𝑂superscript2𝑛O(\log^{2}n)italic_O ( roman_log start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT italic_n ) by Lemma 2.9.

Now consider a pair (,r)𝑟(\ell,r)( roman_ℓ , italic_r ) such that +r+1[1.5k..1.5k+1]𝑟1delimited-[]superscript1.5𝑘superscript..1.5𝑘1\ell+r+1\in[1.5^{k}..1.5^{k+1}]roman_ℓ + italic_r + 1 ∈ [ 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT ..1.5 start_POSTSUPERSCRIPT italic_k + 1 end_POSTSUPERSCRIPT ] and r1.5k2𝑟superscript1.5𝑘2r\geq\left\lfloor{\frac{1.5^{k}}{2}}\right\rflooritalic_r ≥ ⌊ divide start_ARG 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT end_ARG start_ARG 2 end_ARG ⌋, and let 𝗌𝗎𝖻=S[f..f+r]\mathsf{sub}=S[f-\ell..f+r]sansserif_sub = italic_S [ italic_f - roman_ℓ . . italic_f + italic_r ]. If (,r)1𝑟subscript1(\ell,r)\in\mathcal{R}_{1}( roman_ℓ , italic_r ) ∈ caligraphic_R start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT, then (,r)𝑟(\ell,r)( roman_ℓ , italic_r ) belongs to some rectangle from 𝗋𝗂𝗀𝗁𝗍subscript𝗋𝗂𝗀𝗁𝗍\mathcal{R}_{\mathsf{right}}caligraphic_R start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT or 𝗅𝖾𝖿𝗍subscript𝗅𝖾𝖿𝗍\mathcal{R}_{\mathsf{left}}caligraphic_R start_POSTSUBSCRIPT sansserif_left end_POSTSUBSCRIPT; these cases are symmetric, so let this rectangle be R𝗋𝗎𝗇𝗋𝗂𝗀𝗁𝗍subscript𝑅𝗋𝗎𝗇subscript𝗋𝗂𝗀𝗁𝗍R_{\mathsf{run}}\in\mathcal{R}_{\mathsf{right}}italic_R start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT ∈ caligraphic_R start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT, where 𝗋𝗎𝗇=[a𝗋𝗎𝗇..b𝗋𝗎𝗇]\mathsf{run}=[a_{\mathsf{run}}..b_{\mathsf{run}}]sansserif_run = [ italic_a start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT . . italic_b start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT ]. Hence by Claim 1 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub covers i𝑖iitalic_i with an occurrence at b𝗋𝗎𝗇r𝗋𝗎𝗇fsubscript𝑏𝗋𝗎𝗇superscriptsubscript𝑟𝗋𝗎𝗇𝑓b_{\mathsf{run}}-r_{\mathsf{run}}^{f}-\ellitalic_b start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT - italic_r start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT start_POSTSUPERSCRIPT italic_f end_POSTSUPERSCRIPT - roman_ℓ. We also have 𝗉𝖾𝗋(𝗌𝗎𝖻)1.5k4𝗉𝖾𝗋𝗌𝗎𝖻superscript1.5𝑘4\mathsf{per}(\mathsf{sub})\geq\frac{1.5^{k}}{4}sansserif_per ( sansserif_sub ) ≥ divide start_ARG 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT end_ARG start_ARG 4 end_ARG by Claim 3. Conversely, if 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub is not highly periodic, then either r>r𝗋𝗎𝗇f𝑟superscriptsubscript𝑟𝗋𝗎𝗇𝑓r>r_{\mathsf{run}}^{f}italic_r > italic_r start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT start_POSTSUPERSCRIPT italic_f end_POSTSUPERSCRIPT or >𝗋𝗎𝗇fsuperscriptsubscript𝗋𝗎𝗇𝑓\ell>\ell_{\mathsf{run}}^{f}roman_ℓ > roman_ℓ start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT start_POSTSUPERSCRIPT italic_f end_POSTSUPERSCRIPT. Without loss of generality, let r>r𝗋𝗎𝗇f𝑟superscriptsubscript𝑟𝗋𝗎𝗇𝑓r>r_{\mathsf{run}}^{f}italic_r > italic_r start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT start_POSTSUPERSCRIPT italic_f end_POSTSUPERSCRIPT. Now if 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub covers i𝑖iitalic_i with an occurrence at j𝑗jitalic_j, then there is an occurrence of 𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍superscriptsubscript𝗌𝗎𝖻𝗋𝗂𝗀𝗁𝗍\mathsf{sub}_{\mathsf{right}}^{\to}sansserif_sub start_POSTSUBSCRIPT sansserif_right end_POSTSUBSCRIPT start_POSTSUPERSCRIPT → end_POSTSUPERSCRIPT at j=j+superscript𝑗𝑗j^{\prime}=j+\ellitalic_j start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT = italic_j + roman_ℓ that is a suffix of a ρ𝜌\rhoitalic_ρ-periodic run 𝗋𝗎𝗇𝗋𝗎𝗇\mathsf{run}sansserif_run. Then by Claim 1 we have (,r)R𝗋𝗎𝗇𝑟subscript𝑅𝗋𝗎𝗇(\ell,r)\in R_{\mathsf{run}}( roman_ℓ , italic_r ) ∈ italic_R start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT and thus (,r)1𝑟subscript1(\ell,r)\in\mathcal{R}_{1}( roman_ℓ , italic_r ) ∈ caligraphic_R start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT. Thus, we finished the proof of Lemma 3.1.

3.2 The Highly Periodic Case

In this section we prove Lemma 3.2, which is the analog of Lemma 3.1 for periodic strings.

We begin with more notation. Let u𝑢uitalic_u be a ρ𝜌\rhoitalic_ρ-periodic string having a substring v𝑣vitalic_v of length ρ𝜌\rhoitalic_ρ. Then there exist unique integers d1,d2[0..ρ1]d_{1},d_{2}\in[0..\rho-1]italic_d start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT , italic_d start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ∈ [ 0 . . italic_ρ - 1 ] and q𝑞q\in\mathbb{N}italic_q ∈ blackboard_N such that u=v[ρd1+1..ρ]vqv[1..d2]u=v[\rho{-}d_{1}{+}1..\rho]v^{q}v[1..d_{2}]italic_u = italic_v [ italic_ρ - italic_d start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT + 1 . . italic_ρ ] italic_v start_POSTSUPERSCRIPT italic_q end_POSTSUPERSCRIPT italic_v [ 1 . . italic_d start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ]. We abbreviate this representation as u=v[d1;q;d2]𝑢superscript𝑣subscript𝑑1𝑞subscript𝑑2u=v^{[d_{1};q;d_{2}]}italic_u = italic_v start_POSTSUPERSCRIPT [ italic_d start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT ; italic_q ; italic_d start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ] end_POSTSUPERSCRIPT. {observation} Let u=v[d1;q;d2]𝑢superscript𝑣subscript𝑑1𝑞subscript𝑑2u=v^{[d_{1};q;d_{2}]}italic_u = italic_v start_POSTSUPERSCRIPT [ italic_d start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT ; italic_q ; italic_d start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ] end_POSTSUPERSCRIPT. The numbers d1,d2subscript𝑑1subscript𝑑2d_{1},d_{2}italic_d start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT , italic_d start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT, and q𝑞qitalic_q can be computed in O(1)𝑂1O(1)italic_O ( 1 ) time given |u|,|v|𝑢𝑣|u|,|v|| italic_u | , | italic_v |, and the position of any occurrence of v𝑣vitalic_v in u𝑢uitalic_u. For a ρ𝜌\rhoitalic_ρ-periodic substring 𝗌𝗎𝖻=S[f..f+r]\mathsf{sub}=S[f-\ell..f+r]sansserif_sub = italic_S [ italic_f - roman_ℓ . . italic_f + italic_r ], we define its root by

𝗋𝗈𝗈𝗍={S[f..f+ρ1],if rρ1,S[fρ..f1],otherwise.\mathsf{root}=\begin{cases}S[f..f+\rho-1],&\text{if }r\geq\rho-1,\\ S[f-\rho..f-1],&\text{otherwise}.\end{cases}sansserif_root = { start_ROW start_CELL italic_S [ italic_f . . italic_f + italic_ρ - 1 ] , end_CELL start_CELL if italic_r ≥ italic_ρ - 1 , end_CELL end_ROW start_ROW start_CELL italic_S [ italic_f - italic_ρ . . italic_f - 1 ] , end_CELL start_CELL otherwise . end_CELL end_ROW

Thus, 𝗌𝗎𝖻=𝗋𝗈𝗈𝗍[d;q,r;dr]𝗌𝗎𝖻superscript𝗋𝗈𝗈𝗍subscript𝑑subscript𝑞𝑟subscript𝑑𝑟\mathsf{sub}=\mathsf{root}^{[d_{\ell};q_{\ell,r};d_{r}]}sansserif_sub = sansserif_root start_POSTSUPERSCRIPT [ italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT ; italic_q start_POSTSUBSCRIPT roman_ℓ , italic_r end_POSTSUBSCRIPT ; italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ] end_POSTSUPERSCRIPT for some unique integers d,dr[0..ρ1]d_{\ell},d_{r}\in[0..\rho-1]italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT , italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ∈ [ 0 . . italic_ρ - 1 ] and q,r>0subscript𝑞𝑟0q_{\ell,r}>0italic_q start_POSTSUBSCRIPT roman_ℓ , italic_r end_POSTSUBSCRIPT > 0.

Lemma 3.2.

Let f,i[n]𝑓𝑖delimited-[]𝑛f,i\in[n]italic_f , italic_i ∈ [ italic_n ] be two indices and let ρ[n]𝜌delimited-[]𝑛\rho\in[n]italic_ρ ∈ [ italic_n ]. There exists a set 𝒞𝒞\mathcal{C}caligraphic_C of O(1)𝑂1O(1)italic_O ( 1 ) cuboids such that every highly ρ𝜌\rhoitalic_ρ-periodic substring of the form 𝗌𝗎𝖻=S[f..f+r]\mathsf{sub}=S[f-\ell..f+r]sansserif_sub = italic_S [ italic_f - roman_ℓ . . italic_f + italic_r ] with ,r𝑟\ell,r\in\mathbb{N}roman_ℓ , italic_r ∈ blackboard_N satisfies the following: 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub covers index i𝑖iitalic_i if and only if (d,dr,q,r)𝒞subscript𝑑subscript𝑑𝑟subscript𝑞𝑟𝒞(d_{\ell},d_{r},q_{\ell,r})\in\mathcal{C}( italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT , italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT , italic_q start_POSTSUBSCRIPT roman_ℓ , italic_r end_POSTSUBSCRIPT ) ∈ caligraphic_C. Moreover, 𝒞𝒞\mathcal{C}caligraphic_C can be computed in O(log2n)𝑂superscript2𝑛O(\log^{2}n)italic_O ( roman_log start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT italic_n ) time.

Let 𝗌𝗎𝖻=S[f..f+r]\mathsf{sub}=S[f-\ell..f+r]sansserif_sub = italic_S [ italic_f - roman_ℓ . . italic_f + italic_r ] be highly ρ𝜌\rhoitalic_ρ-periodic. By Lemma 2.6, each occurrence of 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub is contained in a unique ρ𝜌\rhoitalic_ρ-periodic run. By Lemma 2.5, there are at most two such runs containing i𝑖iitalic_i (say, 𝗋𝗎𝗇1subscript𝗋𝗎𝗇1\mathsf{run}_{1}sansserif_run start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT and 𝗋𝗎𝗇2subscript𝗋𝗎𝗇2\mathsf{run}_{2}sansserif_run start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT). Hence if 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub covers i𝑖iitalic_i, it does so with an occurrence contained either in 𝗋𝗎𝗇1subscript𝗋𝗎𝗇1\mathsf{run}_{1}sansserif_run start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT or in 𝗋𝗎𝗇2subscript𝗋𝗎𝗇2\mathsf{run}_{2}sansserif_run start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT. Then Lemma 3.2 follows from Lemma 3.3 below: we query D𝗋𝗎𝗇ρsubscriptsuperscript𝐷𝜌𝗋𝗎𝗇D^{\rho}_{\mathsf{run}}italic_D start_POSTSUPERSCRIPT italic_ρ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT to get 𝗋𝗎𝗇1subscript𝗋𝗎𝗇1\mathsf{run}_{1}sansserif_run start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT and 𝗋𝗎𝗇2subscript𝗋𝗎𝗇2\mathsf{run}_{2}sansserif_run start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT (in O(log2n)𝑂superscript2𝑛O(\log^{2}n)italic_O ( roman_log start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT italic_n ) time by Lemma 2.9), take the sets 𝒞1subscript𝒞1\mathcal{C}_{1}caligraphic_C start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT and 𝒞2subscript𝒞2\mathcal{C}_{2}caligraphic_C start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT given by Lemma 3.3 for 𝗋𝗎𝗇1subscript𝗋𝗎𝗇1\mathsf{run}_{1}sansserif_run start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT and 𝗋𝗎𝗇2subscript𝗋𝗎𝗇2\mathsf{run}_{2}sansserif_run start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT respectively, and let 𝒞=𝒞1𝒞2𝒞subscript𝒞1subscript𝒞2\mathcal{C}=\mathcal{C}_{1}\cup\mathcal{C}_{2}caligraphic_C = caligraphic_C start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT ∪ caligraphic_C start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT.

Lemma 3.3.

Let 𝗋𝗎𝗇=S[a𝗋𝗎𝗇..b𝗋𝗎𝗇]\mathsf{run}=S[a_{\mathsf{run}}..b_{\mathsf{run}}]sansserif_run = italic_S [ italic_a start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT . . italic_b start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT ] be a ρ𝜌\rhoitalic_ρ-periodic run containing i𝑖iitalic_i. There is a set 𝒞𝒞\mathcal{C}caligraphic_C of O(1)𝑂1O(1)italic_O ( 1 ) cuboids such that 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub covers i𝑖iitalic_i with an occurrence contained in 𝗋𝗎𝗇𝗋𝗎𝗇\mathsf{run}sansserif_run if and only if (d,dr,q,r)𝒞subscript𝑑subscript𝑑𝑟subscript𝑞𝑟𝒞(d_{\ell},d_{r},q_{\ell,r})\in\mathcal{C}( italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT , italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT , italic_q start_POSTSUBSCRIPT roman_ℓ , italic_r end_POSTSUBSCRIPT ) ∈ caligraphic_C. Moreover, the set 𝒞𝒞\mathcal{C}caligraphic_C can be computed in O(1)𝑂1O(1)italic_O ( 1 ) time.

In the rest of the section we describe the algorithm computing the set 𝒞𝒞\mathcal{C}caligraphic_C of Lemma 3.3.

First the algorithm checks the length of 𝗋𝗎𝗇𝗋𝗎𝗇\mathsf{run}sansserif_run. Since 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub is highly ρ𝜌\rhoitalic_ρ-periodic, 𝗌𝗎𝖻3ρ𝗌𝗎𝖻3𝜌\mathsf{sub}\geq 3\rhosansserif_sub ≥ 3 italic_ρ. If |𝗋𝗎𝗇|<3ρ𝗋𝗎𝗇3𝜌|\mathsf{run}|<3\rho| sansserif_run | < 3 italic_ρ, then 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub has no occurrences in 𝗋𝗎𝗇𝗋𝗎𝗇\mathsf{run}sansserif_run. Hence in this case 𝒞=𝒞\mathcal{C}=\varnothingcaligraphic_C = ∅. Next, the algorithm verifies if 𝗋𝗈𝗈𝗍𝗋𝗈𝗈𝗍\mathsf{root}sansserif_root is a substring of 𝗋𝗎𝗇𝗋𝗎𝗇\mathsf{run}sansserif_run. Due to ρ𝜌\rhoitalic_ρ-periodicity of 𝗋𝗎𝗇𝗋𝗎𝗇\mathsf{run}sansserif_run, it is sufficient to check for an occurrence of 𝗋𝗈𝗈𝗍𝗋𝗈𝗈𝗍\mathsf{root}sansserif_root the prefix of 𝗋𝗎𝗇𝗋𝗎𝗇\mathsf{run}sansserif_run having length 2ρ=2|𝗋𝗈𝗈𝗍|2𝜌2𝗋𝗈𝗈𝗍2\rho=2|\mathsf{root}|2 italic_ρ = 2 | sansserif_root |. By Lemma 2.13, this check can be done in O(1)𝑂1O(1)italic_O ( 1 ) time with the 𝖨𝖯𝖬Ssubscript𝖨𝖯𝖬𝑆\mathsf{IPM}_{S}sansserif_IPM start_POSTSUBSCRIPT italic_S end_POSTSUBSCRIPT data structure. If 𝗋𝗈𝗈𝗍𝗋𝗈𝗈𝗍\mathsf{root}sansserif_root is not a substring of 𝗋𝗎𝗇𝗋𝗎𝗇\mathsf{run}sansserif_run, then once again 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub has no occurrences in 𝗋𝗎𝗇𝗋𝗎𝗇\mathsf{run}sansserif_run and so 𝒞=𝒞\mathcal{C}=\varnothingcaligraphic_C = ∅.

From now on, we assume that |𝗋𝗎𝗇|3ρ𝗋𝗎𝗇3𝜌|\mathsf{run}|\geq 3\rho| sansserif_run | ≥ 3 italic_ρ and 𝗋𝗎𝗇𝗋𝗎𝗇\mathsf{run}sansserif_run contains an occurrence of 𝗋𝗈𝗈𝗍𝗋𝗈𝗈𝗍\mathsf{root}sansserif_root. Then the algorithm computes, in O(1)𝑂1O(1)italic_O ( 1 ) time by Section 3.2, the parameters of the representation 𝗋𝗎𝗇=𝗋𝗈𝗈𝗍[d𝗋𝗎𝗇;q𝗋𝗎𝗇;dr𝗋𝗎𝗇]𝗋𝗎𝗇superscript𝗋𝗈𝗈𝗍superscriptsubscript𝑑𝗋𝗎𝗇subscript𝑞𝗋𝗎𝗇superscriptsubscript𝑑𝑟𝗋𝗎𝗇\mathsf{run}=\mathsf{root}^{[d_{\ell}^{\mathsf{run}};q_{\mathsf{run}};d_{r}^{% \mathsf{run}}]}sansserif_run = sansserif_root start_POSTSUPERSCRIPT [ italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT ; italic_q start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT ; italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT ] end_POSTSUPERSCRIPT. One has q𝗋𝗎𝗇2subscript𝑞𝗋𝗎𝗇2q_{\mathsf{run}}\geq 2italic_q start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT ≥ 2 since |𝗋𝗎𝗇|3ρ𝗋𝗎𝗇3𝜌|\mathsf{run}|\geq 3\rho| sansserif_run | ≥ 3 italic_ρ. We recall that 𝗌𝗎𝖻=𝗋𝗈𝗈𝗍[d;q,r;dr]𝗌𝗎𝖻superscript𝗋𝗈𝗈𝗍subscript𝑑subscript𝑞𝑟subscript𝑑𝑟\mathsf{sub}=\mathsf{root}^{[d_{\ell};q_{\ell,r};d_{r}]}sansserif_sub = sansserif_root start_POSTSUPERSCRIPT [ italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT ; italic_q start_POSTSUBSCRIPT roman_ℓ , italic_r end_POSTSUBSCRIPT ; italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ] end_POSTSUPERSCRIPT.

Note that 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub covers index i𝑖iitalic_i of S𝑆Sitalic_S with an occurrence contained in 𝗋𝗎𝗇𝗋𝗎𝗇\mathsf{run}sansserif_run if and only if it covers the index j=ia𝗋𝗎𝗇+1𝑗𝑖subscript𝑎𝗋𝗎𝗇1j=i-a_{\mathsf{run}}+1italic_j = italic_i - italic_a start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT + 1 of the string 𝗋𝗎𝗇𝗋𝗎𝗇\mathsf{run}sansserif_run. In order to build the set 𝒞𝒞\mathcal{C}caligraphic_C, we describe, in Claim 7, a set of necessary and sufficient conditions for 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub to cover an index j𝑗jitalic_j of 𝗋𝗎𝗇𝗋𝗎𝗇\mathsf{run}sansserif_run. We denote b=[d>d𝗋𝗎𝗇]subscript𝑏delimited-[]subscript𝑑superscriptsubscript𝑑𝗋𝗎𝗇b_{\ell}=[d_{\ell}>d_{\ell}^{\mathsf{run}}]italic_b start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT = [ italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT > italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT ], br=[dr>dr𝗋𝗎𝗇]subscript𝑏𝑟delimited-[]subscript𝑑𝑟superscriptsubscript𝑑𝑟𝗋𝗎𝗇b_{r}=[d_{r}>d_{r}^{\mathsf{run}}]italic_b start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT = [ italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT > italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT ] (Iverson bracket notation). We need two auxiliary claims.

Claim 5.

Let 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub occur in 𝗋𝗎𝗇𝗋𝗎𝗇\mathsf{run}sansserif_run. If x𝑥xitalic_x is the starting index of its leftmost occurrence and y𝑦yitalic_y is the ending index of its rightmost occurrence, then x=d𝗋𝗎𝗇d+1+bρ𝑥superscriptsubscript𝑑𝗋𝗎𝗇subscript𝑑1subscript𝑏𝜌x=d_{\ell}^{\mathsf{run}}-d_{\ell}+1+b_{\ell}\rhoitalic_x = italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT - italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT + 1 + italic_b start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT italic_ρ, y=d𝗋𝗎𝗇+(q𝗋𝗎𝗇br)ρ+dr𝑦superscriptsubscript𝑑𝗋𝗎𝗇subscript𝑞𝗋𝗎𝗇subscript𝑏𝑟𝜌subscript𝑑𝑟y=d_{\ell}^{\mathsf{run}}+(q_{\mathsf{run}}-b_{r})\rho+d_{r}italic_y = italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT + ( italic_q start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT - italic_b start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ) italic_ρ + italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT, and [x..y][x..y][ italic_x . . italic_y ] is exactly the set of indices covered by 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub in 𝗋𝗎𝗇𝗋𝗎𝗇\mathsf{run}sansserif_run.

{claimproof}

We prove the formula for x𝑥xitalic_x as the argument for y𝑦yitalic_y is similar. Since 𝗋𝗎𝗇𝗋𝗎𝗇\mathsf{run}sansserif_run is ρ𝜌\rhoitalic_ρ-periodic, we have x[1..ρ]x\in[1..\rho]italic_x ∈ [ 1 . . italic_ρ ] (otherwise, there is another occurrence at position xρ𝑥𝜌x-\rhoitalic_x - italic_ρ). Since 𝗋𝗈𝗈𝗍𝗋𝗈𝗈𝗍\mathsf{root}sansserif_root occurs in 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub at position d+1subscript𝑑1d_{\ell}+1italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT + 1, 𝗋𝗎𝗇𝗋𝗎𝗇\mathsf{run}sansserif_run has a matching occurrence of 𝗋𝗈𝗈𝗍𝗋𝗈𝗈𝗍\mathsf{root}sansserif_root at position d+x[d+1..d+ρ]d_{\ell}+x\in[d_{\ell}+1..d_{\ell}+\rho]italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT + italic_x ∈ [ italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT + 1 . . italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT + italic_ρ ]. The occurrences of 𝗋𝗈𝗈𝗍𝗋𝗈𝗈𝗍\mathsf{root}sansserif_root in 𝗋𝗎𝗇𝗋𝗎𝗇\mathsf{run}sansserif_run are at positions d𝗋𝗎𝗇+1,d𝗋𝗎𝗇+ρ+1,superscriptsubscript𝑑𝗋𝗎𝗇1superscriptsubscript𝑑𝗋𝗎𝗇𝜌1d_{\ell}^{\mathsf{run}}{+}1,d_{\ell}^{\mathsf{run}}{+}\rho{+}1,\ldotsitalic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT + 1 , italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT + italic_ρ + 1 , …, so exactly one of them starts in [d+1..d+ρ][d_{\ell}+1..d_{\ell}+\rho][ italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT + 1 . . italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT + italic_ρ ]. If dd𝗋𝗎𝗇subscript𝑑superscriptsubscript𝑑𝗋𝗎𝗇d_{\ell}\leq d_{\ell}^{\mathsf{run}}italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT ≤ italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT, one has d𝗋𝗎𝗇+1[d+1..d+ρ]d_{\ell}^{\mathsf{run}}+1\in[d_{\ell}+1..d_{\ell}+\rho]italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT + 1 ∈ [ italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT + 1 . . italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT + italic_ρ ]. Therefore, we have d𝗋𝗎𝗇+1=d+xsuperscriptsubscript𝑑𝗋𝗎𝗇1subscript𝑑𝑥d_{\ell}^{\mathsf{run}}+1=d_{\ell}+xitalic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT + 1 = italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT + italic_x, implying x=d𝗋𝗎𝗇d+1+0ρ𝑥superscriptsubscript𝑑𝗋𝗎𝗇subscript𝑑10𝜌x=d_{\ell}^{\mathsf{run}}-d_{\ell}+1+0\cdot\rhoitalic_x = italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT - italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT + 1 + 0 ⋅ italic_ρ as required. Similarly, if d>d𝗋𝗎𝗇subscript𝑑superscriptsubscript𝑑𝗋𝗎𝗇d_{\ell}>d_{\ell}^{\mathsf{run}}italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT > italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT, one has d𝗋𝗎𝗇+ρ+1[d+1..d+ρ]d_{\ell}^{\mathsf{run}}+\rho+1\in[d_{\ell}+1..d_{\ell}+\rho]italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT + italic_ρ + 1 ∈ [ italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT + 1 . . italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT + italic_ρ ]. Then d𝗋𝗎𝗇+ρ+1=d+xsuperscriptsubscript𝑑𝗋𝗎𝗇𝜌1subscript𝑑𝑥d_{\ell}^{\mathsf{run}}+\rho+1=d_{\ell}+xitalic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT + italic_ρ + 1 = italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT + italic_x, which implies x=d𝗋𝗎𝗇d+1+1ρ𝑥superscriptsubscript𝑑𝗋𝗎𝗇subscript𝑑11𝜌x=d_{\ell}^{\mathsf{run}}-d_{\ell}+1+1\cdot\rhoitalic_x = italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT - italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT + 1 + 1 ⋅ italic_ρ as required.

Since 𝗋𝗎𝗇𝗋𝗎𝗇\mathsf{run}sansserif_run is ρ𝜌\rhoitalic_ρ-periodic, the positions of any two consecutive occurrences of 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub in 𝗋𝗎𝗇𝗋𝗎𝗇\mathsf{run}sansserif_run differ by ρ𝜌\rhoitalic_ρ (see Figure 1). As |𝗌𝗎𝖻|>ρ𝗌𝗎𝖻𝜌|\mathsf{sub}|>\rho| sansserif_sub | > italic_ρ, the indices in 𝗋𝗎𝗇𝗋𝗎𝗇\mathsf{run}sansserif_run covered by occurrences of 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub form a single range from the first index of the leftmost occurrence (i.e., x𝑥xitalic_x) to the last index of the rightmost occurrence (i.e. y𝑦yitalic_y).

Claim 6.

The string 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub occurs in 𝗋𝗎𝗇𝗋𝗎𝗇\mathsf{run}sansserif_run if and only if q,rq𝗋𝗎𝗇bbrsubscript𝑞𝑟subscript𝑞𝗋𝗎𝗇subscript𝑏subscript𝑏𝑟q_{\ell,r}\leq q_{\mathsf{run}}-b_{\ell}-b_{r}italic_q start_POSTSUBSCRIPT roman_ℓ , italic_r end_POSTSUBSCRIPT ≤ italic_q start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT - italic_b start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT - italic_b start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT.

{claimproof}

Let 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub occur in 𝗋𝗎𝗇𝗋𝗎𝗇\mathsf{run}sansserif_run. By Claim 5, its leftmost occurrence is at x=d𝗋𝗎𝗇d+1+bρ𝑥subscriptsuperscript𝑑𝗋𝗎𝗇subscript𝑑1subscript𝑏𝜌x=d^{\mathsf{run}}_{\ell}-d_{\ell}+1+b_{\ell}\rhoitalic_x = italic_d start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT - italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT + 1 + italic_b start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT italic_ρ and its rightmost occurrence ends at y=d𝗋𝗎𝗇+(q𝗋𝗎𝗇br)ρ+dr𝑦superscriptsubscript𝑑𝗋𝗎𝗇subscript𝑞𝗋𝗎𝗇subscript𝑏𝑟𝜌subscript𝑑𝑟y=d_{\ell}^{\mathsf{run}}+(q_{\mathsf{run}}-b_{r})\rho+d_{r}italic_y = italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT + ( italic_q start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT - italic_b start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ) italic_ρ + italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT. Clearly, we have the inequality yx+1|𝗌𝗎𝖻|=d+q,rρ+dr𝑦𝑥1𝗌𝗎𝖻subscript𝑑subscript𝑞𝑟𝜌subscript𝑑𝑟y-x+1\geq|\mathsf{sub}|=d_{\ell}+q_{\ell,r}\rho+d_{r}italic_y - italic_x + 1 ≥ | sansserif_sub | = italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT + italic_q start_POSTSUBSCRIPT roman_ℓ , italic_r end_POSTSUBSCRIPT italic_ρ + italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT, which is equivalent to q,rq𝗋𝗎𝗇bbrsubscript𝑞𝑟subscript𝑞𝗋𝗎𝗇subscript𝑏subscript𝑏𝑟q_{\ell,r}\leq q_{\mathsf{run}}-b_{\ell}-b_{r}italic_q start_POSTSUBSCRIPT roman_ℓ , italic_r end_POSTSUBSCRIPT ≤ italic_q start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT - italic_b start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT - italic_b start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT.

For the converse, we assume that this inequality holds and show that 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub occurs in 𝗋𝗎𝗇𝗋𝗎𝗇\mathsf{run}sansserif_run at position x=d𝗋𝗎𝗇d+1+bρ𝑥superscriptsubscript𝑑𝗋𝗎𝗇subscript𝑑1subscript𝑏𝜌x=d_{\ell}^{\mathsf{run}}-d_{\ell}+1+b_{\ell}\rhoitalic_x = italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT - italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT + 1 + italic_b start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT italic_ρ. Since 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub and 𝗋𝗎𝗇𝗋𝗎𝗇\mathsf{run}sansserif_run are both ρ𝜌\rhoitalic_ρ-periodic and share the substring 𝗋𝗈𝗈𝗍𝗋𝗈𝗈𝗍\mathsf{root}sansserif_root of length ρ𝜌\rhoitalic_ρ, it suffices to prove that |𝗋𝗎𝗇[x..|𝗋𝗎𝗇|]||𝗌𝗎𝖻||\mathsf{run}[x..|\mathsf{run}|]|\geq|\mathsf{sub}|| sansserif_run [ italic_x . . | sansserif_run | ] | ≥ | sansserif_sub |. Observing that brρdrdr𝗋𝗎𝗇subscript𝑏𝑟𝜌subscript𝑑𝑟superscriptsubscript𝑑𝑟𝗋𝗎𝗇b_{r}\rho\geq d_{r}-d_{r}^{\mathsf{run}}italic_b start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT italic_ρ ≥ italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT - italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT, we obtain

|𝗋𝗎𝗇[x..|𝗋𝗎𝗇|]|=q𝗋𝗎𝗇ρ+d𝗋𝗎𝗇+dr𝗋𝗎𝗇x+1=(q𝗋𝗎𝗇b)ρ+d+dr𝗋𝗎𝗇(q𝗋𝗎𝗇bbr)ρ+d+drq,rρ+d+dr=|𝗌𝗎𝖻|,|\mathsf{run}[x..|\mathsf{run}|]|=q_{\mathsf{run}}\rho+d_{\ell}^{\mathsf{run}}% +d_{r}^{\mathsf{run}}-x+1=(q_{\mathsf{run}}-b_{\ell})\rho+d_{\ell}+d_{r}^{% \mathsf{run}}\\ \geq(q_{\mathsf{run}}-b_{\ell}-b_{r})\rho+d_{\ell}+d_{r}\geq q_{\ell,r}\rho+d_% {\ell}+d_{r}=|\mathsf{sub}|,start_ROW start_CELL | sansserif_run [ italic_x . . | sansserif_run | ] | = italic_q start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT italic_ρ + italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT + italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT - italic_x + 1 = ( italic_q start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT - italic_b start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT ) italic_ρ + italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT + italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT end_CELL end_ROW start_ROW start_CELL ≥ ( italic_q start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT - italic_b start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT - italic_b start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ) italic_ρ + italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT + italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ≥ italic_q start_POSTSUBSCRIPT roman_ℓ , italic_r end_POSTSUBSCRIPT italic_ρ + italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT + italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT = | sansserif_sub | , end_CELL end_ROW

as required.

Claim 7.

The string 𝗌𝗎𝖻=𝗋𝗈𝗈𝗍[d;q,r;dr]𝗌𝗎𝖻superscript𝗋𝗈𝗈𝗍subscript𝑑subscript𝑞𝑟subscript𝑑𝑟\mathsf{sub}=\mathsf{root}^{[d_{\ell};q_{\ell,r};d_{r}]}sansserif_sub = sansserif_root start_POSTSUPERSCRIPT [ italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT ; italic_q start_POSTSUBSCRIPT roman_ℓ , italic_r end_POSTSUBSCRIPT ; italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ] end_POSTSUPERSCRIPT covers index j𝑗jitalic_j in 𝗋𝗎𝗇=𝗋𝗈𝗈𝗍[d𝗋𝗎𝗇;q𝗋𝗎𝗇;dr𝗋𝗎𝗇]𝗋𝗎𝗇superscript𝗋𝗈𝗈𝗍superscriptsubscript𝑑𝗋𝗎𝗇subscript𝑞𝗋𝗎𝗇superscriptsubscript𝑑𝑟𝗋𝗎𝗇\mathsf{run}=\mathsf{root}^{[d_{\ell}^{\mathsf{run}};q_{\mathsf{run}};d_{r}^{% \mathsf{run}}]}sansserif_run = sansserif_root start_POSTSUPERSCRIPT [ italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT ; italic_q start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT ; italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT ] end_POSTSUPERSCRIPT if and only if one of the following mutually exclusive conditions holds:

  1. 1.

    dd𝗋𝗎𝗇subscript𝑑superscriptsubscript𝑑𝗋𝗎𝗇d_{\ell}\leq d_{\ell}^{\mathsf{run}}italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT ≤ italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT, drdr𝗋𝗎𝗇subscript𝑑𝑟superscriptsubscript𝑑𝑟𝗋𝗎𝗇d_{r}\leq d_{r}^{\mathsf{run}}italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ≤ italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT, q,rq𝗋𝗎𝗇subscript𝑞𝑟subscript𝑞𝗋𝗎𝗇q_{\ell,r}\leq q_{\mathsf{run}}\phantom{\,-\,1}italic_q start_POSTSUBSCRIPT roman_ℓ , italic_r end_POSTSUBSCRIPT ≤ italic_q start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT, and j[d𝗋𝗎𝗇d+1..d𝗋𝗎𝗇+q𝗋𝗎𝗇ρ+dr]j\in[d_{\ell}^{\mathsf{run}}-d_{\ell}+1\>..\>d_{\ell}^{\mathsf{run}}+q_{% \mathsf{run}}\rho+d_{r}]italic_j ∈ [ italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT - italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT + 1 . . italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT + italic_q start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT italic_ρ + italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ];

  2. 2.

    d>d𝗋𝗎𝗇subscript𝑑superscriptsubscript𝑑𝗋𝗎𝗇d_{\ell}>d_{\ell}^{\mathsf{run}}italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT > italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT, drdr𝗋𝗎𝗇subscript𝑑𝑟superscriptsubscript𝑑𝑟𝗋𝗎𝗇d_{r}\leq d_{r}^{\mathsf{run}}italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ≤ italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT, q,rq𝗋𝗎𝗇1subscript𝑞𝑟subscript𝑞𝗋𝗎𝗇1q_{\ell,r}\leq q_{\mathsf{run}}-1italic_q start_POSTSUBSCRIPT roman_ℓ , italic_r end_POSTSUBSCRIPT ≤ italic_q start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT - 1, and j[d𝗋𝗎𝗇d+1+ρ..d𝗋𝗎𝗇+q𝗋𝗎𝗇ρ+dr]j\in[d_{\ell}^{\mathsf{run}}-d_{\ell}+1+\rho\>..\>d_{\ell}^{\mathsf{run}}+q_{% \mathsf{run}}\rho+d_{r}]italic_j ∈ [ italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT - italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT + 1 + italic_ρ . . italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT + italic_q start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT italic_ρ + italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ];

  3. 3.

    dd𝗋𝗎𝗇subscript𝑑superscriptsubscript𝑑𝗋𝗎𝗇d_{\ell}\leq d_{\ell}^{\mathsf{run}}italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT ≤ italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT, dr>dr𝗋𝗎𝗇subscript𝑑𝑟superscriptsubscript𝑑𝑟𝗋𝗎𝗇d_{r}>d_{r}^{\mathsf{run}}italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT > italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT, q,rq𝗋𝗎𝗇1subscript𝑞𝑟subscript𝑞𝗋𝗎𝗇1q_{\ell,r}\leq q_{\mathsf{run}}-1italic_q start_POSTSUBSCRIPT roman_ℓ , italic_r end_POSTSUBSCRIPT ≤ italic_q start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT - 1, and j[d𝗋𝗎𝗇d+1..d𝗋𝗎𝗇+(q𝗋𝗎𝗇1)ρ+dr]j\in[d_{\ell}^{\mathsf{run}}-d_{\ell}+1\>..\>d_{\ell}^{\mathsf{run}}+(q_{% \mathsf{run}}-1)\rho+d_{r}]italic_j ∈ [ italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT - italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT + 1 . . italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT + ( italic_q start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT - 1 ) italic_ρ + italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ];

  4. 4.

    d>d𝗋𝗎𝗇subscript𝑑superscriptsubscript𝑑𝗋𝗎𝗇d_{\ell}>d_{\ell}^{\mathsf{run}}italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT > italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT, dr>dr𝗋𝗎𝗇subscript𝑑𝑟superscriptsubscript𝑑𝑟𝗋𝗎𝗇d_{r}>d_{r}^{\mathsf{run}}italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT > italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT, q,rq𝗋𝗎𝗇2subscript𝑞𝑟subscript𝑞𝗋𝗎𝗇2q_{\ell,r}\leq q_{\mathsf{run}}-2italic_q start_POSTSUBSCRIPT roman_ℓ , italic_r end_POSTSUBSCRIPT ≤ italic_q start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT - 2, and j[d𝗋𝗎𝗇d+1+ρ..d𝗋𝗎𝗇+(q𝗋𝗎𝗇1)ρ+dr]j\in[d_{\ell}^{\mathsf{run}}-d_{\ell}+1+\rho\>..\>d_{\ell}^{\mathsf{run}}+(q_{% \mathsf{run}}-1)\rho+d_{r}]italic_j ∈ [ italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT - italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT + 1 + italic_ρ . . italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT + ( italic_q start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT - 1 ) italic_ρ + italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ].

{claimproof}

If 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub covers index j𝑗jitalic_j, then 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub occurs in 𝗋𝗎𝗇𝗋𝗎𝗇\mathsf{run}sansserif_run. Hence Claim 6 implies the inequalities and Claim 5 implies the range for each of conditions 1–4.

For the converse, if one of the conditions 1–4 is true, then 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub indeed occurs in 𝗋𝗎𝗇𝗋𝗎𝗇\mathsf{run}sansserif_run according to Claim 6. Then again Claim 5 implies the range of indices covered by occurrences of 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub. As j𝑗jitalic_j belongs to this range, it is covered.

The algorithm builds the set 𝒞𝒞\mathcal{C}caligraphic_C by running through conditions 1–4 of Claim 7. If j=ia𝗋𝗎𝗇+1𝑗𝑖subscript𝑎𝗋𝗎𝗇1j=i-a_{\mathsf{run}}+1italic_j = italic_i - italic_a start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT + 1 belongs to the range from a condition, the algorithm adds to 𝒞𝒞\mathcal{C}caligraphic_C the cuboid defined by the inequalities listed in this condition; otherwise, it does nothing. The cuboids for the conditions 1, 2, 3, and 4 are, respectively, [1..d𝗋𝗎𝗇]×[1..dr𝗋𝗎𝗇]×[1..q𝗋𝗎𝗇][1..d_{\ell}^{\mathsf{run}}]\times[1..d_{r}^{\mathsf{run}}]\times[1..q_{% \mathsf{run}}][ 1 . . italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT ] × [ 1 . . italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT ] × [ 1 . . italic_q start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT ]; [d𝗋𝗎𝗇+1..ρ]×[1..dr𝗋𝗎𝗇]×[1..q𝗋𝗎𝗇1][d_{\ell}^{\mathsf{run}}{+}1..\rho]\times[1..d_{r}^{\mathsf{run}}]\times[1..q_% {\mathsf{run}}{-}1][ italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT + 1 . . italic_ρ ] × [ 1 . . italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT ] × [ 1 . . italic_q start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT - 1 ]; [1..d𝗋𝗎𝗇]×[dr𝗋𝗎𝗇+1..ρ]×[1..q𝗋𝗎𝗇1][1..d_{\ell}^{\mathsf{run}}]\times[d_{r}^{\mathsf{run}}{+}1..\rho]\times[1..q_% {\mathsf{run}}{-}1][ 1 . . italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT ] × [ italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT + 1 . . italic_ρ ] × [ 1 . . italic_q start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT - 1 ]; [d𝗋𝗎𝗇+1..ρ]×[dr𝗋𝗎𝗇+1..ρ]×[1..q𝗋𝗎𝗇2][d_{\ell}^{\mathsf{run}}{+}1..\rho]\times[d_{r}^{\mathsf{run}}{+}1..\rho]% \times[1..q_{\mathsf{run}}{-}2][ italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT + 1 . . italic_ρ ] × [ italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT start_POSTSUPERSCRIPT sansserif_run end_POSTSUPERSCRIPT + 1 . . italic_ρ ] × [ 1 . . italic_q start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT - 2 ].

Correctness.

Let 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub cover i𝑖iitalic_i with an occurrence contained in 𝗋𝗎𝗇𝗋𝗎𝗇\mathsf{run}sansserif_run. Then 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub covers the index j=ia𝗋𝗎𝗇+1𝑗𝑖subscript𝑎𝗋𝗎𝗇1j=i-a_{\mathsf{run}}+1italic_j = italic_i - italic_a start_POSTSUBSCRIPT sansserif_run end_POSTSUBSCRIPT + 1 in 𝗋𝗎𝗇𝗋𝗎𝗇\mathsf{run}sansserif_run. By Claim 7, the triple (d,dr,q,r)subscript𝑑subscript𝑑𝑟subscript𝑞𝑟(d_{\ell},d_{r},q_{\ell,r})( italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT , italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT , italic_q start_POSTSUBSCRIPT roman_ℓ , italic_r end_POSTSUBSCRIPT ) satisfies one of conditions 1–4, say, condition N. In particular, j𝑗jitalic_j belongs to the interval of condition N. Then the algorithm built a cuboid C𝐶Citalic_C from the inequalities of condition N such that (d,dr,q,r)Csubscript𝑑subscript𝑑𝑟subscript𝑞𝑟𝐶(d_{\ell},d_{r},q_{\ell,r})\in C( italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT , italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT , italic_q start_POSTSUBSCRIPT roman_ℓ , italic_r end_POSTSUBSCRIPT ) ∈ italic_C. Conversely, if (d,dr,q,r)Csubscript𝑑subscript𝑑𝑟subscript𝑞𝑟𝐶(d_{\ell},d_{r},q_{\ell,r})\in C( italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT , italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT , italic_q start_POSTSUBSCRIPT roman_ℓ , italic_r end_POSTSUBSCRIPT ) ∈ italic_C, where C𝐶Citalic_C was built from condition N of Claim 7, then j𝑗jitalic_j belongs to the interval of condition N. Hence condition N holds; by Claim 7, 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub covers the index j𝑗jitalic_j in 𝗋𝗎𝗇𝗋𝗎𝗇\mathsf{run}sansserif_run, and thus covers i𝑖iitalic_i in S𝑆Sitalic_S.

As the time complexity is straightforward, Lemma 3.3, and then Lemma 3.2, is proved.

4 2-Covers Oracle

In this section, we present a solution to the 2-cover_Oracle problem (Theorem 1.3). The preliminary part of the solution is common to all three problems.

Every 2-cover of S𝑆Sitalic_S contains a prefix and a suffix of S𝑆Sitalic_S. Respectively, each 2-cover has one of two types (see [26]): a prefix-suffix 2-cover (ps-cover) consists of a prefix of S𝑆Sitalic_S and a suffix of S𝑆Sitalic_S while in a border-substring 2-cover (bs-cover) one string is a border of S𝑆Sitalic_S. We process these two cases separately.

Let (U1,U2)subscript𝑈1subscript𝑈2(U_{1},U_{2})( italic_U start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT , italic_U start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ) be a pair of substrings. Lemmas 3.1 and 3.2 allow us to express each predicate “Ujsubscript𝑈𝑗U_{j}italic_U start_POSTSUBSCRIPT italic_j end_POSTSUBSCRIPT covers index i𝑖iitalic_i” as pjjisubscript𝑝𝑗superscriptsubscript𝑗𝑖p_{j}\in\mathcal{R}_{j}^{i}italic_p start_POSTSUBSCRIPT italic_j end_POSTSUBSCRIPT ∈ caligraphic_R start_POSTSUBSCRIPT italic_j end_POSTSUBSCRIPT start_POSTSUPERSCRIPT italic_i end_POSTSUPERSCRIPT, where pjsubscript𝑝𝑗p_{j}italic_p start_POSTSUBSCRIPT italic_j end_POSTSUBSCRIPT is a point and jisuperscriptsubscript𝑗𝑖\mathcal{R}_{j}^{i}caligraphic_R start_POSTSUBSCRIPT italic_j end_POSTSUBSCRIPT start_POSTSUPERSCRIPT italic_i end_POSTSUPERSCRIPT is a set of O(1)𝑂1O(1)italic_O ( 1 ) ranges in djsubscript𝑑𝑗d_{j}italic_d start_POSTSUBSCRIPT italic_j end_POSTSUBSCRIPT dimensions. Then the predicate “(U1,U2)subscript𝑈1subscript𝑈2(U_{1},U_{2})( italic_U start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT , italic_U start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ) is a 2-cover” is expressed by the 2CNF formula i=1n(p11ip22i)superscriptsubscript𝑖1𝑛subscript𝑝1superscriptsubscript1𝑖subscript𝑝2superscriptsubscript2𝑖\bigwedge_{i=1}^{n}(p_{1}\in\mathcal{R}_{1}^{i}\vee p_{2}\in\mathcal{R}_{2}^{i})⋀ start_POSTSUBSCRIPT italic_i = 1 end_POSTSUBSCRIPT start_POSTSUPERSCRIPT italic_n end_POSTSUPERSCRIPT ( italic_p start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT ∈ caligraphic_R start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT start_POSTSUPERSCRIPT italic_i end_POSTSUPERSCRIPT ∨ italic_p start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ∈ caligraphic_R start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT start_POSTSUPERSCRIPT italic_i end_POSTSUPERSCRIPT ). We answer the instances of this predicate with a new data structure based on rectangle stabbing (Lemma 2.10). The lemma below is proven in Appendix C.

Lemma 4.1 (2CNF Range Data Structure).

Let d,drsubscript𝑑subscript𝑑𝑟d_{\ell},d_{r}italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT , italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT be integer constants and let 𝖯𝖺𝗂𝗋𝗌={(1,1),(2,2),,(n,n)}𝖯𝖺𝗂𝗋𝗌subscript1subscript1subscript2subscript2subscript𝑛subscript𝑛\mathsf{Pairs}=\{(\mathcal{L}_{1},\mathcal{R}_{1}),(\mathcal{L}_{2},\mathcal{R% }_{2}),\ldots,(\mathcal{L}_{n},\mathcal{R}_{n})\}sansserif_Pairs = { ( caligraphic_L start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT , caligraphic_R start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT ) , ( caligraphic_L start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT , caligraphic_R start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ) , … , ( caligraphic_L start_POSTSUBSCRIPT italic_n end_POSTSUBSCRIPT , caligraphic_R start_POSTSUBSCRIPT italic_n end_POSTSUBSCRIPT ) } be a set of pairs such that for every i[n]𝑖delimited-[]𝑛i\in[n]italic_i ∈ [ italic_n ], isubscript𝑖\mathcal{L}_{i}caligraphic_L start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT is a set of O(1)𝑂1O(1)italic_O ( 1 ) dsubscript𝑑d_{\ell}italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT-dimensional orthogonal ranges and isubscript𝑖\mathcal{R}_{i}caligraphic_R start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT is a set of O(1)𝑂1O(1)italic_O ( 1 ) drsubscript𝑑𝑟d_{r}italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT-dimensional orthogonal ranges. The set 𝖯𝖺𝗂𝗋𝗌𝖯𝖺𝗂𝗋𝗌\mathsf{Pairs}sansserif_Pairs can be prepossessed in O(nlogd+dr1n)𝑂𝑛superscriptsubscript𝑑subscript𝑑𝑟1𝑛O(n\log^{d_{\ell}+d_{r}-1}n)italic_O ( italic_n roman_log start_POSTSUPERSCRIPT italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT + italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT - 1 end_POSTSUPERSCRIPT italic_n ) time to a data structure that supports the following query in O(logd+dr1n)𝑂superscriptsubscript𝑑subscript𝑑𝑟1𝑛O(\log^{d_{\ell}+d_{r}-1}n)italic_O ( roman_log start_POSTSUPERSCRIPT italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT + italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT - 1 end_POSTSUPERSCRIPT italic_n ) time:

  • 𝗊𝗎𝖾𝗋𝗒(p,pr)𝗊𝗎𝖾𝗋𝗒subscript𝑝subscript𝑝𝑟\mathsf{query}(p_{\ell},p_{r})sansserif_query ( italic_p start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT , italic_p start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ): for a dsubscript𝑑d_{\ell}italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT-dimensional point psubscript𝑝p_{\ell}italic_p start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT and a drsubscript𝑑𝑟d_{r}italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT-dimensional point prsubscript𝑝𝑟p_{r}italic_p start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT, decide if for every i[n]𝑖delimited-[]𝑛i\in[n]italic_i ∈ [ italic_n ] either pisubscript𝑝subscript𝑖p_{\ell}\in\mathcal{L}_{i}italic_p start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT ∈ caligraphic_L start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT or prisubscript𝑝𝑟subscript𝑖p_{r}\in\mathcal{R}_{i}italic_p start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ∈ caligraphic_R start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT.

As Lemma 3.1 refers to particular ranges and Lemma 3.2 refers to particular periods, we partition substrings into groups and build a separate 2𝖢𝖭𝖥2𝖢𝖭𝖥2\mathsf{CNF}2 sansserif_CNF data structure for each pair of groups. For prefixes, suffixes, and borders, we have O(logn)𝑂𝑛O(\log n)italic_O ( roman_log italic_n ) periods (Lemma 2.1) and thus O(logn)𝑂𝑛O(\log n)italic_O ( roman_log italic_n ) groups of highly periodic prefixes (suffixes, borders). The remaining prefixes (suffixes) form O(logn)𝑂𝑛O(\log n)italic_O ( roman_log italic_n ) groups associated with length ranges [1.5k..1.5k+1]delimited-[]superscript1.5𝑘superscript..1.5𝑘1[1.5^{k}..1.5^{k+1}][ 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT ..1.5 start_POSTSUPERSCRIPT italic_k + 1 end_POSTSUPERSCRIPT ] for some k𝑘kitalic_k. There are O(logn)𝑂𝑛O(\log n)italic_O ( roman_log italic_n ) remaining borders (Lemma 2.2), so each of them forms a separate group. For each group of borders we choose a fixed position f𝑓fitalic_f. Highly periodic substrings containing f𝑓fitalic_f form O(logn)𝑂𝑛O(\log n)italic_O ( roman_log italic_n ) groups (Lemma 2.5); the other are grouped according to O(logn)𝑂𝑛O(\log n)italic_O ( roman_log italic_n ) length ranges. Therefore, in total we build O(log2n)𝑂superscript2𝑛O(\log^{2}n)italic_O ( roman_log start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT italic_n ) 2𝖢𝖭𝖥2𝖢𝖭𝖥2\mathsf{CNF}2 sansserif_CNF data structures for ps-covers and bs-covers.

Effective dimension.

A direct implementation of Lemmas 3.1 and 3.2 leads to the 2𝖢𝖭𝖥2𝖢𝖭𝖥2\mathsf{CNF}2 sansserif_CNF structures of dimension 4 to 6. Let us show how to lower the dimension. For any group 𝗉𝗋𝖾𝖿𝗉𝗋𝖾𝖿\mathsf{pref}sansserif_pref of prefixes we take f=1𝑓1f=1italic_f = 1. Then in Lemma 3.1 all points have the form (0,r)0𝑟(0,r)( 0 , italic_r ). So we have fixed first coordinate and variable second coordinate. In Lemma 3.2, one has 𝗋𝗈𝗈𝗍=S[1..ρ]\mathsf{root}=S[1..\rho]sansserif_root = italic_S [ 1 . . italic_ρ ], and thus all points have the form (0,dr,qr)0subscript𝑑𝑟subscript𝑞𝑟(0,d_{r},q_{r})( 0 , italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT , italic_q start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ) with two variable coordinates. For groups of suffixes we take f=n𝑓𝑛f=nitalic_f = italic_n and symmetrically get the points of the form (,0)0(\ell,0)( roman_ℓ , 0 ) or (d,0,q)subscript𝑑0subscript𝑞(d_{\ell},0,q_{\ell})( italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT , 0 , italic_q start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT ). Since borders are simultaneously prefixes and suffixes, we get two fixed coordinates in the corresponding points. (Assuming f=1𝑓1f=1italic_f = 1, a group consisting of a single border U𝑈Uitalic_U, has the point (0,|U|)0𝑈(0,|U|)( 0 , | italic_U | ); the group 𝖻𝗈𝗋𝖻𝗈𝗋\mathsf{bor}sansserif_bor of highly ρ𝜌\rhoitalic_ρ-periodic borders has the points (0,dr,q)0subscript𝑑𝑟𝑞(0,d_{r},q)( 0 , italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT , italic_q ), where the remainder dr=|U|modρsubscript𝑑𝑟modulo𝑈𝜌d_{r}=|U|\bmod\rhoitalic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT = | italic_U | roman_mod italic_ρ is the same for all U𝖻𝗈𝗋𝑈𝖻𝗈𝗋U\in\mathsf{bor}italic_U ∈ sansserif_bor.) Finally, for general substrings all coordinates are variable. The effective dimension of a point is the number of its variable coordinates. Given a pair of groups of substrings, where the first (second) group has points of effective dimension d1subscript𝑑1d_{1}italic_d start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT (respectively, d2subscript𝑑2d_{2}italic_d start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT), we construct for them the 2𝖢𝖭𝖥2𝖢𝖭𝖥2\mathsf{CNF}2 sansserif_CNF structure of dimension d1+d2subscript𝑑1subscript𝑑2d_{1}+d_{2}italic_d start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT + italic_d start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT. In order to do this, we replace each involved range with its projection onto variable coordinates.

Building an oracle.

Given a group 𝗉𝗋𝖾𝖿𝗉𝗋𝖾𝖿\mathsf{pref}sansserif_pref of not highly periodic (resp., highly periodic) prefixes, we apply Lemma 3.1 (resp., Lemma 3.2) for every i[n]𝑖delimited-[]𝑛i\in[n]italic_i ∈ [ italic_n ]. Let 1,nsubscript1subscript𝑛\mathcal{L}_{1},\ldots\mathcal{L}_{n}caligraphic_L start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT , … caligraphic_L start_POSTSUBSCRIPT italic_n end_POSTSUBSCRIPT be the projections of the obtained ranges onto variable coordinates. A group 𝗌𝗎𝖿𝖿𝗌𝗎𝖿𝖿\mathsf{suff}sansserif_suff of suffixes is processed in the same way, resulting in the ranges 1,,nsubscript1subscript𝑛\mathcal{R}_{1},\ldots,\mathcal{R}_{n}caligraphic_R start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT , … , caligraphic_R start_POSTSUBSCRIPT italic_n end_POSTSUBSCRIPT. Then we apply Lemma 4.1, constructing the 2𝖢𝖭𝖥2𝖢𝖭𝖥2\mathsf{CNF}2 sansserif_CNF structure over the set 𝖯𝖺𝗂𝗋𝗌={(1,1),,(n,n)}𝖯𝖺𝗂𝗋𝗌subscript1subscript1subscript𝑛subscript𝑛\mathsf{Pairs}=\{(\mathcal{L}_{1},\mathcal{R}_{1}),\ldots,(\mathcal{L}_{n},% \mathcal{R}_{n})\}sansserif_Pairs = { ( caligraphic_L start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT , caligraphic_R start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT ) , … , ( caligraphic_L start_POSTSUBSCRIPT italic_n end_POSTSUBSCRIPT , caligraphic_R start_POSTSUBSCRIPT italic_n end_POSTSUBSCRIPT ) }. This 2𝖢𝖭𝖥2𝖢𝖭𝖥2\mathsf{CNF}2 sansserif_CNF thus represents the set 𝗉𝗋𝖾𝖿×𝗌𝗎𝖿𝖿𝗉𝗋𝖾𝖿𝗌𝗎𝖿𝖿\mathsf{pref}\times\mathsf{suff}sansserif_pref × sansserif_suff of pairs of substrings. We also memorize the values of fixed coordinates. Iterating over all pairs of prefix and suffix groups, we obtain the ps-cover part of the oracle.

Given a group 𝖻𝗈𝗋𝖻𝗈𝗋\mathsf{bor}sansserif_bor of borders, we first determine the reference position f𝖻𝗈𝗋subscript𝑓𝖻𝗈𝗋f_{\mathsf{bor}}italic_f start_POSTSUBSCRIPT sansserif_bor end_POSTSUBSCRIPT for groups of substrings and store it. If 𝖻𝗈𝗋={U}𝖻𝗈𝗋𝑈\mathsf{bor}=\{U\}sansserif_bor = { italic_U }, we use Lemma 2.11 to find all occurrences of U𝑈Uitalic_U in S𝑆Sitalic_S and choose f𝖻𝗈𝗋subscript𝑓𝖻𝗈𝗋f_{\mathsf{bor}}italic_f start_POSTSUBSCRIPT sansserif_bor end_POSTSUBSCRIPT to be any position not covered by U𝑈Uitalic_U; if there is no such position, i.e., if U𝑈Uitalic_U is a 1-cover, we set f𝖻𝗈𝗋=subscript𝑓𝖻𝗈𝗋f_{\mathsf{bor}}=\inftyitalic_f start_POSTSUBSCRIPT sansserif_bor end_POSTSUBSCRIPT = ∞. If 𝖻𝗈𝗋𝖻𝗈𝗋\mathsf{bor}sansserif_bor is a group of highly ρ𝜌\rhoitalic_ρ-periodic borders, we run a binary search on it, finding the shortest border U𝑈Uitalic_U that is not a 1-cover. Then we choose a position f𝖻𝗈𝗋subscript𝑓𝖻𝗈𝗋f_{\mathsf{bor}}italic_f start_POSTSUBSCRIPT sansserif_bor end_POSTSUBSCRIPT not covered by U𝑈Uitalic_U. Additionally, we store |U|𝑈|U|| italic_U |. If U𝑈Uitalic_U does not exist, we set f𝖻𝗈𝗋=subscript𝑓𝖻𝗈𝗋f_{\mathsf{bor}}=\inftyitalic_f start_POSTSUBSCRIPT sansserif_bor end_POSTSUBSCRIPT = ∞. After determining f𝖻𝗈𝗋subscript𝑓𝖻𝗈𝗋f_{\mathsf{bor}}italic_f start_POSTSUBSCRIPT sansserif_bor end_POSTSUBSCRIPT, and only if it is finite, we build a 2𝖢𝖭𝖥2𝖢𝖭𝖥2\mathsf{CNF}2 sansserif_CNF structure similar to the prefix-suffix case. Iterating over all pairs of border and substring groups (for the latter, we fix f=f𝖻𝗈𝗋𝑓subscript𝑓𝖻𝗈𝗋f=f_{\mathsf{bor}}italic_f = italic_f start_POSTSUBSCRIPT sansserif_bor end_POSTSUBSCRIPT), we obtain the bs-cover part of the oracle.

The time complexity of the construction is dominated by building O(log2n)𝑂superscript2𝑛O(\log^{2}n)italic_O ( roman_log start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT italic_n ) 2𝖢𝖭𝖥2𝖢𝖭𝖥2\mathsf{CNF}2 sansserif_CNF structures, each of dimension at most 4 (in the case where both groups consist of highly periodic strings). By Lemma 4.1, we get the required O(nlog5n)𝑂𝑛superscript5𝑛O(n\log^{5}n)italic_O ( italic_n roman_log start_POSTSUPERSCRIPT 5 end_POSTSUPERSCRIPT italic_n ) time bound.

Querying an oracle.

Given a pair (U1,U2)subscript𝑈1subscript𝑈2(U_{1},U_{2})( italic_U start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT , italic_U start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ) of substrings of S𝑆Sitalic_S, the oracle decides with 𝖫𝖢𝖯𝖫𝖢𝖯\mathsf{LCP}sansserif_LCP queries, to which of the cases (prefix-suffix, border-substring, both, or neither) this pair can be attributed, and proceed accordingly. For prefix, suffix, or border, the oracle finds its group deciding high periodicity with a query to 𝖨𝖯𝖬Ssubscript𝖨𝖯𝖬𝑆\mathsf{IPM}_{S}sansserif_IPM start_POSTSUBSCRIPT italic_S end_POSTSUBSCRIPT (Lemma 2.13). In the prefix-suffix case the oracle then create points for U1subscript𝑈1U_{1}italic_U start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT and U2subscript𝑈2U_{2}italic_U start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT, “trim” them by drop** fixed coordinates, and query with this pair of trimmed points the 2𝖢𝖭𝖥2𝖢𝖭𝖥2\mathsf{CNF}2 sansserif_CNF structure built for the set 𝗉𝗋𝖾𝖿×𝗌𝗎𝖿𝖿𝗉𝗋𝖾𝖿𝗌𝗎𝖿𝖿\mathsf{pref}\times\mathsf{suff}sansserif_pref × sansserif_suff, where the groups 𝗉𝗋𝖾𝖿𝗉𝗋𝖾𝖿\mathsf{pref}sansserif_pref and 𝗌𝗎𝖿𝖿𝗌𝗎𝖿𝖿\mathsf{suff}sansserif_suff contain U1subscript𝑈1U_{1}italic_U start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT and U2subscript𝑈2U_{2}italic_U start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT respectively.

Consider the border-substring case (let U1subscript𝑈1U_{1}italic_U start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT be the border). After determining the group 𝖻𝗈𝗋𝖻𝗈𝗋\mathsf{bor}sansserif_bor of U1subscript𝑈1U_{1}italic_U start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT, we check f𝖻𝗈𝗋subscript𝑓𝖻𝗈𝗋f_{\mathsf{bor}}italic_f start_POSTSUBSCRIPT sansserif_bor end_POSTSUBSCRIPT. If f𝖻𝗈𝗋=subscript𝑓𝖻𝗈𝗋f_{\mathsf{bor}}=\inftyitalic_f start_POSTSUBSCRIPT sansserif_bor end_POSTSUBSCRIPT = ∞, the oracle returns True since U1subscript𝑈1U_{1}italic_U start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT is a 1-cover. The same applies for the case where f𝖻𝗈𝗋subscript𝑓𝖻𝗈𝗋f_{\mathsf{bor}}italic_f start_POSTSUBSCRIPT sansserif_bor end_POSTSUBSCRIPT is finite, 𝖻𝗈𝗋𝖻𝗈𝗋\mathsf{bor}sansserif_bor is highly periodic, and U1subscript𝑈1U_{1}italic_U start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT is shorter than the saved length |U|𝑈|U|| italic_U |. Otherwise, we create the point for U1subscript𝑈1U_{1}italic_U start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT, “trim” it by drop** fixed coordinates, and create the point for U2subscript𝑈2U_{2}italic_U start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT using f=f𝖻𝗈𝗋𝑓subscript𝑓𝖻𝗈𝗋f=f_{\mathsf{bor}}italic_f = italic_f start_POSTSUBSCRIPT sansserif_bor end_POSTSUBSCRIPT. Then we query with the obtained pair of points the 2𝖢𝖭𝖥2𝖢𝖭𝖥2\mathsf{CNF}2 sansserif_CNF structure built for the pair 𝖻𝗈𝗋×𝗌𝗎𝖻f𝖻𝗈𝗋subscript𝗌𝗎𝖻𝑓\mathsf{bor}\times\mathsf{sub}_{f}sansserif_bor × sansserif_sub start_POSTSUBSCRIPT italic_f end_POSTSUBSCRIPT, where the group 𝗌𝗎𝖻fsubscript𝗌𝗎𝖻𝑓\mathsf{sub}_{f}sansserif_sub start_POSTSUBSCRIPT italic_f end_POSTSUBSCRIPT contains U2subscript𝑈2U_{2}italic_U start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT.

Finally, the oracle returns True if it met a condition for “True” in the border-substring case or if some query made to a 2𝖢𝖭𝖥2𝖢𝖭𝖥2\mathsf{CNF}2 sansserif_CNF structure returned True. Otherwise, the oracle returns False. The query time is dominated by O(1)𝑂1O(1)italic_O ( 1 ) queries to 2𝖢𝖭𝖥2𝖢𝖭𝖥2\mathsf{CNF}2 sansserif_CNF structures, each of dimension at most 4. By Lemma 4.1, we get the required O(log3n)𝑂superscript3𝑛O(\log^{3}n)italic_O ( roman_log start_POSTSUPERSCRIPT 3 end_POSTSUPERSCRIPT italic_n ) time bound.

As a result, we proved Theorem 1.3. The omitted details can be found in Appendix D.

5 Reporting 2-Covers

A possible, but in general highly inefficient, approach to the All_2-covers and Shortest_2-cover problems is to construct the oracle of Theorem 1.3 and query it with every pair (of substrings) that can be in the answer. In this section, we describe our approach to achieve near-linear running time. In a high level, we build a fast reporting procedure for “simple” cases and use its answer to determine the rest of the output with a small number of oracle queries. To rule out the trivial situation, we assume that all 2-covers containing a 1-cover are already reported just by listing the 1-covers.

We call a 2-cover (X,Y)𝑋𝑌(X,Y)( italic_X , italic_Y ) core if both substrings X𝑋Xitalic_X and Y𝑌Yitalic_Y are not highly periodic. This means that the 2𝖢𝖭𝖥2𝖢𝖭𝖥2\mathsf{CNF}2 sansserif_CNF structure for their groups is built by using only Lemma 3.1, and thus is 2-dimensional. In particular, a core cover is associated with a 2-dimensional point (x,y)𝑥𝑦(x,y)( italic_x , italic_y ). Note that 2-dimensional 2𝖢𝖭𝖥2𝖢𝖭𝖥2\mathsf{CNF}2 sansserif_CNF structures represent all core 2-covers (and may represent some non-core 2-covers as certain highly periodic substrings pass the restriction on periods in statement 1 of Lemma 3.1). The shortest 2-cover is core in view of Lemma 2.4.

On the ground level, a d𝑑ditalic_d-dimensional 2𝖢𝖭𝖥2𝖢𝖭𝖥2\mathsf{CNF}2 sansserif_CNF structure stores a set \mathcal{R}caligraphic_R of O(n)𝑂𝑛O(n)italic_O ( italic_n ) d𝑑ditalic_d-dimensional ranges and checks whether a d𝑑ditalic_d-dimensional point, sent as a query, is outside all rectangles. Such a view inspires the following definition for the case d=2𝑑2d=2italic_d = 2 we are interested in.

Definition 5.1 (Free Point).

Let \mathcal{R}caligraphic_R be a set of rectangles with corners in [n]2superscriptdelimited-[]𝑛2[n]^{2}[ italic_n ] start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT. A point p[n]2𝑝superscriptdelimited-[]𝑛2p\in[n]^{2}italic_p ∈ [ italic_n ] start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT is \mathcal{R}caligraphic_R-free if pR𝑝𝑅p\notin Ritalic_p ∉ italic_R for every R𝑅R\in\mathcal{R}italic_R ∈ caligraphic_R.

The following lemma is crucial. For the full proof see Appendix E.

Lemma 5.2 (Free Points Reporting).

Let \mathcal{R}caligraphic_R be a set consisting of Θ(n)Θ𝑛\Theta(n)roman_Θ ( italic_n ) rectangles with corners in [n]2superscriptdelimited-[]𝑛2[n]^{2}[ italic_n ] start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT. There is an algorithm that reports, for the input \mathcal{R}caligraphic_R,

  • all \mathcal{R}caligraphic_R-free points in O(nlog2n+𝗈𝗎𝗍𝗉𝗎𝗍logn)𝑂𝑛superscript2𝑛𝗈𝗎𝗍𝗉𝗎𝗍𝑛O(n\log^{2}n+\mathsf{output}\cdot\log n)italic_O ( italic_n roman_log start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT italic_n + sansserif_output ⋅ roman_log italic_n ) time, or

  • for each y[n]𝑦delimited-[]𝑛y\in[n]italic_y ∈ [ italic_n ], the \mathcal{R}caligraphic_R-free point (x,y)𝑥𝑦(x,y)( italic_x , italic_y ) with minimal x𝑥xitalic_x (if any) in O(nlog2n)𝑂𝑛superscript2𝑛O(n\log^{2}n)italic_O ( italic_n roman_log start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT italic_n ) time, or

  • for an additional input m[n]𝑚delimited-[]𝑛m\in[n]italic_m ∈ [ italic_n ], all \mathcal{R}caligraphic_R-free points (x,y)𝑥𝑦(x,y)( italic_x , italic_y ) with x+ym𝑥𝑦𝑚x+y\leq mitalic_x + italic_y ≤ italic_m in time O(nlog2n+𝗈𝗎𝗍𝗉𝗎𝗍log(n))𝑂𝑛superscript2𝑛𝗈𝗎𝗍𝗉𝗎𝗍𝑛O(n\log^{2}n+\mathsf{output}\cdot\log(n))italic_O ( italic_n roman_log start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT italic_n + sansserif_output ⋅ roman_log ( italic_n ) ).

The main point of the algorithm of Lemma 5.2 is an efficient reduction of the 2-dimensional problem to its 1-dimensional analog: for each y[n]𝑦delimited-[]𝑛y\in[n]italic_y ∈ [ italic_n ], report all points in the complement of a union of x𝑥xitalic_x-ranges, corresponding to this y𝑦yitalic_y. The algorithm stores the total of O(nlogn)𝑂𝑛𝑛O(n\log n)italic_O ( italic_n roman_log italic_n ) x𝑥xitalic_x-ranges in an auxiliary tree 𝒯𝒯\mathcal{T}caligraphic_T and associates with each node of 𝒯𝒯\mathcal{T}caligraphic_T a version of the main structure 𝒟𝒟\mathcal{D}caligraphic_D, which is a variant of persistent lazy segment tree [27].

To present a solution to the Shortest_2-cover problem, we need the following claim.

Claim 8.

If a point (x,y)𝑥𝑦(x,y)( italic_x , italic_y ) is associated with a core 2-cover (X,Y)𝑋𝑌(X,Y)( italic_X , italic_Y ), then |X|+|Y|=x+y𝑋𝑌𝑥𝑦|X|+|Y|=x+y| italic_X | + | italic_Y | = italic_x + italic_y in the prefix-suffix case and |X|+|Y|=x+y+1+b𝑋𝑌𝑥𝑦1𝑏|X|+|Y|=x+y+1+b| italic_X | + | italic_Y | = italic_x + italic_y + 1 + italic_b in the border-substring case with the border of length b𝑏bitalic_b.

{claimproof}

In the prefix-suffix case, X=S[1..x]X=S[1..x]italic_X = italic_S [ 1 . . italic_x ] and Y=S[ny+1..n]Y=S[n-y+1..n]italic_Y = italic_S [ italic_n - italic_y + 1 . . italic_n ]. In the border-substring case, X=S[1..b]X=S[1..b]italic_X = italic_S [ 1 . . italic_b ] and Y=S[fx..f+y]Y=S[f-x..f+y]italic_Y = italic_S [ italic_f - italic_x . . italic_f + italic_y ] for some position f𝑓fitalic_f. The claim follows.

A solution to Shortest_2-cover is as follows. We build all 2-dimensional 2𝖢𝖭𝖥2𝖢𝖭𝖥2\mathsf{CNF}2 sansserif_CNF structures (Lemma 4.1). For the set \mathcal{R}caligraphic_R of each structure, we run the algorithm of Lemma 5.2 with the second option and choose the point (x,y)𝑥𝑦(x,y)( italic_x , italic_y ) with the minimum sum of coordinates. From this pair we restore the corresponding 2-cover (X,Y)𝑋𝑌(X,Y)( italic_X , italic_Y ). Due to Claim 8, (X,Y)𝑋𝑌(X,Y)( italic_X , italic_Y ) has the minimum length among all 2222-covers corresponding to this 2𝖢𝖭𝖥2𝖢𝖭𝖥2\mathsf{CNF}2 sansserif_CNF structure. After processing all sets \mathcal{R}caligraphic_R, we return the 2222-cover of minimum length among those found.

Since each of O(log2n)𝑂superscript2𝑛O(\log^{2}n)italic_O ( roman_log start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT italic_n ) sets \mathcal{R}caligraphic_R is computed in O(nlogn)𝑂𝑛𝑛O(n\log n)italic_O ( italic_n roman_log italic_n ) time (Lemma 4.1) and processed in O(nlog2n)𝑂𝑛superscript2𝑛O(n\log^{2}n)italic_O ( italic_n roman_log start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT italic_n ) time (Lemma 5.2), the time complexity is O(nlog4n)𝑂𝑛superscript4𝑛O(n\log^{4}n)italic_O ( italic_n roman_log start_POSTSUPERSCRIPT 4 end_POSTSUPERSCRIPT italic_n ), as required. Theorem 1.2 is proved.

5.1 Report All 2-Covers Of Bounded Length

In this section, we overview the proof of Theorem 1.1, focusing on reporting all ps-covers with length bounded by m𝑚mitalic_m. The process of reporting all bs-covers is similar, and the full details are presented in Appendix F.

As a preliminary step, the algorithm constructs the 2222-cover oracle of Theorem 1.3. The first main step is similar to the proof of Theorem 1.2: the algorithm computes the set 𝒞msubscript𝒞𝑚\mathcal{C}_{m}caligraphic_C start_POSTSUBSCRIPT italic_m end_POSTSUBSCRIPT of all core 2222-covers of length at most m𝑚mitalic_m using the third variant of Lemma 5.2.

The remaining task is to report all highly periodic 2222-covers with length bounded by m𝑚mitalic_m. This is achieved via an extending procedure based on the following observation.

{observation}

Let (X,Y)𝑋𝑌(X,Y)( italic_X , italic_Y ) be a ps-cover of S𝑆Sitalic_S. Let X𝗍𝗋𝗂𝗆=Xsubscript𝑋𝗍𝗋𝗂𝗆𝑋X_{\mathsf{trim}}=Xitalic_X start_POSTSUBSCRIPT sansserif_trim end_POSTSUBSCRIPT = italic_X if X𝑋Xitalic_X is not highly periodic, or X𝗍𝗋𝗂𝗆=X[1..2ρ+|X|modρ]subscript𝑋𝗍𝗋𝗂𝗆𝑋delimited-[]modulo1..2𝜌𝑋𝜌X_{\mathsf{trim}}=X[1..2\rho+|X|\bmod\rho]italic_X start_POSTSUBSCRIPT sansserif_trim end_POSTSUBSCRIPT = italic_X [ 1..2 italic_ρ + | italic_X | roman_mod italic_ρ ] if X𝑋Xitalic_X is highly ρ𝜌\rhoitalic_ρ-periodic. Similarly, let Y𝗍𝗋𝗂𝗆=Ysubscript𝑌𝗍𝗋𝗂𝗆𝑌Y_{\mathsf{trim}}=Yitalic_Y start_POSTSUBSCRIPT sansserif_trim end_POSTSUBSCRIPT = italic_Y if Y𝑌Yitalic_Y is not highly periodic, or Y𝗍𝗋𝗂𝗆=Y[|Y|2ρ|Y|modρ+1..|Y|]Y_{\mathsf{trim}}=Y[|Y|-2\rho-|Y|\bmod\rho+1..|Y|]italic_Y start_POSTSUBSCRIPT sansserif_trim end_POSTSUBSCRIPT = italic_Y [ | italic_Y | - 2 italic_ρ - | italic_Y | roman_mod italic_ρ + 1 . . | italic_Y | ] if Y𝑌Yitalic_Y is highly ρ𝜌\rhoitalic_ρ-periodic.

The pair (X𝗍𝗋𝗂𝗆,Y𝗍𝗋𝗂𝗆)subscript𝑋𝗍𝗋𝗂𝗆subscript𝑌𝗍𝗋𝗂𝗆(X_{\mathsf{trim}},Y_{\mathsf{trim}})( italic_X start_POSTSUBSCRIPT sansserif_trim end_POSTSUBSCRIPT , italic_Y start_POSTSUBSCRIPT sansserif_trim end_POSTSUBSCRIPT ) is a core 2222-cover of S𝑆Sitalic_S. The fact that (X𝗍𝗋𝗂𝗆,Y𝗍𝗋𝗂𝗆)subscript𝑋𝗍𝗋𝗂𝗆subscript𝑌𝗍𝗋𝗂𝗆(X_{\mathsf{trim}},Y_{\mathsf{trim}})( italic_X start_POSTSUBSCRIPT sansserif_trim end_POSTSUBSCRIPT , italic_Y start_POSTSUBSCRIPT sansserif_trim end_POSTSUBSCRIPT ) is a 2222-cover follows directly from Lemma 2.4, and the fact that it is core is immediate: if X𝑋Xitalic_X is not short periodic, so is X𝗍𝗋𝗂𝗆=Xsubscript𝑋𝗍𝗋𝗂𝗆𝑋X_{\mathsf{trim}}=Xitalic_X start_POSTSUBSCRIPT sansserif_trim end_POSTSUBSCRIPT = italic_X, and if X𝑋Xitalic_X is highly periodic, X𝗍𝗋𝗂𝗆subscript𝑋𝗍𝗋𝗂𝗆X_{\mathsf{trim}}italic_X start_POSTSUBSCRIPT sansserif_trim end_POSTSUBSCRIPT is its short ρ𝜌\rhoitalic_ρ-periodic prefix with the same length modulo ρ𝜌\rhoitalic_ρ. The same analysis applies to Y𝑌Yitalic_Y and Y𝗍𝗋𝗂𝗆subscript𝑌𝗍𝗋𝗂𝗆Y_{\mathsf{trim}}italic_Y start_POSTSUBSCRIPT sansserif_trim end_POSTSUBSCRIPT. Clearly, if the length of (X,Y)𝑋𝑌(X,Y)( italic_X , italic_Y ) is bounded by m𝑚mitalic_m, we have (X𝗍𝗋𝗂𝗆,Y𝗍𝗋𝗂𝗆)𝒞msubscript𝑋𝗍𝗋𝗂𝗆subscript𝑌𝗍𝗋𝗂𝗆subscript𝒞𝑚(X_{\mathsf{trim}},Y_{\mathsf{trim}})\in\mathcal{C}_{m}( italic_X start_POSTSUBSCRIPT sansserif_trim end_POSTSUBSCRIPT , italic_Y start_POSTSUBSCRIPT sansserif_trim end_POSTSUBSCRIPT ) ∈ caligraphic_C start_POSTSUBSCRIPT italic_m end_POSTSUBSCRIPT.

We proceed to describe how to exploit Section 5.1 to report all prefix-suffix 2222-covers with length bounded by m𝑚mitalic_m. We process every pair (X,Y)𝒞m𝑋𝑌subscript𝒞𝑚(X,Y)\in\mathcal{C}_{m}( italic_X , italic_Y ) ∈ caligraphic_C start_POSTSUBSCRIPT italic_m end_POSTSUBSCRIPT. When processing (X,Y)𝑋𝑌(X,Y)( italic_X , italic_Y ), we wish to report all 2222-covers (X,Y)𝒞msuperscript𝑋superscript𝑌subscript𝒞𝑚(X^{\prime},Y^{\prime})\notin\mathcal{C}_{m}( italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT , italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT ) ∉ caligraphic_C start_POSTSUBSCRIPT italic_m end_POSTSUBSCRIPT with |X|+|Y|msuperscript𝑋superscript𝑌𝑚|X^{\prime}|+|Y^{\prime}|\leq m| italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT | + | italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT | ≤ italic_m, X=X𝗍𝗋𝗂𝗆𝑋subscriptsuperscript𝑋𝗍𝗋𝗂𝗆X=X^{\prime}_{\mathsf{trim}}italic_X = italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT sansserif_trim end_POSTSUBSCRIPT and Y=Y𝗍𝗋𝗂𝗆𝑌subscriptsuperscript𝑌𝗍𝗋𝗂𝗆Y=Y^{\prime}_{\mathsf{trim}}italic_Y = italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT sansserif_trim end_POSTSUBSCRIPT. Note that such (X,Y)superscript𝑋superscript𝑌(X^{\prime},Y^{\prime})( italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT , italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT ) may exist only if X𝑋Xitalic_X or Y𝑌Yitalic_Y is short periodic. Section 5.1 directly implies that all non-core ps-covers of length at most m𝑚mitalic_m are reported in this way. Assume that X=S[1..2ρ+d]𝑋𝑆delimited-[]1..2𝜌𝑑X=S[1..2\rho+d]italic_X = italic_S [ 1..2 italic_ρ + italic_d ] is short ρ𝜌\rhoitalic_ρ-periodic and Y𝑌Yitalic_Y is aperiodic. We initialize an iterator q=3𝑞3q=3italic_q = 3, and check if the following conditions hold:

  1. 1.

    qρ+d+|Y|m𝑞𝜌𝑑𝑌𝑚q\cdot\rho+d+|Y|\leq mitalic_q ⋅ italic_ρ + italic_d + | italic_Y | ≤ italic_m.

  2. 2.

    Xq=S[1..qρ+d]X_{q}=S[1..q\cdot\rho+d]italic_X start_POSTSUBSCRIPT italic_q end_POSTSUBSCRIPT = italic_S [ 1 . . italic_q ⋅ italic_ρ + italic_d ] is ρ𝜌\rhoitalic_ρ periodic.

  3. 3.

    the pair (Xq,Y)subscript𝑋𝑞𝑌(X_{q},Y)( italic_X start_POSTSUBSCRIPT italic_q end_POSTSUBSCRIPT , italic_Y ) is a 2222-cover.

The second condition is checked using Lemma 2.13, and the third condition is checked via a query to the 2222-cover oracle. If all three conditions are true, the algorithm reports (Xq,Y)subscript𝑋𝑞𝑌(X_{q},Y)( italic_X start_POSTSUBSCRIPT italic_q end_POSTSUBSCRIPT , italic_Y ) as a 2222-cover, assigns qq+1𝑞𝑞1q\leftarrow q+1italic_q ← italic_q + 1 and checks the conditions again. Otherwise, the algorithm halts. It is easy to see that exactly all 2222-covers (X,Y)superscript𝑋superscript𝑌(X^{\prime},Y^{\prime})( italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT , italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT ) such that (X,Y)𝑋𝑌(X,Y)( italic_X , italic_Y ) is the trimmed version of (X,Y)superscript𝑋superscript𝑌(X^{\prime},Y^{\prime})( italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT , italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT ) are reported this way.

The time complexity is dominated by the queries to the oracle. Each query that returns True can be charged on the reported 2222-cover. Every query that returns False can be charged on the original pair (X,Y)𝑋𝑌(X,Y)( italic_X , italic_Y ), as a ’False’ response terminates the algorithm. It follows that the total running time on every (X,Y)𝒞m𝑋𝑌subscript𝒞𝑚(X,Y)\in\mathcal{C}_{m}( italic_X , italic_Y ) ∈ caligraphic_C start_POSTSUBSCRIPT italic_m end_POSTSUBSCRIPT is O(𝗈𝗎𝗍𝗉𝗎𝗍log3n)𝑂𝗈𝗎𝗍𝗉𝗎𝗍superscript3𝑛O(\mathsf{output}\cdot\log^{3}n)italic_O ( sansserif_output ⋅ roman_log start_POSTSUPERSCRIPT 3 end_POSTSUPERSCRIPT italic_n ) as required.

The case in which X𝑋Xitalic_X is aperiodic and Y𝑌Yitalic_Y is short periodic is completely symmetrical, and the case in which both are short periodic is treated in a ’nested loop’ fashion: only X𝑋Xitalic_X is extended until breaking one of the conditions, and then the process repeats with Y𝑌Yitalic_Y extended by one period, two periods, and so on. In Appendix F, we show how to appropriately execute this nested loop in a manner that guaranteed the desired running time.

References

  • [1] Ali Alatabbi, M. Sohel Rahman, and W. F. Smyth. Computing covers using prefix tables. Discret. Appl. Math., 212:2–9, 2016. URL: https://doi.org/10.1016/j.dam.2015.05.019, doi:10.1016/J.DAM.2015.05.019.
  • [2] Amihood Amir, Avivit Levy, Moshe Lewenstein, Ronit Lubin, and Benny Porat. Can we recover the cover? Algorithmica, 81(7):2857–2875, 2019. URL: https://doi.org/10.1007/s00453-019-00559-8, doi:10.1007/S00453-019-00559-8.
  • [3] Amihood Amir, Avivit Levy, Ronit Lubin, and Ely Porat. Approximate cover of strings. Theor. Comput. Sci., 793:59–69, 2019. URL: https://doi.org/10.1016/j.tcs.2019.05.020, doi:10.1016/J.TCS.2019.05.020.
  • [4] Alberto Apostolico and Andrzej Ehrenfeucht. Efficient detection of quasiperiodicities in strings. Theoretical Computer Science, 119(2):247–265, 1993. URL: https://www.sciencedirect.com/science/article/pii/030439759390159Q, doi:10.1016/0304-3975(93)90159-Q.
  • [5] Hideo Bannai, Tomohiro I, Shunsuke Inenaga, Yuto Nakashima, Masayuki Takeda, and Kazuya Tsuruta. The "runs" theorem. SIAM J. Comput., 46(5):1501–1514, 2017. doi:10.1137/15M1011032.
  • [6] Carl Barton, Tomasz Kociumaka, Chang Liu, Solon P. Pissis, and Jakub Radoszewski. Indexing weighted sequences: Neat and efficient. Inf. Comput., 270, 2020. URL: https://doi.org/10.1016/j.ic.2019.104462, doi:10.1016/J.IC.2019.104462.
  • [7] Omer Berkman, Costas S. Iliopoulos, and Kunsoo Park. The subtree max gap problem with application to parallel string covering. Inf. Comput., 123(1):127–137, 1995. URL: https://doi.org/10.1006/inco.1995.1162, doi:10.1006/INCO.1995.1162.
  • [8] Bernard Chazelle. A functional approach to data structures and its use in multidimensional searching. SIAM J. Comput., 17(3):427–462, 1988. doi:10.1137/0217026.
  • [9] Richard Cole, CS Ilopoulos, Manal Mohamed, William F Smyth, and Lu Yang. The complexity of the minimum k-cover problem. Journal of Automata, Languages and Combinatorics, 10(5-6):641–653, 2005.
  • [10] Maxime Crochemore and Wojciech Rytter. Squares, cubes, and time-space efficient string searching. Algorithmica, 13(5):405–425, 1995.
  • [11] Patryk Czajka and Jakub Radoszewski. Experimental evaluation of algorithms for computing quasiperiods. Theoretical Computer Science, 854:17–29, 2021.
  • [12] James R. Driscoll, Neil Sarnak, Daniel Dominic Sleator, and Robert Endre Tarjan. Making data structures persistent. J. Comput. Syst. Sci., 38(1):86–124, 1989. doi:10.1016/0022-0000(89)90034-2.
  • [13] Jonas Ellert, Pawel Gawrychowski, and Garance Gourdel. Optimal square detection over general alphabets. In Nikhil Bansal and Viswanath Nagarajan, editors, Proceedings of the 2023 ACM-SIAM Symposium on Discrete Algorithms, SODA 2023, Florence, Italy, January 22-25, 2023, pages 5220–5242. SIAM, 2023. URL: https://doi.org/10.1137/1.9781611977554.ch189, doi:10.1137/1.9781611977554.CH189.
  • [14] N. J. Fine and H. S. Wilf. Uniqueness theorems for periodic functions. Proc. Amer. Math. Soc., 16:109–114, 1965.
  • [15] Zvi Galil and Raffaele Giancarlo. Data structures and algorithms for approximate string matching. Journal of Complexity, 4(1):33–72, 1988. doi:10.1016/0885-064X(88)90008-8.
  • [16] Pawel Gawrychowski, Jakub Radoszewski, and Tatiana Starikovskaya. Quasi-periodicity in streams. In Nadia Pisanti and Solon P. Pissis, editors, 30th Annual Symposium on Combinatorial Pattern Matching, CPM 2019, June 18-20, 2019, Pisa, Italy, volume 128 of LIPIcs, pages 22:1–22:14. Schloss Dagstuhl - Leibniz-Zentrum für Informatik, 2019. URL: https://doi.org/10.4230/LIPIcs.CPM.2019.22, doi:10.4230/LIPICS.CPM.2019.22.
  • [17] Qing Guo, Hui Zhang, and Costas S Iliopoulos. Computing the λ𝜆\lambdaitalic_λ-covers of a string. Information Sciences, 177(19):3957–3967, 2007.
  • [18] Donald E. Knuth, James H. Morris Jr., and Vaughan R. Pratt. Fast pattern matching in strings. SIAM J. Comput., 6(2):323–350, 1977.
  • [19] Tomasz Kociumaka, Solon P. Pissis, Jakub Radoszewski, Wojciech Rytter, and Tomasz Walen. Fast algorithm for partial covers in words. Algorithmica, 73(1):217–233, 2015. URL: https://doi.org/10.1007/s00453-014-9915-3, doi:10.1007/S00453-014-9915-3.
  • [20] Tomasz Kociumaka, Jakub Radoszewski, Wojciech Rytter, and Tomasz Walen. Optimal data structure for internal pattern matching queries in a text and applications. CoRR, abs/1311.6235, 2013. URL: http://arxiv.longhoe.net/abs/1311.6235, arXiv:1311.6235.
  • [21] Gad M. Landau and Uzi Vishkin. Fast string matching with k differences. Journal of Computer and System Sciences, 37(1):63–78, 1988. doi:10.1016/0022-0000(88)90045-1.
  • [22] Laurentius Leonard and Ken Tanaka. Suffix tree-based linear algorithms for multiple prefixes, single suffix counting and listing problems. CoRR, abs/2203.16908, 2022. URL: https://doi.org/10.48550/arXiv.2203.16908, arXiv:2203.16908, doi:10.48550/ARXIV.2203.16908.
  • [23] Dennis Moore and W.F. Smyth. An optimal algorithm to compute all the covers of a string. Information Processing Letters, 50(5):239–246, 1994. URL: https://www.sciencedirect.com/science/article/pii/002001909400045X, doi:10.1016/0020-0190(94)00045-X.
  • [24] Dennis Moore and W.F. Smyth. A correction to “an optimal algorithm to compute all the covers of a string”. Information Processing Letters, 54(2):101–103, 1995. URL: https://www.sciencedirect.com/science/article/pii/002001909400235Q, doi:10.1016/0020-0190(94)00235-Q.
  • [25] Alexandru Popa and Andrei Tanasescu. An output-sensitive algorithm for the minimization of 2-dimensional string covers. In T. V. Gopal and Junzo Watada, editors, Theory and Applications of Models of Computation - 15th Annual Conference, TAMC 2019, Kitakyushu, Japan, April 13-16, 2019, Proceedings, volume 11436 of Lecture Notes in Computer Science, pages 536–549. Springer, 2019. doi:10.1007/978-3-030-14812-6\_33.
  • [26] Jakub Radoszewski and Juliusz Straszyński. Efficient computation of 2-covers of a string. In 28th Annual European Symposium on Algorithms (ESA 2020). Schloss Dagstuhl-Leibniz-Zentrum für Informatik, 2020.
  • [27] Mikhail Rubinchik and Arseny M. Shur. Counting palindromes in substrings. In Gabriele Fici, Marinella Sciortino, and Rossano Venturini, editors, String Processing and Information Retrieval - 24th International Symposium, SPIRE 2017, Proceedings, volume 10508 of Lecture Notes in Computer Science, pages 290–303. Springer, 2017.
  • [28] Smyth. Computing the cover array in linear time. Algorithmica, 32:95–106, 2002.
  • [29] Dan E. Willard. New data structures for orthogonal range queries. SIAM J. Comput., 14(1):232–253, 1985. doi:10.1137/0214019.
  • [30] Hui Zhang, Qing Guo, and Costas S Iliopoulos. Algorithms for computing the λ𝜆\lambdaitalic_λ-regularities in strings. Fundamenta Informaticae, 84(1):33–49, 2008.

Appendix A Figures

Refer to caption
Figure 1: Occurrences of 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub in 𝗋𝗎𝗇𝗋𝗎𝗇\mathsf{run}sansserif_run (Claim 7). Grey strip is 𝗋𝗎𝗇𝗋𝗎𝗇\mathsf{run}sansserif_run, color strips indicate occurrences of 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub (one color for one substring 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub). The substrings drawn red, green, and blue realize, respectively, conditions 1, 3, and 4 of Claim 7. Dash lines show ranges covered by 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub in each case.

Appendix B Missing Proofs

The following two well-known lemmas are useful for the proofs in this section.

Lemma B.1 (Fine and Wilf Theorem [14]).

Every string having periods p,q𝑝𝑞p,qitalic_p , italic_q and length at least p+qgcd(p,q)𝑝𝑞𝑝𝑞p+q-\gcd(p,q)italic_p + italic_q - roman_gcd ( italic_p , italic_q ), has period gcd(p,q)𝑝𝑞\gcd(p,q)roman_gcd ( italic_p , italic_q ).

Lemma B.2 (Rephrased Three Squares Lemma [10]).

If a string S𝑆Sitalic_S has periodic prefixes X𝑋Xitalic_X, Y𝑌Yitalic_Y, and Z𝑍Zitalic_Z such that 𝗉𝖾𝗋(X)<𝗉𝖾𝗋(Y)<𝗉𝖾𝗋(Z)𝗉𝖾𝗋𝑋𝗉𝖾𝗋𝑌𝗉𝖾𝗋𝑍\mathsf{per}(X)<\mathsf{per}(Y)<\mathsf{per}(Z)sansserif_per ( italic_X ) < sansserif_per ( italic_Y ) < sansserif_per ( italic_Z ), then 𝗉𝖾𝗋(Z)𝗉𝖾𝗋(X)+𝗉𝖾𝗋(Y)𝗉𝖾𝗋𝑍𝗉𝖾𝗋𝑋𝗉𝖾𝗋𝑌\mathsf{per}(Z)\geq\mathsf{per}(X)+\mathsf{per}(Y)sansserif_per ( italic_Z ) ≥ sansserif_per ( italic_X ) + sansserif_per ( italic_Y ).

We complete here all missing proofs of Section 2.

Proof B.3 (Proof of Lemma 2.1).

Let p1<p2<<psubscript𝑝1subscript𝑝2subscript𝑝p_{1}<p_{2}<\cdots<p_{\ell}italic_p start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT < italic_p start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT < ⋯ < italic_p start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT be all periods of periodic prefixes of the string S[1..n]S[1..n]italic_S [ 1 . . italic_n ]. Consider three consecutive values pi,pi+1,pi+2subscript𝑝𝑖subscript𝑝𝑖1subscript𝑝𝑖2p_{i},p_{i+1},p_{i+2}italic_p start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT , italic_p start_POSTSUBSCRIPT italic_i + 1 end_POSTSUBSCRIPT , italic_p start_POSTSUBSCRIPT italic_i + 2 end_POSTSUBSCRIPT from this list. By Lemma B.2, pi+2>2pisubscript𝑝𝑖22subscript𝑝𝑖p_{i+2}>2p_{i}italic_p start_POSTSUBSCRIPT italic_i + 2 end_POSTSUBSCRIPT > 2 italic_p start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT. Since pnsubscript𝑝𝑛p_{\ell}\leq nitalic_p start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT ≤ italic_n, we immediately get =O(logn)𝑂𝑛\ell=O(\log n)roman_ℓ = italic_O ( roman_log italic_n ) as required.

Proof B.4 (Proof of Lemma 2.2).

Let b1<b2<<bsubscript𝑏1subscript𝑏2subscript𝑏b_{1}<b_{2}<\cdots<b_{\ell}italic_b start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT < italic_b start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT < ⋯ < italic_b start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT be all different lengths of aperiodic borders of the string S[1..n]S[1..n]italic_S [ 1 . . italic_n ]. Since for every i[1..1]i\in[1..\ell-1]italic_i ∈ [ 1 . . roman_ℓ - 1 ] the string S[1..bi]S[1..b_{i}]italic_S [ 1 . . italic_b start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT ] is a border of S[1..bi+1]S[1..b_{i+1}]italic_S [ 1 . . italic_b start_POSTSUBSCRIPT italic_i + 1 end_POSTSUBSCRIPT ], bi+1bisubscript𝑏𝑖1subscript𝑏𝑖b_{i+1}-b_{i}italic_b start_POSTSUBSCRIPT italic_i + 1 end_POSTSUBSCRIPT - italic_b start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT is a period of S[1..bi+1]S[1..b_{i+1}]italic_S [ 1 . . italic_b start_POSTSUBSCRIPT italic_i + 1 end_POSTSUBSCRIPT ]. Then (bi+1bi)>bi+1/2subscript𝑏𝑖1subscript𝑏𝑖subscript𝑏𝑖12(b_{i+1}-b_{i})>b_{i+1}/2( italic_b start_POSTSUBSCRIPT italic_i + 1 end_POSTSUBSCRIPT - italic_b start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT ) > italic_b start_POSTSUBSCRIPT italic_i + 1 end_POSTSUBSCRIPT / 2 because of aperiodicity. Hence bi+1>2bisubscript𝑏𝑖12subscript𝑏𝑖b_{i+1}>2b_{i}italic_b start_POSTSUBSCRIPT italic_i + 1 end_POSTSUBSCRIPT > 2 italic_b start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT. Since bnsubscript𝑏𝑛b_{\ell}\leq nitalic_b start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT ≤ italic_n, we get =O(logn)𝑂𝑛\ell=O(\log n)roman_ℓ = italic_O ( roman_log italic_n ).

For short periodic borders, a similar argument gives the inequalities bi+1>1.5bisubscript𝑏𝑖11.5subscript𝑏𝑖b_{i+1}>1.5b_{i}italic_b start_POSTSUBSCRIPT italic_i + 1 end_POSTSUBSCRIPT > 1.5 italic_b start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT, which also bound the number of borders by O(logn)𝑂𝑛O(\log n)italic_O ( roman_log italic_n ).

Proof B.5 (Proof of Lemma 2.4).

By definition of period, X[1..|X|ρ]=YX[1..|X|-\rho]=Yitalic_X [ 1 . . | italic_X | - italic_ρ ] = italic_Y implies X[ρ+1..|X|]=YX[\rho+1..|X|]=Yitalic_X [ italic_ρ + 1 . . | italic_X | ] = italic_Y. Since ρ|X|2𝜌𝑋2\rho\leq\frac{|X|}{2}italic_ρ ≤ divide start_ARG | italic_X | end_ARG start_ARG 2 end_ARG, the string Y𝑌Yitalic_Y covers all indices in X𝑋Xitalic_X with these two occurrences. It follows that Y𝑌Yitalic_Y covers every index i𝑖iitalic_i in S𝑆Sitalic_S that is covered by X𝑋Xitalic_X.

Proof B.6 (Proof of Lemma 2.5).

Clearly, an occurrence of a substring of length ρ𝜌\rhoitalic_ρ is contained in at most one ρ𝜌\rhoitalic_ρ-periodic run. Then the first statement of the lemma stems from the fact that a ρ𝜌\rhoitalic_ρ-periodic run covering i𝑖iitalic_i contains either S[iρ+1..i]S[i-\rho+1..i]italic_S [ italic_i - italic_ρ + 1 . . italic_i ] or S[i..i+ρ1]S[i..i+\rho-1]italic_S [ italic_i . . italic_i + italic_ρ - 1 ] (or both). For the second statement, some work is needed.

With respect to the index i𝑖iitalic_i, we call a run left (resp., right, central) if it contains the substring S[i2ρ+1..i]S[i-2\rho+1..i]italic_S [ italic_i - 2 italic_ρ + 1 . . italic_i ] (resp., S[i..i+2ρ1]S[i..i+2\rho-1]italic_S [ italic_i . . italic_i + 2 italic_ρ - 1 ], S[iρ..i+ρ]S[i-\rho..i+\rho]italic_S [ italic_i - italic_ρ . . italic_i + italic_ρ ]), where ρ𝜌\rhoitalic_ρ is the period of the run. Every highly-periodic run covering i𝑖iitalic_i possesses at least one of these three properties. By Lemma B.2, the number of right runs is O(logn)𝑂𝑛O(\log n)italic_O ( roman_log italic_n ); the dual of Lemma B.2 implies the same result for left runs. Now let A𝐴Aitalic_A and B𝐵Bitalic_B be two highly-periodic central runs, with ρ=𝗉𝖾𝗋(A)𝜌𝗉𝖾𝗋𝐴\rho=\mathsf{per}(A)italic_ρ = sansserif_per ( italic_A ), ρ=𝗉𝖾𝗋(B)superscript𝜌𝗉𝖾𝗋𝐵\rho^{\prime}=\mathsf{per}(B)italic_ρ start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT = sansserif_per ( italic_B ). If neither of A𝐴Aitalic_A, B𝐵Bitalic_B contains the other one, then their overlap is of length at least ρ+ρ𝜌superscript𝜌\rho+\rho^{\prime}italic_ρ + italic_ρ start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT (otherwise, one of them is not central). This overlap is a string with periods ρ𝜌\rhoitalic_ρ and ρsuperscript𝜌\rho^{\prime}italic_ρ start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT, and then has period gcd(ρ,ρ)𝜌superscript𝜌\gcd(\rho,\rho^{\prime})roman_gcd ( italic_ρ , italic_ρ start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT ) by Lemma B.1. Hence A𝐴Aitalic_A and B𝐵Bitalic_B are substrings of some gcd(ρ,ρ)𝜌superscript𝜌\gcd(\rho,\rho^{\prime})roman_gcd ( italic_ρ , italic_ρ start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT )-periodic run, contradicting our assumption that A𝐴Aitalic_A and B𝐵Bitalic_B are runs. Therefore, one of A,B𝐴𝐵A,Bitalic_A , italic_B contains the other. Without loss of generality, B𝐵Bitalic_B contains A𝐴Aitalic_A. Then ρρ𝜌superscript𝜌\rho\neq\rho^{\prime}italic_ρ ≠ italic_ρ start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT. The string A𝐴Aitalic_A has periods ρ𝜌\rhoitalic_ρ and ρsuperscript𝜌\rho^{\prime}italic_ρ start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT but no period gcd(ρ,ρ)𝜌superscript𝜌\gcd(\rho,\rho^{\prime})roman_gcd ( italic_ρ , italic_ρ start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT ) (otherwise it is not a ρ𝜌\rhoitalic_ρ-periodic run). Then |A|<ρ+ρ𝐴𝜌superscript𝜌|A|<\rho+\rho^{\prime}| italic_A | < italic_ρ + italic_ρ start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT by Lemma B.1; as |A|3ρ𝐴3𝜌|A|\geq 3\rho| italic_A | ≥ 3 italic_ρ, we have ρ>2ρsuperscript𝜌2𝜌\rho^{\prime}>2\rhoitalic_ρ start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT > 2 italic_ρ. This immediately implies the O(logn)𝑂𝑛O(\log n)italic_O ( roman_log italic_n ) upper bound on the number of central runs. The lemma now follows.

Proof B.7 (Proof of Lemma 2.6).

First notice that since S[i..j]S[i..j]italic_S [ italic_i . . italic_j ] is ρ𝜌\rhoitalic_ρ periodic, there is at least one ρ𝜌\rhoitalic_ρ-periodic run containing S[i..j]S[i..j]italic_S [ italic_i . . italic_j ]. Assume to the contrary that there are two different ρ𝜌\rhoitalic_ρ-periodic runs A𝐴Aitalic_A and B𝐵Bitalic_B containing S[i..j]S[i..j]italic_S [ italic_i . . italic_j ]. Since A𝐴Aitalic_A and B𝐵Bitalic_B overlap by a range of length ρ𝜌\rhoitalic_ρ, the entire range covered by A𝐴Aitalic_A and B𝐵Bitalic_B is ρ𝜌\rhoitalic_ρ-periodic, which contradicts the maximality of A𝐴Aitalic_A and B𝐵Bitalic_B.

Proof B.8 (Proof of Lemma 2.7).

Assume to the contrary that S[x..y+1]S[x..y+1]italic_S [ italic_x . . italic_y + 1 ] is ρsuperscript𝜌\rho^{\prime}italic_ρ start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT-periodic for some ρsuperscript𝜌\rho^{\prime}italic_ρ start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT. If ρsuperscript𝜌\rho^{\prime}italic_ρ start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT is divisible by ρ𝜌\rhoitalic_ρ, we have S[y+1]=S[yρ+1]=S[yρ+1]𝑆delimited-[]𝑦1𝑆delimited-[]𝑦superscript𝜌1𝑆delimited-[]𝑦𝜌1S[y+1]=S[y-\rho^{\prime}+1]=S[y-\rho+1]italic_S [ italic_y + 1 ] = italic_S [ italic_y - italic_ρ start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT + 1 ] = italic_S [ italic_y - italic_ρ + 1 ] where the second equality is due to the 𝗉𝖾𝗋(S[x..y])=ρ\mathsf{per}(S[x..y])=\rhosansserif_per ( italic_S [ italic_x . . italic_y ] ) = italic_ρ. Then S[x..y+1]S[x..y+1]italic_S [ italic_x . . italic_y + 1 ] is p𝑝pitalic_p-periodic, contradicting the condition of the lemma. If ρsuperscript𝜌\rho^{\prime}italic_ρ start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT is not divisible by ρ𝜌\rhoitalic_ρ, by Lemma B.1 we have that 𝗉𝖾𝗋(S[x..y])\mathsf{per}(S[x..y])sansserif_per ( italic_S [ italic_x . . italic_y ] ) is smaller than ρ𝜌\rhoitalic_ρ, contradicting the definition of ρ𝜌\rhoitalic_ρ-periodic.

Proof B.9 (Proof of Lemma C.1).

We present a proof for a set of 2222-dimensional ranges, i.e., rectangles. This proof can be easily generalized for any constant dimension. Consider the infinite extensions of every side of a rectangle R𝑅R\in\mathcal{R}italic_R ∈ caligraphic_R to both directions. Namely, for a rectangle R=[x1..x2]×[y1..y2]R=[x_{1}..x_{2}]\times[y_{1}..y_{2}]italic_R = [ italic_x start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT . . italic_x start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ] × [ italic_y start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT . . italic_y start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ], the infinite extensions of the sides of R𝑅Ritalic_R are the lines x=x1𝑥subscript𝑥1x=x_{1}italic_x = italic_x start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT, x=x2𝑥subscript𝑥2x=x_{2}italic_x = italic_x start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT, y=y1𝑦subscript𝑦1y=y_{1}italic_y = italic_y start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT and y=y2𝑦subscript𝑦2y=y_{2}italic_y = italic_y start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT. Clearly, the infinite extensions of the sides of all rectangles in \mathcal{R}caligraphic_R partition the plane into O(||2)=O(1)𝑂superscript2𝑂1O(|\mathcal{R}|^{2})=O(1)italic_O ( | caligraphic_R | start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT ) = italic_O ( 1 ) rectangles. Every rectangle in this partition is either contained in a rectangle of R𝑅Ritalic_R, or is disjoint from all rectangles of R𝑅Ritalic_R. The set of rectangles in the partition disjoint from all rectangles of R𝑅Ritalic_R satisfies the claim. Moreover, this set can be computed in O(1)𝑂1O(1)italic_O ( 1 ) time straightforwardly.

Appendix C 2CNF Data Structure

In this section we prove Lemma 4.1. We use the following auxiliary lemma to manipulate d𝑑ditalic_d-dimensional ranges.

Lemma C.1 (Inverse of Ranges).

Let \mathcal{R}caligraphic_R be a set of O(1)𝑂1O(1)italic_O ( 1 ) d𝑑ditalic_d-dimensional ranges, where d𝑑ditalic_d is an integer constant. There is a set ¯¯\overline{\mathcal{R}}over¯ start_ARG caligraphic_R end_ARG of O(1)𝑂1O(1)italic_O ( 1 ) d𝑑ditalic_d-dimensional ranges such that R¯R=[..]dRR\bigcup_{R\in\overline{\mathcal{R}}}R=[-\infty..\infty]^{d}\setminus\bigcup_{R% \in\mathcal{R}}R⋃ start_POSTSUBSCRIPT italic_R ∈ over¯ start_ARG caligraphic_R end_ARG end_POSTSUBSCRIPT italic_R = [ - ∞ . . ∞ ] start_POSTSUPERSCRIPT italic_d end_POSTSUPERSCRIPT ∖ ⋃ start_POSTSUBSCRIPT italic_R ∈ caligraphic_R end_POSTSUBSCRIPT italic_R. Moreover, the set R¯¯𝑅\overline{R}over¯ start_ARG italic_R end_ARG can be computed in O(1)𝑂1O(1)italic_O ( 1 ) time given \mathcal{R}caligraphic_R.

See 4.1

Proof C.2.

Let d=d+dr𝑑subscript𝑑subscript𝑑𝑟d=d_{\ell}+d_{r}italic_d = italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT + italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT. We build a set \mathcal{B}caligraphic_B of d𝑑ditalic_d-dimensional ranges, processing each pair (i,i)subscript𝑖subscript𝑖(\mathcal{L}_{i},\mathcal{R}_{i})( caligraphic_L start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT , caligraphic_R start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT ) as follows. We start with the set Bisubscript𝐵𝑖B_{i}italic_B start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT of d𝑑ditalic_d-dimensional ranges defined by

Bi={L×[..]drLi}{[..]d×RRi}.B_{i}=\{L\times[-\infty..\infty]^{d_{r}}\mid L\in\mathcal{L}_{i}\}\cup\{[-% \infty..\infty]^{d_{\ell}}\times R\mid R\in\mathcal{R}_{i}\}.italic_B start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT = { italic_L × [ - ∞ . . ∞ ] start_POSTSUPERSCRIPT italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT end_POSTSUPERSCRIPT ∣ italic_L ∈ caligraphic_L start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT } ∪ { [ - ∞ . . ∞ ] start_POSTSUPERSCRIPT italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT end_POSTSUPERSCRIPT × italic_R ∣ italic_R ∈ caligraphic_R start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT } .

As |Bi|=|i|+|i|=O(1)subscript𝐵𝑖subscript𝑖subscript𝑖𝑂1|B_{i}|=|\mathcal{L}_{i}|+|\mathcal{R}_{i}|=O(1)| italic_B start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT | = | caligraphic_L start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT | + | caligraphic_R start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT | = italic_O ( 1 ), we apply Lemma C.1 to get, in O(1)𝑂1O(1)italic_O ( 1 ) time, its inverse set of ranges Bi¯¯subscript𝐵𝑖\overline{B_{i}}over¯ start_ARG italic_B start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT end_ARG, which is also of size O(1)𝑂1O(1)italic_O ( 1 ). Now let =i[n]Bi¯subscript𝑖delimited-[]𝑛¯subscript𝐵𝑖\mathcal{B}=\bigcup_{i\in[n]}\overline{B_{i}}caligraphic_B = ⋃ start_POSTSUBSCRIPT italic_i ∈ [ italic_n ] end_POSTSUBSCRIPT over¯ start_ARG italic_B start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT end_ARG.

We preprocess the set \mathcal{B}caligraphic_B into a range stabbing data structure (Lemma 2.10). To answer 𝗊𝗎𝖾𝗋𝗒(p,pr)𝗊𝗎𝖾𝗋𝗒subscript𝑝subscript𝑝𝑟\mathsf{query}(p_{\ell},p_{r})sansserif_query ( italic_p start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT , italic_p start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ), where p=(x1,,xd)subscript𝑝subscript𝑥1subscript𝑥subscript𝑑p_{\ell}=(x_{1},\ldots,x_{d_{\ell}})italic_p start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT = ( italic_x start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT , … , italic_x start_POSTSUBSCRIPT italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT end_POSTSUBSCRIPT ) and pr=(y1,,ydr)subscript𝑝𝑟subscript𝑦1subscript𝑦subscript𝑑𝑟p_{r}=(y_{1},\ldots,y_{d_{r}})italic_p start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT = ( italic_y start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT , … , italic_y start_POSTSUBSCRIPT italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT end_POSTSUBSCRIPT ), we perform the Existence query to this structure with the d𝑑ditalic_d-dimensional point p=(x1,,xd,y1,,ydr)𝑝subscript𝑥1subscript𝑥subscript𝑑subscript𝑦1subscript𝑦subscript𝑑𝑟p=(x_{1},\ldots,x_{d_{\ell}},y_{1},\ldots,y_{d_{r}})italic_p = ( italic_x start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT , … , italic_x start_POSTSUBSCRIPT italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT end_POSTSUBSCRIPT , italic_y start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT , … , italic_y start_POSTSUBSCRIPT italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT end_POSTSUBSCRIPT ) and report the negation of the obtained answer. The required time complexities follow from Lemma 2.10.

Correctness.

We need to prove that pBB𝑝subscript𝐵𝐵p\notin\bigcup_{B\in\mathcal{B}}Bitalic_p ∉ ⋃ start_POSTSUBSCRIPT italic_B ∈ caligraphic_B end_POSTSUBSCRIPT italic_B if and only if for every i[n]𝑖delimited-[]𝑛i\in[n]italic_i ∈ [ italic_n ] either pLsubscript𝑝𝐿p_{\ell}\in Litalic_p start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT ∈ italic_L for some Li𝐿subscript𝑖L\in\mathcal{L}_{i}italic_L ∈ caligraphic_L start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT or prRsubscript𝑝𝑟𝑅p_{r}\in Ritalic_p start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ∈ italic_R for some Ri𝑅subscript𝑖R\in\mathcal{R}_{i}italic_R ∈ caligraphic_R start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT.

Let pBB𝑝subscript𝐵𝐵p\notin\bigcup_{B\in\mathcal{B}}Bitalic_p ∉ ⋃ start_POSTSUBSCRIPT italic_B ∈ caligraphic_B end_POSTSUBSCRIPT italic_B and let i[n]𝑖delimited-[]𝑛i\in[n]italic_i ∈ [ italic_n ]. By definition of \mathcal{B}caligraphic_B, pBBi¯B𝑝subscript𝐵¯subscript𝐵𝑖𝐵p\notin\bigcup_{B\in\overline{B_{i}}}Bitalic_p ∉ ⋃ start_POSTSUBSCRIPT italic_B ∈ over¯ start_ARG italic_B start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT end_ARG end_POSTSUBSCRIPT italic_B. Then pBBiB𝑝subscript𝐵subscript𝐵𝑖𝐵p\in\bigcup_{B\in B_{i}}Bitalic_p ∈ ⋃ start_POSTSUBSCRIPT italic_B ∈ italic_B start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT end_POSTSUBSCRIPT italic_B. By definition of Bisubscript𝐵𝑖B_{i}italic_B start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT, this implies that pLsubscript𝑝𝐿p_{\ell}\in Litalic_p start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT ∈ italic_L for some Li𝐿subscript𝑖L\in\mathcal{L}_{i}italic_L ∈ caligraphic_L start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT of prRsubscript𝑝𝑟𝑅p_{r}\in Ritalic_p start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ∈ italic_R for some Ri𝑅subscript𝑖R\in\mathcal{R}_{i}italic_R ∈ caligraphic_R start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT.

For the converse note that if pLsubscript𝑝𝐿p_{\ell}\in Litalic_p start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT ∈ italic_L for some Li𝐿subscript𝑖L\in\mathcal{L}_{i}italic_L ∈ caligraphic_L start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT of prRsubscript𝑝𝑟𝑅p_{r}\in Ritalic_p start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ∈ italic_R for some Ri𝑅subscript𝑖R\in\mathcal{R}_{i}italic_R ∈ caligraphic_R start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT, then pB𝑝𝐵p\in Bitalic_p ∈ italic_B for some BBi𝐵subscript𝐵𝑖B\in B_{i}italic_B ∈ italic_B start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT and hence pB𝑝superscript𝐵p\notin B^{\prime}italic_p ∉ italic_B start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT for all BBi¯superscript𝐵¯subscript𝐵𝑖B^{\prime}\in\overline{B_{i}}italic_B start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT ∈ over¯ start_ARG italic_B start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT end_ARG. If this is the case for all i𝑖iitalic_i, then pBB𝑝subscript𝐵𝐵p\notin\bigcup_{B\in\mathcal{B}}Bitalic_p ∉ ⋃ start_POSTSUBSCRIPT italic_B ∈ caligraphic_B end_POSTSUBSCRIPT italic_B by definition.

Appendix D 2-Covers Oracle

In this section, we provide a detailed proof of Theorem 1.3. We distinguish between two types of 2222-covers. A 2222-cover (C1,C2)subscript𝐶1subscript𝐶2(C_{1},C_{2})( italic_C start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT , italic_C start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ) is a prefix-suffix cover (ps-cover) if C1subscript𝐶1C_{1}italic_C start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT is a prefix of S𝑆Sitalic_S and C2subscript𝐶2C_{2}italic_C start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT is a suffix of S𝑆Sitalic_S (or vice versa). A 2222-cover is a border-substring cover (bs-cover) if C1subscript𝐶1C_{1}italic_C start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT is a border of S𝑆Sitalic_S and C2subscript𝐶2C_{2}italic_C start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT is a substring of S𝑆Sitalic_S (or vice versa). Note that every 2222-cover can be classified into one of these two types. More specifically, the oracle queries each data structure twice: once for (C1,C2)subscript𝐶1subscript𝐶2(C_{1},C_{2})( italic_C start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT , italic_C start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ) and once for (C2,C1)subscript𝐶2subscript𝐶1(C_{2},C_{1})( italic_C start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT , italic_C start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT ).

Our data structure consists of two independent data structures, one for checking if (C1,C2)subscript𝐶1subscript𝐶2(C_{1},C_{2})( italic_C start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT , italic_C start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ) is a prefix-suffix cover, and one for checking if it is a border-substring cover. When receiving a query, the data structure checks both options and answers accordingly.

Common Preprocess.

In both data structures, the following preprocess on S𝑆Sitalic_S is applied in addition to the preprocessing phase described in Section 3. The algorithm constructs an interval stabbing data structure Dhpssubscriptsuperscript𝐷𝑠𝑝D^{s}_{hp}italic_D start_POSTSUPERSCRIPT italic_s end_POSTSUPERSCRIPT start_POSTSUBSCRIPT italic_h italic_p end_POSTSUBSCRIPT that contains an interval [i..j][i..j][ italic_i . . italic_j ] for every highly periodic run S[i..j]S[i..j]italic_S [ italic_i . . italic_j ] in S𝑆Sitalic_S using Lemma 2.10. This preprocess takes O(nlog2n)𝑂𝑛superscript2𝑛O(n\log^{2}n)italic_O ( italic_n roman_log start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT italic_n ) time.

D.1 Prefix-Suffix Oracle

In this section, we describe the oracle checking if (U1,U2)=(S[1..p],S[x..y])(U_{1},U_{2})=(S[1..p],S[x..y])( italic_U start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT , italic_U start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ) = ( italic_S [ 1 . . italic_p ] , italic_S [ italic_x . . italic_y ] ) is a ps-cover.

Construction.

We partition prefixes and suffixes into groups. The groups 𝗉𝗉𝖾𝗋(ρ)𝗉𝗉𝖾𝗋𝜌\mathsf{pper}(\rho)sansserif_pper ( italic_ρ ) and 𝗌𝗉𝖾𝗋(ρ)𝗌𝗉𝖾𝗋𝜌\mathsf{sper}(\rho)sansserif_sper ( italic_ρ ) consist of all highly ρ𝜌\rhoitalic_ρ-periodic prefixes (resp., suffixes). The groups 𝗉𝖺𝗉𝖾𝗋(k)𝗉𝖺𝗉𝖾𝗋𝑘\mathsf{paper}(k)sansserif_paper ( italic_k ) and 𝗌𝖺𝗉𝖾𝗋(k)𝗌𝖺𝗉𝖾𝗋𝑘\mathsf{saper}(k)sansserif_saper ( italic_k ) consist of all not highly periodic prefixes (resp., suffixes) that have length in the range [1.5k..1.5k+1]delimited-[]superscript1.5𝑘superscript..1.5𝑘1[1.5^{k}..1.5^{k+1}][ 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT ..1.5 start_POSTSUPERSCRIPT italic_k + 1 end_POSTSUPERSCRIPT ]. Let

𝒫𝒫\displaystyle\mathcal{P}caligraphic_P ={𝗉𝖺𝗉𝖾𝗋(k)1.5kn}{𝗉𝗉𝖾𝗋(ρ)ρ is a prefix period},absentconditional-set𝗉𝖺𝗉𝖾𝗋𝑘superscript1.5𝑘𝑛conditional-set𝗉𝗉𝖾𝗋𝜌𝜌 is a prefix period\displaystyle=\{\mathsf{paper}(k)\mid 1.5^{k}\leq n\}\cup\{\mathsf{pper}(\rho)% \mid\rho\text{ is a prefix period}\},= { sansserif_paper ( italic_k ) ∣ 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT ≤ italic_n } ∪ { sansserif_pper ( italic_ρ ) ∣ italic_ρ is a prefix period } ,
𝒮𝒮\displaystyle\mathcal{S}caligraphic_S ={𝗌𝖺𝗉𝖾𝗋(k)1.5kn}{𝗌𝗉𝖾𝗋(ρ)ρ is a suffix period}.absentconditional-set𝗌𝖺𝗉𝖾𝗋𝑘superscript1.5𝑘𝑛conditional-set𝗌𝗉𝖾𝗋𝜌𝜌 is a suffix period\displaystyle=\{\mathsf{saper}(k)\mid 1.5^{k}\leq n\}\cup\{\mathsf{sper}(\rho)% \mid\rho\text{ is a suffix period}\}.= { sansserif_saper ( italic_k ) ∣ 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT ≤ italic_n } ∪ { sansserif_sper ( italic_ρ ) ∣ italic_ρ is a suffix period } .

As the number of prefix/suffix periods is O(logn)𝑂𝑛O(\log n)italic_O ( roman_log italic_n ) by Lemma 2.1, one has |𝒫|,|𝒮|O(logn)𝒫𝒮𝑂𝑛|\mathcal{P}|,|\mathcal{S}|\in O(\log n)| caligraphic_P | , | caligraphic_S | ∈ italic_O ( roman_log italic_n ).

The algorithm processes each pair (𝗉𝗋𝖾𝖿,𝗌𝗎𝖿𝖿)𝒫×𝒮𝗉𝗋𝖾𝖿𝗌𝗎𝖿𝖿𝒫𝒮(\mathsf{pref},\mathsf{suff})\in\mathcal{P}\times\mathcal{S}( sansserif_pref , sansserif_suff ) ∈ caligraphic_P × caligraphic_S as follows.

  • If 𝗉𝗋𝖾𝖿=𝗉𝖺𝗉𝖾𝗋(k)𝗉𝗋𝖾𝖿𝗉𝖺𝗉𝖾𝗋𝑘\mathsf{pref}=\mathsf{paper}(k)sansserif_pref = sansserif_paper ( italic_k ), it uses, for every i[n]𝑖delimited-[]𝑛i\in[n]italic_i ∈ [ italic_n ], Lemma 3.1 with parameters f=1𝑓1f=1italic_f = 1, i𝑖iitalic_i, and k𝑘kitalic_k to build a set of rectangles isubscript𝑖\mathcal{R}_{i}caligraphic_R start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT. From the construction given in Claim 1 we see that each rectangle in isubscript𝑖\mathcal{R}_{i}caligraphic_R start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT has the form R=[0..0]×[r1..r2]R=[0..0]\times[r_{1}..r_{2}]italic_R = [ 0..0 ] × [ italic_r start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT . . italic_r start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ], and every prefix in 𝗉𝖺𝗉𝖾𝗋(k)𝗉𝖺𝗉𝖾𝗋𝑘\mathsf{paper}(k)sansserif_paper ( italic_k ) is associated with a point of the form p=(0,r)𝑝0𝑟p=(0,r)italic_p = ( 0 , italic_r ). Then the inclusion pR𝑝𝑅p\in Ritalic_p ∈ italic_R is decided solely from the second coordinates. We refer to the first coordinate as fixed and we drop it to reduce the dimension. We thus define 𝒫isubscript𝒫𝑖\mathcal{P}_{i}caligraphic_P start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT to be the set of projections of all rectangles from isubscript𝑖\mathcal{R}_{i}caligraphic_R start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT to the second coordinate.

  • If 𝗉𝗋𝖾𝖿=𝗉𝗉𝖾𝗋(ρ)𝗉𝗋𝖾𝖿𝗉𝗉𝖾𝗋𝜌\mathsf{pref}=\mathsf{pper}(\rho)sansserif_pref = sansserif_pper ( italic_ρ ), it uses, for every i[n]𝑖delimited-[]𝑛i\in[n]italic_i ∈ [ italic_n ], Lemma 3.2 with parameters f=1𝑓1f=1italic_f = 1, i𝑖iitalic_i, and ρ𝜌\rhoitalic_ρ to build a set of cuboids 𝒞isubscript𝒞𝑖\mathcal{C}_{i}caligraphic_C start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT. Similar to the previous case we know that each Ri𝑅subscript𝑖R\in\mathcal{R}_{i}italic_R ∈ caligraphic_R start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT has the first range [0..0]delimited-[]0..0[0..0][ 0..0 ] and every prefix in 𝗉𝗉𝖾𝗋(ρ)𝗉𝗉𝖾𝗋𝜌\mathsf{pper}(\rho)sansserif_pper ( italic_ρ ) is associated with a point having zero first coordinate. Accordingly, we drop the fixed first coordinate and define 𝒫isubscript𝒫𝑖\mathcal{P}_{i}caligraphic_P start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT as the set of projections, to the last two coordinates, of all cuboids from 𝒞isubscript𝒞𝑖\mathcal{C}_{i}caligraphic_C start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT.

The sets 𝒮isubscript𝒮𝑖\mathcal{S}_{i}caligraphic_S start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT are defined in a symmetric way from 𝗌𝗎𝖿𝖿𝗌𝗎𝖿𝖿\mathsf{suff}sansserif_suff; here we drop the fixed second coordinate.

Thus, the algorithm obtain the set 𝖯𝖺𝗂𝗋𝗌={(𝒫1,𝒮1),(𝒫2,𝒮2),,(𝒫n,𝒮n)}𝖯𝖺𝗂𝗋𝗌subscript𝒫1subscript𝒮1subscript𝒫2subscript𝒮2subscript𝒫𝑛subscript𝒮𝑛\mathsf{Pairs}=\{(\mathcal{P}_{1},\mathcal{S}_{1}),(\mathcal{P}_{2},\mathcal{S% }_{2}),\ldots,(\mathcal{P}_{n},\mathcal{S}_{n})\}sansserif_Pairs = { ( caligraphic_P start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT , caligraphic_S start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT ) , ( caligraphic_P start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT , caligraphic_S start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ) , … , ( caligraphic_P start_POSTSUBSCRIPT italic_n end_POSTSUBSCRIPT , caligraphic_S start_POSTSUBSCRIPT italic_n end_POSTSUBSCRIPT ) } and builds a 2𝖢𝖭𝖥2𝖢𝖭𝖥2\mathsf{CNF}2 sansserif_CNF data structure 𝖢𝖭𝖥𝗉𝗋𝖾𝖿,𝗌𝗎𝖿𝖿subscript𝖢𝖭𝖥𝗉𝗋𝖾𝖿𝗌𝗎𝖿𝖿\mathsf{CNF}_{\mathsf{pref},\mathsf{suff}}sansserif_CNF start_POSTSUBSCRIPT sansserif_pref , sansserif_suff end_POSTSUBSCRIPT of Lemma 4.1 for the set 𝖯𝖺𝗂𝗋𝗌𝖯𝖺𝗂𝗋𝗌\mathsf{Pairs}sansserif_Pairs with the following dimensions:

d={1if 𝗉𝗋𝖾𝖿=𝗉𝖺𝗉𝖾𝗋(k)2if 𝗉𝗋𝖾𝖿=𝗉𝗉𝖾𝗋(ρ) and dr={1if 𝗌𝗎𝖿𝖿=𝗌𝖺𝗉𝖾𝗋(k)2if 𝗌𝗎𝖿𝖿=𝗌𝗉𝖾𝗋(ρ).subscript𝑑cases1if 𝗉𝗋𝖾𝖿𝗉𝖺𝗉𝖾𝗋𝑘2if 𝗉𝗋𝖾𝖿𝗉𝗉𝖾𝗋𝜌 and subscript𝑑𝑟cases1if 𝗌𝗎𝖿𝖿𝗌𝖺𝗉𝖾𝗋𝑘2if 𝗌𝗎𝖿𝖿𝗌𝗉𝖾𝗋𝜌d_{\ell}=\begin{cases}1&\text{if }\mathsf{pref}=\mathsf{paper}(k)\\ 2&\text{if }\mathsf{pref}=\mathsf{pper}(\rho)\end{cases}\text{ and }d_{r}=% \begin{cases}1&\text{if }\mathsf{suff}=\mathsf{saper}(k)\\ 2&\text{if }\mathsf{suff}=\mathsf{sper}(\rho)\end{cases}.italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT = { start_ROW start_CELL 1 end_CELL start_CELL if sansserif_pref = sansserif_paper ( italic_k ) end_CELL end_ROW start_ROW start_CELL 2 end_CELL start_CELL if sansserif_pref = sansserif_pper ( italic_ρ ) end_CELL end_ROW and italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT = { start_ROW start_CELL 1 end_CELL start_CELL if sansserif_suff = sansserif_saper ( italic_k ) end_CELL end_ROW start_ROW start_CELL 2 end_CELL start_CELL if sansserif_suff = sansserif_sper ( italic_ρ ) end_CELL end_ROW .
Query.

For a query (S[1..p],S[x..y])(S[1..p],S[x..y])( italic_S [ 1 . . italic_p ] , italic_S [ italic_x . . italic_y ] ) the algorithm first verifies that S[x..y]S[x..y]italic_S [ italic_x . . italic_y ] is a suffix of S𝑆Sitalic_S by checking if 𝖫𝖢𝖯R(y,n)yx+1superscript𝖫𝖢𝖯𝑅𝑦𝑛𝑦𝑥1\mathsf{LCP}^{R}(y,n)\geq y-x+1sansserif_LCP start_POSTSUPERSCRIPT italic_R end_POSTSUPERSCRIPT ( italic_y , italic_n ) ≥ italic_y - italic_x + 1. If it is not a suffix, the oracle answers False. Otherwise, the algorithm queries the 𝖨𝖯𝖬Ssubscript𝖨𝖯𝖬𝑆\mathsf{IPM}_{S}sansserif_IPM start_POSTSUBSCRIPT italic_S end_POSTSUBSCRIPT data structure (Lemma 2.13) with Periodic(S[1..p]S[1..p]italic_S [ 1 . . italic_p ]). If the answer is “aperiodic” or a number ρ>p/3𝜌𝑝3\rho>p/3italic_ρ > italic_p / 3, the prefix is not highly periodic. So the algorithm computes k=log1.5p𝑘subscript1.5𝑝k=\left\lfloor{\log_{1.5}p}\right\rflooritalic_k = ⌊ roman_log start_POSTSUBSCRIPT 1.5 end_POSTSUBSCRIPT italic_p ⌋ and sets 𝗉𝗋𝖾𝖿=𝗉𝖺𝗉𝖾𝗋(k)𝗉𝗋𝖾𝖿𝗉𝖺𝗉𝖾𝗋𝑘\mathsf{pref}=\mathsf{paper}(k)sansserif_pref = sansserif_paper ( italic_k ) and p=(p1)subscript𝑝𝑝1p_{\ell}=(p-1)italic_p start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT = ( italic_p - 1 ) (recall that the first coordinate is dropped!). Otherwise, the prefix is highly ρ𝜌\rhoitalic_ρ-periodic. In this case, the algorithm sets 𝗉𝗋𝖾𝖿=𝗉𝗉𝖾𝗋(ρ)𝗉𝗋𝖾𝖿𝗉𝗉𝖾𝗋𝜌\mathsf{pref}=\mathsf{pper}(\rho)sansserif_pref = sansserif_pper ( italic_ρ ) and p=(dr,qr)subscript𝑝subscript𝑑𝑟subscript𝑞𝑟p_{\ell}=(d_{r},q_{r})italic_p start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT = ( italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT , italic_q start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ) for the integers dr[0..ρ1]d_{r}\in[0..\rho-1]italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ∈ [ 0 . . italic_ρ - 1 ] and qrsubscript𝑞𝑟q_{r}italic_q start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT such that p=qrρ+dr𝑝subscript𝑞𝑟𝜌subscript𝑑𝑟p=q_{r}\cdot\rho+d_{r}italic_p = italic_q start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ⋅ italic_ρ + italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT. Similarly, the algorithm queries 𝖨𝖯𝖬Ssubscript𝖨𝖯𝖬𝑆\mathsf{IPM}_{S}sansserif_IPM start_POSTSUBSCRIPT italic_S end_POSTSUBSCRIPT with Periodic(S[x..y]S[x..y]italic_S [ italic_x . . italic_y ]). If S[x..y]S[x..y]italic_S [ italic_x . . italic_y ] is not highly periodic, the algorithm computes k=log1.5(yx+1)𝑘subscript1.5𝑦𝑥1k=\left\lfloor{\log_{1.5}(y-x+1)}\right\rflooritalic_k = ⌊ roman_log start_POSTSUBSCRIPT 1.5 end_POSTSUBSCRIPT ( italic_y - italic_x + 1 ) ⌋ and sets 𝗌𝗎𝖿𝖿=𝗌𝖺𝗉𝖾𝗋(k)𝗌𝗎𝖿𝖿𝗌𝖺𝗉𝖾𝗋𝑘\mathsf{suff}=\mathsf{saper}(k)sansserif_suff = sansserif_saper ( italic_k ) and pr=(yx)subscript𝑝𝑟𝑦𝑥p_{r}=(y-x)italic_p start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT = ( italic_y - italic_x ). Otherwise, it is highly ρ𝜌\rhoitalic_ρ-periodic, and the algorithm sets 𝗌𝗎𝖿𝖿=𝗌𝗉𝖾𝗋(ρ)𝗌𝗎𝖿𝖿𝗌𝗉𝖾𝗋𝜌\mathsf{suff}=\mathsf{sper}(\rho)sansserif_suff = sansserif_sper ( italic_ρ ) and pr=(d,q)subscript𝑝𝑟subscript𝑑subscript𝑞p_{r}=(d_{\ell},q_{\ell})italic_p start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT = ( italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT , italic_q start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT ) for the integers d[0..ρ1]d_{\ell}\in[0..\rho-1]italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT ∈ [ 0 . . italic_ρ - 1 ] and qsubscript𝑞q_{\ell}italic_q start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT such that yx+1=qρ+d𝑦𝑥1subscript𝑞𝜌subscript𝑑y-x+1=q_{\ell}\cdot\rho+d_{\ell}italic_y - italic_x + 1 = italic_q start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT ⋅ italic_ρ + italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT. Finally, the algorithm queries the structure 𝖢𝖭𝖥𝗉𝗋𝖾𝖿,𝗌𝗎𝖿𝖿subscript𝖢𝖭𝖥𝗉𝗋𝖾𝖿𝗌𝗎𝖿𝖿\mathsf{CNF}_{\mathsf{pref},\mathsf{suff}}sansserif_CNF start_POSTSUBSCRIPT sansserif_pref , sansserif_suff end_POSTSUBSCRIPT with the pair of points (p,pr)subscript𝑝subscript𝑝𝑟(p_{\ell},p_{r})( italic_p start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT , italic_p start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ) and returns the obtained answer.

Complexity.

The preprocessing phase takes O(nlog2n)𝑂𝑛superscript2𝑛O(n\log^{2}n)italic_O ( italic_n roman_log start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT italic_n ) time. The periods of highly periodic prefixes and these prefixes themselves can be found in O(n)𝑂𝑛O(n)italic_O ( italic_n ) time by O(n)𝑂𝑛O(n)italic_O ( italic_n ) 𝖫𝖢𝖯𝖫𝖢𝖯\mathsf{LCP}sansserif_LCP queries 𝖫𝖢𝖯S(S[1..n],S[ρ+1..n])\mathsf{LCP}_{S}(S[1..n],S[\rho+1..n])sansserif_LCP start_POSTSUBSCRIPT italic_S end_POSTSUBSCRIPT ( italic_S [ 1 . . italic_n ] , italic_S [ italic_ρ + 1 . . italic_n ] ). Highly periodic suffixes are operated in a symmetric way. For each pair (𝗉𝗋𝖾𝖿,𝗌𝗎𝖿𝖿)𝗉𝗋𝖾𝖿𝗌𝗎𝖿𝖿(\mathsf{pref},\mathsf{suff})( sansserif_pref , sansserif_suff ), the algorithm iterates every i[n]𝑖delimited-[]𝑛i\in[n]italic_i ∈ [ italic_n ] and applies Lemma 3.1 or Lemma 3.2, which takes O(nlog2n)𝑂𝑛superscript2𝑛O(n\log^{2}n)italic_O ( italic_n roman_log start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT italic_n ) time. Then, the algorithm constructs a 2𝖢𝖭𝖥𝖢𝖭𝖥\mathsf{CNF}sansserif_CNF data structure of dimension d+dr2+2=4subscript𝑑subscript𝑑𝑟224d_{\ell}+d_{r}\leq 2+2=4italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT + italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ≤ 2 + 2 = 4, which takes O(nlog3n)𝑂𝑛superscript3𝑛O(n\log^{3}n)italic_O ( italic_n roman_log start_POSTSUPERSCRIPT 3 end_POSTSUPERSCRIPT italic_n ) time by Lemma 4.1. Summing over O(log2n)𝑂superscript2𝑛O(\log^{2}n)italic_O ( roman_log start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT italic_n ) distinct pairs (𝗉𝗋𝖾𝖿,𝗌𝗎𝖿𝖿)𝗉𝗋𝖾𝖿𝗌𝗎𝖿𝖿(\mathsf{pref},\mathsf{suff})( sansserif_pref , sansserif_suff ), we get the total running time of O(nlog5n)𝑂𝑛superscript5𝑛O(n\log^{5}n)italic_O ( italic_n roman_log start_POSTSUPERSCRIPT 5 end_POSTSUPERSCRIPT italic_n ).

The query complexity is dominated by a single query to a 2𝖢𝖭𝖥2𝖢𝖭𝖥2\mathsf{CNF}2 sansserif_CNF data structure with d+dr4subscript𝑑subscript𝑑𝑟4d_{\ell}+d_{r}\leq 4italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT + italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ≤ 4, which requires O(log3n)𝑂superscript3𝑛O(\log^{3}n)italic_O ( roman_log start_POSTSUPERSCRIPT 3 end_POSTSUPERSCRIPT italic_n ) time by Lemma 4.1.

Correctness.

By definition, (S[1..p],S[x..y])(S[1..p],S[x..y])( italic_S [ 1 . . italic_p ] , italic_S [ italic_x . . italic_y ] ) is a 2222-cover of S𝑆Sitalic_S if and only if for every i𝑖iitalic_i we have (S[1..p],S[x..y])(S[1..p],S[x..y])( italic_S [ 1 . . italic_p ] , italic_S [ italic_x . . italic_y ] ) covers i𝑖iitalic_i. Denote by 𝖢𝗈𝗏𝖾𝗋isubscript𝖢𝗈𝗏𝖾𝗋𝑖\mathsf{Cover}_{i}sansserif_Cover start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT the event in which either psubscript𝑝p_{\ell}italic_p start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT is in some range of 𝒫isubscript𝒫𝑖\mathcal{P}_{i}caligraphic_P start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT or prsubscript𝑝𝑟p_{r}italic_p start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT is in some range of 𝒮isubscript𝒮𝑖\mathcal{S}_{i}caligraphic_S start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT (where 𝒮isubscript𝒮𝑖\mathcal{S}_{i}caligraphic_S start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT and 𝒫isubscript𝒫𝑖\mathcal{P}_{i}caligraphic_P start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT refer to the sets used in the construction of 𝖢𝖭𝖥𝗉𝗋𝖾𝖿,𝗌𝗎𝖿𝖿subscript𝖢𝖭𝖥𝗉𝗋𝖾𝖿𝗌𝗎𝖿𝖿\mathsf{CNF}_{\mathsf{pref},\mathsf{suff}}sansserif_CNF start_POSTSUBSCRIPT sansserif_pref , sansserif_suff end_POSTSUBSCRIPT). According to Lemmas 3.1 and 3.2, and our rule on drop** coordinates, an index i𝑖iitalic_i is covered by (S[1..p],S[x..y])(S[1..p],S[x..y])( italic_S [ 1 . . italic_p ] , italic_S [ italic_x . . italic_y ] ) if and only if 𝖢𝗈𝗏𝖾𝗋isubscript𝖢𝗈𝗏𝖾𝗋𝑖\mathsf{Cover}_{i}sansserif_Cover start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT occurs. It immediately follows that (S[1..p],S[x..y])(S[1..p],S[x..y])( italic_S [ 1 . . italic_p ] , italic_S [ italic_x . . italic_y ] ) is a 2222-cover if and only if 𝖢𝗈𝗏𝖾𝗋isubscript𝖢𝗈𝗏𝖾𝗋𝑖\mathsf{Cover}_{i}sansserif_Cover start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT occurs for every i[n]𝑖delimited-[]𝑛i\in[n]italic_i ∈ [ italic_n ]. This is exactly the output of the 2𝖢𝖭𝖥2𝖢𝖭𝖥2\mathsf{CNF}2 sansserif_CNF query, as required.

D.2 Border-Substring Oracle

In this section, we describe the oracle checking if (U1,U2)=(S[1..p],S[x..y])(U_{1},U_{2})=(S[1..p],S[x..y])( italic_U start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT , italic_U start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ) = ( italic_S [ 1 . . italic_p ] , italic_S [ italic_x . . italic_y ] ) is a bs-cover.

Construction.

Similar to the prefix-suffix case, we partition borders into groups. A group 𝖻𝗈𝗋(ρ)𝖻𝗈𝗋𝜌\mathsf{bor}(\rho)sansserif_bor ( italic_ρ ) consists of all highly ρ𝜌\rhoitalic_ρ-periodic borders. Each border that is not highly periodic, forms a separate group. By Lemmas 2.1 and 2.2 the number of groups is O(logn)𝑂𝑛O(\log n)italic_O ( roman_log italic_n ).

With every group 𝖻𝗈𝗋𝖻𝗈𝗋\mathsf{bor}sansserif_bor of borders, the algorithm associates the reference position f𝖻𝗈𝗋subscript𝑓𝖻𝗈𝗋f_{\mathsf{bor}}italic_f start_POSTSUBSCRIPT sansserif_bor end_POSTSUBSCRIPT for the groups of substrings that will be considered with this group of borders. If 𝖻𝗈𝗋={U}𝖻𝗈𝗋𝑈\mathsf{bor}=\{U\}sansserif_bor = { italic_U }, the algorithm uses Lemma 2.11 to find all occurrences of U𝑈Uitalic_U in S𝑆Sitalic_S and chooses f𝖻𝗈𝗋subscript𝑓𝖻𝗈𝗋f_{\mathsf{bor}}italic_f start_POSTSUBSCRIPT sansserif_bor end_POSTSUBSCRIPT to be any position not covered by U𝑈Uitalic_U; if there is no such position, i.e., if U𝑈Uitalic_U is a 1-cover, it sets f𝖻𝗈𝗋=subscript𝑓𝖻𝗈𝗋f_{\mathsf{bor}}=\inftyitalic_f start_POSTSUBSCRIPT sansserif_bor end_POSTSUBSCRIPT = ∞. If 𝖻𝗈𝗋=𝖻𝗈𝗋(ρ)𝖻𝗈𝗋𝖻𝗈𝗋𝜌\mathsf{bor}=\mathsf{bor}(\rho)sansserif_bor = sansserif_bor ( italic_ρ ), the algorithm runs a binary search on 𝖻𝗈𝗋𝖻𝗈𝗋\mathsf{bor}sansserif_bor, finding the shortest border U𝖻𝗈𝗋(ρ)𝑈𝖻𝗈𝗋𝜌U\in\mathsf{bor}(\rho)italic_U ∈ sansserif_bor ( italic_ρ ) that is not a 1-cover. This requires O(nlogn)𝑂𝑛𝑛O(n\log n)italic_O ( italic_n roman_log italic_n ) time as it uses Lemma 2.11 O(logn)𝑂𝑛O(\log n)italic_O ( roman_log italic_n ) times. Then the algorithm chooses a position f𝖻𝗈𝗋subscript𝑓𝖻𝗈𝗋f_{\mathsf{bor}}italic_f start_POSTSUBSCRIPT sansserif_bor end_POSTSUBSCRIPT not covered by U𝑈Uitalic_U and additionally stores q𝖻𝗈𝗋=|U|subscript𝑞𝖻𝗈𝗋𝑈q_{\mathsf{bor}}=|U|italic_q start_POSTSUBSCRIPT sansserif_bor end_POSTSUBSCRIPT = | italic_U |. If U𝑈Uitalic_U does not exist, it sets f𝖻𝗈𝗋=subscript𝑓𝖻𝗈𝗋f_{\mathsf{bor}}=\inftyitalic_f start_POSTSUBSCRIPT sansserif_bor end_POSTSUBSCRIPT = ∞.

Let f[n]𝑓delimited-[]𝑛f\in[n]italic_f ∈ [ italic_n ] be an index. We define the sets 𝗌𝗎𝖻𝖺𝗉𝖾𝗋f(k)subscript𝗌𝗎𝖻𝖺𝗉𝖾𝗋𝑓𝑘\mathsf{subaper}_{f}(k)sansserif_subaper start_POSTSUBSCRIPT italic_f end_POSTSUBSCRIPT ( italic_k ) and 𝗌𝗎𝖻𝗉𝖾𝗋f(ρ)subscript𝗌𝗎𝖻𝗉𝖾𝗋𝑓𝜌\mathsf{subper}_{f}(\rho)sansserif_subper start_POSTSUBSCRIPT italic_f end_POSTSUBSCRIPT ( italic_ρ ) in an analogous manner to the sets 𝗉𝖺𝗉𝖾𝗋(k)𝗉𝖺𝗉𝖾𝗋𝑘\mathsf{paper}(k)sansserif_paper ( italic_k ) and 𝗉𝗉𝖾𝗋(ρ)𝗉𝗉𝖾𝗋𝜌\mathsf{pper}(\rho)sansserif_pper ( italic_ρ ). For an integer k𝑘kitalic_k, we define

𝗌𝗎𝖻𝖺𝗉𝖾𝗋f(k)={S[f..f+r]r,0 , S[f..f+r] is aperiodic, and +r1[1.5k..1.5k+1)}\displaystyle\mathsf{subaper}_{f}(k)=\{S[f-\ell..f+r]\mid r,\ell\geq 0\textit{% , }S[f-\ell..f+r]\textit{ is aperiodic, and }\ell+r-1\in[1.5^{k}..1.5^{k+1})\}sansserif_subaper start_POSTSUBSCRIPT italic_f end_POSTSUBSCRIPT ( italic_k ) = { italic_S [ italic_f - roman_ℓ . . italic_f + italic_r ] ∣ italic_r , roman_ℓ ≥ 0 , italic_S [ italic_f - roman_ℓ . . italic_f + italic_r ] is aperiodic, and roman_ℓ + italic_r - 1 ∈ [ 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT ..1.5 start_POSTSUPERSCRIPT italic_k + 1 end_POSTSUPERSCRIPT ) }

For an integer ρ𝜌\rhoitalic_ρ, we define

𝗌𝗎𝖻𝗉𝖾𝗋f(ρ)={S[f..f+r]r,0 and S[f..f+r] is highly ρ-periodic}\displaystyle\mathsf{subper}_{f}(\rho)=\{S[f-\ell..f+r]\mid r,\ell\geq 0% \textit{ and }S[f-\ell..f+r]\textit{ is highly }\rho\textit{-periodic}\}sansserif_subper start_POSTSUBSCRIPT italic_f end_POSTSUBSCRIPT ( italic_ρ ) = { italic_S [ italic_f - roman_ℓ . . italic_f + italic_r ] ∣ italic_r , roman_ℓ ≥ 0 and italic_S [ italic_f - roman_ℓ . . italic_f + italic_r ] is highly italic_ρ -periodic }

Note that every substring of S𝑆Sitalic_S that covers the index f𝑓fitalic_f is either contained in 𝗌𝗎𝖻𝖺𝗉𝖾𝗋f(k)subscript𝗌𝗎𝖻𝖺𝗉𝖾𝗋𝑓𝑘\mathsf{subaper}_{f}(k)sansserif_subaper start_POSTSUBSCRIPT italic_f end_POSTSUBSCRIPT ( italic_k ) for some value of k𝑘kitalic_k, or in 𝗌𝗎𝖻𝗉𝖾𝗋f(ρ)subscript𝗌𝗎𝖻𝗉𝖾𝗋𝑓𝜌\mathsf{subper}_{f}(\rho)sansserif_subper start_POSTSUBSCRIPT italic_f end_POSTSUBSCRIPT ( italic_ρ ) for some value of ρ𝜌\rhoitalic_ρ. The set 𝒮fsubscript𝒮𝑓\mathcal{S}_{f}caligraphic_S start_POSTSUBSCRIPT italic_f end_POSTSUBSCRIPT (analogous to 𝒫𝒫\mathcal{P}caligraphic_P and 𝒮𝒮\mathcal{S}caligraphic_S) is defined as

𝒮f={𝗌𝗎𝖻𝖺𝗉𝖾𝗋f(k)1.5kn}{𝗌𝗎𝖻𝗉𝖾𝗋f(ρ)𝗌𝗎𝖻𝗉𝖾𝗋f(ρ)}subscript𝒮𝑓conditional-setsubscript𝗌𝗎𝖻𝖺𝗉𝖾𝗋𝑓𝑘superscript1.5𝑘𝑛conditional-setsubscript𝗌𝗎𝖻𝗉𝖾𝗋𝑓𝜌subscript𝗌𝗎𝖻𝗉𝖾𝗋𝑓𝜌\displaystyle\mathcal{S}_{f}=\{\mathsf{subaper}_{f}(k)\mid 1.5^{k}\leq n\}\cup% \{\mathsf{subper}_{f}(\rho)\mid\mathsf{subper}_{f}(\rho)\neq\emptyset\}caligraphic_S start_POSTSUBSCRIPT italic_f end_POSTSUBSCRIPT = { sansserif_subaper start_POSTSUBSCRIPT italic_f end_POSTSUBSCRIPT ( italic_k ) ∣ 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT ≤ italic_n } ∪ { sansserif_subper start_POSTSUBSCRIPT italic_f end_POSTSUBSCRIPT ( italic_ρ ) ∣ sansserif_subper start_POSTSUBSCRIPT italic_f end_POSTSUBSCRIPT ( italic_ρ ) ≠ ∅ }

Note that for a period ρ𝜌\rhoitalic_ρ, every substring in 𝗌𝗎𝖻𝗉𝖾𝗋f(ρ)subscript𝗌𝗎𝖻𝗉𝖾𝗋𝑓𝜌\mathsf{subper}_{f}(\rho)sansserif_subper start_POSTSUBSCRIPT italic_f end_POSTSUBSCRIPT ( italic_ρ ) is contained in a highly ρ𝜌\rhoitalic_ρ-periodic run that contains f𝑓fitalic_f. It follows from Lemma 2.5, that |𝒮f|O(logn)subscript𝒮𝑓𝑂𝑛|\mathcal{S}_{f}|\in O(\log n)| caligraphic_S start_POSTSUBSCRIPT italic_f end_POSTSUBSCRIPT | ∈ italic_O ( roman_log italic_n ).

The algorithm iterates over all pairs (𝖻𝗈𝗋,𝗌𝗎𝖻)𝖻𝗈𝗋𝗌𝗎𝖻(\mathsf{bor},\mathsf{sub})( sansserif_bor , sansserif_sub ) where 𝖻𝗈𝗋𝖻𝗈𝗋\mathsf{bor}\in\mathcal{B}sansserif_bor ∈ caligraphic_B and 𝗌𝗎𝖻𝒮f𝖻𝗈𝗋𝗌𝗎𝖻subscript𝒮subscript𝑓𝖻𝗈𝗋\mathsf{sub}\in\mathcal{S}_{f_{\mathsf{bor}}}sansserif_sub ∈ caligraphic_S start_POSTSUBSCRIPT italic_f start_POSTSUBSCRIPT sansserif_bor end_POSTSUBSCRIPT end_POSTSUBSCRIPT (If f𝖻𝗈𝗋=subscript𝑓𝖻𝗈𝗋f_{\mathsf{bor}}=\inftyitalic_f start_POSTSUBSCRIPT sansserif_bor end_POSTSUBSCRIPT = ∞, we treat 𝒮f𝖻𝗈𝗋subscript𝒮subscript𝑓𝖻𝗈𝗋\mathcal{S}_{f_{\mathsf{bor}}}caligraphic_S start_POSTSUBSCRIPT italic_f start_POSTSUBSCRIPT sansserif_bor end_POSTSUBSCRIPT end_POSTSUBSCRIPT as \emptyset). Once again, there will be two fixed coordinates, but now both of them come from the border’s side. As the border is both a prefix (having fixed first coordinate) and a suffix (having fixed second coordinate), it has the first two coordinates fixed.

Consider processing of one pair (𝖻𝗈𝗋,𝗌𝗎𝖻)𝖻𝗈𝗋𝗌𝗎𝖻(\mathsf{bor},\mathsf{sub})( sansserif_bor , sansserif_sub ). It results in creation of the set 𝖯𝖺𝗂𝗋𝗌={(1,𝒮1),(2,𝒮2),,(n,𝒮n)}𝖯𝖺𝗂𝗋𝗌subscript1subscript𝒮1subscript2subscript𝒮2subscript𝑛subscript𝒮𝑛\mathsf{Pairs}=\{(\mathcal{B}_{1},\mathcal{S}_{1}),(\mathcal{B}_{2},\mathcal{S% }_{2}),\ldots,(\mathcal{B}_{n},\mathcal{S}_{n})\}sansserif_Pairs = { ( caligraphic_B start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT , caligraphic_S start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT ) , ( caligraphic_B start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT , caligraphic_S start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ) , … , ( caligraphic_B start_POSTSUBSCRIPT italic_n end_POSTSUBSCRIPT , caligraphic_S start_POSTSUBSCRIPT italic_n end_POSTSUBSCRIPT ) }, which is then used to create a 2𝖢𝖭𝖥2𝖢𝖭𝖥2\mathsf{CNF}2 sansserif_CNF structure of Lemma 4.1. If 𝖻𝗈𝗋={U}𝖻𝗈𝗋𝑈\mathsf{bor}=\{U\}sansserif_bor = { italic_U }, the algorithm applies Lemma 2.11 to find the all indices in S𝑆Sitalic_S that are covered by U𝑈Uitalic_U. For each i[n]𝑖delimited-[]𝑛i\in[n]italic_i ∈ [ italic_n ], we have a “0-dimensional” interval, which is just a boolean value indicating whether this position is covered by U𝑈Uitalic_U or not. However, it is more convenient to represent it as a 1-dimensional interval, setting i={[..]}\mathcal{B}_{i}=\{[-\infty..\infty]\}caligraphic_B start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT = { [ - ∞ . . ∞ ] } if i𝑖iitalic_i is covered and i=subscript𝑖\mathcal{B}_{i}=\varnothingcaligraphic_B start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT = ∅ if it is not. If 𝖻𝗈𝗋=𝖻𝗈𝗋(ρ)𝖻𝗈𝗋𝖻𝗈𝗋𝜌\mathsf{bor}=\mathsf{bor}(\rho)sansserif_bor = sansserif_bor ( italic_ρ ), the algorithm constructs for every i[n]𝑖delimited-[]𝑛i\in[n]italic_i ∈ [ italic_n ] the set of cuboids 𝒞isubscript𝒞𝑖\mathcal{C}_{i}caligraphic_C start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT, drops the first two coordinates that are fixed, and sets isubscript𝑖\mathcal{B}_{i}caligraphic_B start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT to be the set of obtained intervals. (In fact, this set is always a single interval in view of Lemma 2.4.) For the group 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub we use Lemma 3.1 or Lemma 3.2 depending on periodicity, and store the results as the sets 𝒮isubscript𝒮𝑖\mathcal{S}_{i}caligraphic_S start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT. The dimensions of the 2𝖢𝖭𝖥2𝖢𝖭𝖥2\mathsf{CNF}2 sansserif_CNF data structure constructed from 𝖯𝖺𝗂𝗋𝗌𝖯𝖺𝗂𝗋𝗌\mathsf{Pairs}sansserif_Pairs are

d={1if 𝖻𝗈𝗋={U}1if 𝖻𝗈𝗋=𝖻𝗈𝗋(ρ) and dr={2if 𝗌𝗎𝖻 is not highly periodic 3if 𝗌𝗎𝖻 is highly periodic .subscript𝑑cases1if 𝖻𝗈𝗋𝑈1if 𝖻𝗈𝗋𝖻𝗈𝗋𝜌 and subscript𝑑𝑟cases2if 𝗌𝗎𝖻 is not highly periodic 3if 𝗌𝗎𝖻 is highly periodic d_{\ell}=\begin{cases}1&\text{if }\mathsf{bor}=\{U\}\\ 1&\text{if }\mathsf{bor}=\mathsf{bor}(\rho)\end{cases}\text{ and }d_{r}=\begin% {cases}2&\text{if }\mathsf{sub}\text{ is not highly periodic }\\ 3&\text{if }\mathsf{sub}\text{ is highly periodic }\end{cases}.italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT = { start_ROW start_CELL 1 end_CELL start_CELL if sansserif_bor = { italic_U } end_CELL end_ROW start_ROW start_CELL 1 end_CELL start_CELL if sansserif_bor = sansserif_bor ( italic_ρ ) end_CELL end_ROW and italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT = { start_ROW start_CELL 2 end_CELL start_CELL if sansserif_sub is not highly periodic end_CELL end_ROW start_ROW start_CELL 3 end_CELL start_CELL if sansserif_sub is highly periodic end_CELL end_ROW .
Query.

For a query (S[1..b],S[x..y])(S[1..b],S[x..y])( italic_S [ 1 . . italic_b ] , italic_S [ italic_x . . italic_y ] ), the algorithm verifies that U=S[1..b]U=S[1..b]italic_U = italic_S [ 1 . . italic_b ] is a border by checking if 𝖫𝖢𝖯SR(b,n)=bsuperscriptsubscript𝖫𝖢𝖯𝑆𝑅𝑏𝑛𝑏\mathsf{LCP}_{S}^{R}(b,n)=bsansserif_LCP start_POSTSUBSCRIPT italic_S end_POSTSUBSCRIPT start_POSTSUPERSCRIPT italic_R end_POSTSUPERSCRIPT ( italic_b , italic_n ) = italic_b. If this is not the case, the oracle answers False. Otherwise, the algorithm checks if U𝑈Uitalic_U is highly periodic by querying 𝖨𝖯𝖬Ssubscript𝖨𝖯𝖬𝑆\mathsf{IPM}_{S}sansserif_IPM start_POSTSUBSCRIPT italic_S end_POSTSUBSCRIPT. If it is not, the algorithm sets 𝖻𝗈𝗋={U}𝖻𝗈𝗋𝑈\mathsf{bor}=\{U\}sansserif_bor = { italic_U } and checks if f𝖻𝗈𝗋=subscript𝑓𝖻𝗈𝗋f_{\mathsf{bor}}=\inftyitalic_f start_POSTSUBSCRIPT sansserif_bor end_POSTSUBSCRIPT = ∞. If f𝖻𝗈𝗋=subscript𝑓𝖻𝗈𝗋f_{\mathsf{bor}}=\inftyitalic_f start_POSTSUBSCRIPT sansserif_bor end_POSTSUBSCRIPT = ∞, the algorithm returns True. Otherwise, it creates a 1111-dimensional point p=(0)subscript𝑝0p_{\ell}=(0)italic_p start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT = ( 0 ).

If U𝑈Uitalic_U is highly ρ𝜌\rhoitalic_ρ-periodic, the algorithm defines 𝖻𝗈𝗋=𝖻𝗈𝗋(ρ)𝖻𝗈𝗋𝖻𝗈𝗋𝜌\mathsf{bor}=\mathsf{bor}(\rho)sansserif_bor = sansserif_bor ( italic_ρ ), and checks if f𝖻𝗈𝗋=subscript𝑓𝖻𝗈𝗋f_{\mathsf{bor}}=\inftyitalic_f start_POSTSUBSCRIPT sansserif_bor end_POSTSUBSCRIPT = ∞ or q𝖻𝗈𝗋>|U|subscript𝑞𝖻𝗈𝗋𝑈q_{\mathsf{bor}}>|U|italic_q start_POSTSUBSCRIPT sansserif_bor end_POSTSUBSCRIPT > | italic_U |. If this is the case, the algorithm returns True. Otherwise, it sets |U|=qρ+d𝑈𝑞𝜌𝑑|U|=q\rho+d| italic_U | = italic_q italic_ρ + italic_d with the unique integers and d[0..ρ1]d\in[0..\rho-1]italic_d ∈ [ 0 . . italic_ρ - 1 ] and q𝑞qitalic_q. The algorithm then creates the point (0,d,q)0𝑑𝑞(0,d,q)( 0 , italic_d , italic_q ) and trims it to p=(q)subscript𝑝𝑞p_{\ell}=(q)italic_p start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT = ( italic_q ) as the first two coordinates are fixed.

Now the algorithm works with f=f𝖻𝗈𝗋𝑓subscript𝑓𝖻𝗈𝗋f=f_{\mathsf{bor}}italic_f = italic_f start_POSTSUBSCRIPT sansserif_bor end_POSTSUBSCRIPT (which is finite). It queries 𝖨𝖯𝖬Ssubscript𝖨𝖯𝖬𝑆\mathsf{IPM}_{S}sansserif_IPM start_POSTSUBSCRIPT italic_S end_POSTSUBSCRIPT to check if there is an occurrence of V=S[x..y]V=S[x..y]italic_V = italic_S [ italic_x . . italic_y ] that covers f𝑓fitalic_f. Technically, the algorithm queries 𝖨𝖯𝖬S(S[f|V|+1..f+|V|1],V)\mathsf{IPM}_{S}(S[f-|V|+1..f+|V|-1],V)sansserif_IPM start_POSTSUBSCRIPT italic_S end_POSTSUBSCRIPT ( italic_S [ italic_f - | italic_V | + 1 . . italic_f + | italic_V | - 1 ] , italic_V ) and checks if the output is empty. If there is no such occurrence, the algorithm returns False. Otherwise, the algorithm picks an arbitrary occurrence iVsubscript𝑖𝑉i_{V}italic_i start_POSTSUBSCRIPT italic_V end_POSTSUBSCRIPT of V𝑉Vitalic_V covering f𝑓fitalic_f and computes the integers \ellroman_ℓ and r𝑟ritalic_r such that V=S[f..f+r]V=S[f-\ell..f+r]italic_V = italic_S [ italic_f - roman_ℓ . . italic_f + italic_r ].

Next, the algorithm checks if V𝑉Vitalic_V is highly periodic by querying 𝖨𝖯𝖬Ssubscript𝖨𝖯𝖬𝑆\mathsf{IPM}_{S}sansserif_IPM start_POSTSUBSCRIPT italic_S end_POSTSUBSCRIPT. Getting the result, it chooses the appropriate group 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub for V𝑉Vitalic_V and creates a point for V𝑉Vitalic_V. If V𝑉Vitalic_V is not highly periodic, the point is pr=(,r)subscript𝑝𝑟𝑟p_{r}=(\ell,r)italic_p start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT = ( roman_ℓ , italic_r ). If V𝑉Vitalic_V is highly ρ𝜌\rhoitalic_ρ-periodic then the algorithm defines 𝗋𝗈𝗈𝗍𝗋𝗈𝗈𝗍\mathsf{root}sansserif_root and computes the point pr=(d,dr,q,r)subscript𝑝𝑟subscript𝑑subscript𝑑𝑟subscript𝑞𝑟p_{r}=(d_{\ell},d_{r},q_{\ell,r})italic_p start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT = ( italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT , italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT , italic_q start_POSTSUBSCRIPT roman_ℓ , italic_r end_POSTSUBSCRIPT ) for V𝑉Vitalic_V such that V=𝗋𝗈𝗈𝗍[ρd+1..ρ]𝗋𝗈𝗈𝗍q,r𝗋𝗈𝗈𝗍[1..dr]V=\mathsf{root}[\rho-d_{\ell}+1..\rho]\cdot\mathsf{root}^{q_{\ell,r}}\cdot% \mathsf{root}[1..d_{r}]italic_V = sansserif_root [ italic_ρ - italic_d start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT + 1 . . italic_ρ ] ⋅ sansserif_root start_POSTSUPERSCRIPT italic_q start_POSTSUBSCRIPT roman_ℓ , italic_r end_POSTSUBSCRIPT end_POSTSUPERSCRIPT ⋅ sansserif_root [ 1 . . italic_d start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ]. Finally, the algorithm queries the 2𝖢𝖭𝖥(𝖻𝗈𝗋,𝗌𝗎𝖻)2subscript𝖢𝖭𝖥𝖻𝗈𝗋𝗌𝗎𝖻2\mathsf{CNF}_{(\mathsf{bor},\mathsf{sub})}2 sansserif_CNF start_POSTSUBSCRIPT ( sansserif_bor , sansserif_sub ) end_POSTSUBSCRIPT data structure with the point (p,pr)subscript𝑝subscript𝑝𝑟(p_{\ell},p_{r})( italic_p start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT , italic_p start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ) and returns the answer obtained for this query.

Complexity.

As in the prefix-suffix case, the construction time is dominated by the amount needed to build 2𝖢𝖭𝖥2𝖢𝖭𝖥2\mathsf{CNF}2 sansserif_CNF structure (all the rest is within O(nlog2n)𝑂𝑛superscript2𝑛O(n\log^{2}n)italic_O ( italic_n roman_log start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT italic_n ) bound). As this structure is at most 4-dimensional, we get the O(nlog3n)𝑂𝑛superscript3𝑛O(n\log^{3}n)italic_O ( italic_n roman_log start_POSTSUPERSCRIPT 3 end_POSTSUPERSCRIPT italic_n ) bound from Lemma 4.1. Since the number of such structures the algorithm builds for different pairs of groups is O(log2n)𝑂superscript2𝑛O(\log^{2}n)italic_O ( roman_log start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT italic_n ), the total construction time is O(nlog5n)𝑂𝑛superscript5𝑛O(n\log^{5}n)italic_O ( italic_n roman_log start_POSTSUPERSCRIPT 5 end_POSTSUPERSCRIPT italic_n ).

A query consists of a constant number of 𝖫𝖢𝖯𝖫𝖢𝖯\mathsf{LCP}sansserif_LCP queries, 𝖨𝖯𝖬𝖨𝖯𝖬\mathsf{IPM}sansserif_IPM queries, and arithmetic operations. The algorithm also applies a single query to a 2𝖢𝖭𝖥2𝖢𝖭𝖥2\mathsf{CNF}2 sansserif_CNF data structure, which takes O(log3n)𝑂superscript3𝑛O(\log^{3}n)italic_O ( roman_log start_POSTSUPERSCRIPT 3 end_POSTSUPERSCRIPT italic_n ) time due to Lemma 4.1.

Correctness.

We claim that the query returns the correct answer to the question “Is (S[1..b],S[x..y])(S[1..b],S[x..y])( italic_S [ 1 . . italic_b ] , italic_S [ italic_x . . italic_y ] ) a border-substring cover?”. If the algorithm returns False due to S[1..b]S[1..b]italic_S [ 1 . . italic_b ] not being a border, this is obviously correct. Assume that U=S[1..b]U=S[1..b]italic_U = italic_S [ 1 . . italic_b ] is a border and let 𝖻𝗈𝗋𝖻𝗈𝗋\mathsf{bor}sansserif_bor be its group. If the query returns True due to f𝖻𝗈𝗋=subscript𝑓𝖻𝗈𝗋f_{\mathsf{bor}}=\inftyitalic_f start_POSTSUBSCRIPT sansserif_bor end_POSTSUBSCRIPT = ∞ or Let |U|<q𝖻𝗈𝗋𝑈subscript𝑞𝖻𝗈𝗋|U|<q_{\mathsf{bor}}| italic_U | < italic_q start_POSTSUBSCRIPT sansserif_bor end_POSTSUBSCRIPT, then U𝑈Uitalic_U is a 1-cover, and then forms a 2-cover with any substring.

In the remaining case the query returns the same answer as the 2𝖢𝖭𝖥2𝖢𝖭𝖥2\mathsf{CNF}2 sansserif_CNF data structure built for the groups of U𝑈Uitalic_U and S[x..y]S[x..y]italic_S [ italic_x . . italic_y ]. Here the analysis is similar to the prefix-suffix case, so we omit it.

Appendix E Free Points Data Structure

In this section, we prove Lemma 5.2. In the proof of Lemma 5.2 we use the concept of persistent data structure [12]. A data structure is persistent if it supports multiple versions of itself and allows quick access to any version for querying, deletion, or update (an update creates a new version).

We first recall the definition of a free point and the statement of Lemma 5.2. See 5.1

See 5.2

Proof E.1.

The main point of the algorithm is an efficient reduction of the 2-dimensional problem to its 1-dimensional analog. The algorithm uses an auxiliary tree 𝒯𝒯\mathcal{T}caligraphic_T and the main structure 𝒟𝒟\mathcal{D}caligraphic_D, which is a variant of persistent lazy segment tree [27]. The details are described below.

We take a fully balanced binary tree with 2lognsuperscript2𝑛2^{\left\lceil{\log n}\right\rceil}2 start_POSTSUPERSCRIPT ⌈ roman_log italic_n ⌉ end_POSTSUPERSCRIPT leaves and delete 2lognnsuperscript2𝑛𝑛2^{\left\lceil{\log n}\right\rceil}-n2 start_POSTSUPERSCRIPT ⌈ roman_log italic_n ⌉ end_POSTSUPERSCRIPT - italic_n rightmost leaves together with all internal nodes having no leaves remained in their subtrees. The remaining leaves are enumerated from 1111 to n𝑛nitalic_n in a natural order. Thus, the leaves in a subtree of every node form a sub-range of [n]delimited-[]𝑛[n][ italic_n ]; we use these ranges as names of nodes. The only data stored in a node is the links .𝑙𝑒𝑓𝑡.\mathit{left}. italic_left and .𝑟𝑖𝑔ℎ𝑡.\mathit{right}. italic_right to its children. We refer to the obtained tree 𝒯0subscript𝒯0\mathcal{T}_{0}caligraphic_T start_POSTSUBSCRIPT 0 end_POSTSUBSCRIPT as the blank tree. It is used to construct both 𝒯𝒯\mathcal{T}caligraphic_T and 𝒟𝒟\mathcal{D}caligraphic_D.

Building 𝒯𝒯\mathcal{T}caligraphic_T from 𝒯0subscript𝒯0\mathcal{T}_{0}caligraphic_T start_POSTSUBSCRIPT 0 end_POSTSUBSCRIPT, we interpret each leaf as the y𝑦yitalic_y-coordinate of a point in [n]2superscriptdelimited-[]𝑛2[n]^{2}[ italic_n ] start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT; the leaf k𝑘kitalic_k corresponds to all points (x,y)[n]2𝑥𝑦superscriptdelimited-[]𝑛2(x,y)\in[n]^{2}( italic_x , italic_y ) ∈ [ italic_n ] start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT with y=k𝑦𝑘y=kitalic_y = italic_k. The algorithm processes each rectangle R𝑅R\in\mathcal{R}italic_R ∈ caligraphic_R, adding the information about its x𝑥xitalic_x-range to the nodes of 𝒯0subscript𝒯0\mathcal{T}_{0}caligraphic_T start_POSTSUBSCRIPT 0 end_POSTSUBSCRIPT. Precisely, a rectangle [x1..x2]×[y1..y2][x_{1}..x_{2}]\times[y_{1}..y_{2}][ italic_x start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT . . italic_x start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ] × [ italic_y start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT . . italic_y start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ] is fed to the following recursive procedure, starting at the root [n]delimited-[]𝑛[n][ italic_n ] of 𝒯0subscript𝒯0\mathcal{T}_{0}caligraphic_T start_POSTSUBSCRIPT 0 end_POSTSUBSCRIPT:

  • for the current node [y..y′′][y^{\prime}..y^{\prime\prime}][ italic_y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT . . italic_y start_POSTSUPERSCRIPT ′ ′ end_POSTSUPERSCRIPT ]:

    • if [y..y′′][y1..y2]=[y^{\prime}..y^{\prime\prime}]\cap[y_{1}..y_{2}]=\varnothing[ italic_y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT . . italic_y start_POSTSUPERSCRIPT ′ ′ end_POSTSUPERSCRIPT ] ∩ [ italic_y start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT . . italic_y start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ] = ∅, stop;

    • if [y..y′′][y1..y2][y^{\prime}..y^{\prime\prime}]\subseteq[y_{1}..y_{2}][ italic_y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT . . italic_y start_POSTSUPERSCRIPT ′ ′ end_POSTSUPERSCRIPT ] ⊆ [ italic_y start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT . . italic_y start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ], add the range [x1..x2][x_{1}..x_{2}][ italic_x start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT . . italic_x start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ] to [y..y′′][y^{\prime}..y^{\prime\prime}][ italic_y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT . . italic_y start_POSTSUPERSCRIPT ′ ′ end_POSTSUPERSCRIPT ] and stop;

    • otherwise, call the procedure for both children of [y..y′′][y^{\prime}..y^{\prime\prime}][ italic_y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT . . italic_y start_POSTSUPERSCRIPT ′ ′ end_POSTSUPERSCRIPT ].

A well-known observation says that the procedure is called for at most 4444 nodes at each level of 𝒯0subscript𝒯0\mathcal{T}_{0}caligraphic_T start_POSTSUBSCRIPT 0 end_POSTSUBSCRIPT. Therefore, each rectangle is processed in O(logn)𝑂𝑛O(\log n)italic_O ( roman_log italic_n ) time and adds O(logn)𝑂𝑛O(\log n)italic_O ( roman_log italic_n ) to the space used by 𝒯0subscript𝒯0\mathcal{T}_{0}caligraphic_T start_POSTSUBSCRIPT 0 end_POSTSUBSCRIPT. After processing all rectangles from \mathcal{R}caligraphic_R in O(nlogn)𝑂𝑛𝑛O(n\log n)italic_O ( italic_n roman_log italic_n ) time, the resulting tree is 𝒯𝒯\mathcal{T}caligraphic_T. This tree stores stores O(nlogn)𝑂𝑛𝑛O(n\log n)italic_O ( italic_n roman_log italic_n ) information.

Claim 9.

A point (x,y)𝑥𝑦(x,y)( italic_x , italic_y ) is \mathcal{R}caligraphic_R-free if and only if x𝑥xitalic_x does not belong to any x𝑥xitalic_x-range stored in a node on the path from the root of 𝒯𝒯\mathcal{T}caligraphic_T to the leaf y𝑦yitalic_y.

{claimproof}

Note that the algorithm partitions each rectangle R𝑅R\in\mathcal{R}italic_R ∈ caligraphic_R into a set of O(logn)𝑂𝑛O(\log n)italic_O ( roman_log italic_n ) disjoint “new” rectangles with the same x𝑥xitalic_x-range such that the y𝑦yitalic_y-ranges of these rectangles correspond to some nodes of 𝒯𝒯\mathcal{T}caligraphic_T. Then a point is \mathcal{R}caligraphic_R-free if and only if it is not contained in any of the new rectangles. Each node [y1..y2][y_{1}..y_{2}][ italic_y start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT . . italic_y start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ] stores all new rectangles of the form [x1..x2]×[y1..y2][x_{1}..x_{2}]\times[y_{1}..y_{2}][ italic_x start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT . . italic_x start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ] × [ italic_y start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT . . italic_y start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ]. Hence, the nodes on the path from the root to y𝑦yitalic_y store all new rectangles containing y𝑦yitalic_y in their y𝑦yitalic_y-range. Then (x,y)𝑥𝑦(x,y)( italic_x , italic_y ) is \mathcal{R}caligraphic_R-free if and only if x𝑥xitalic_x is out of all x𝑥xitalic_x-ranges stored in these nodes. By Claim 9, to report all free points it suffices to find, for each y[n]𝑦delimited-[]𝑛y\in[n]italic_y ∈ [ italic_n ], the complement of the union of all x𝑥xitalic_x-ranges stored in the nodes on the path from the root of 𝒯𝒯\mathcal{T}caligraphic_T to the leaf y𝑦yitalic_y. First we describe how to compute the complement of the union of a fixed set 𝒳𝒳\mathcal{X}caligraphic_X of x𝑥xitalic_x-ranges.

Solution for the 1111-dimensional case.

The algorithm starts with 𝒯0subscript𝒯0\mathcal{T}_{0}caligraphic_T start_POSTSUBSCRIPT 0 end_POSTSUBSCRIPT, associates each leaf with the x𝑥xitalic_x-coordinate of a point, and processes ranges from 𝒳𝒳\mathcal{X}caligraphic_X one by one. Each node [x1..x2][x_{1}..x_{2}][ italic_x start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT . . italic_x start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ] stores a single value [x1..x2].f[x_{1}..x_{2}].f[ italic_x start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT . . italic_x start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ] . italic_f, which is the number of points from [x1..x2][x_{1}..x_{2}][ italic_x start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT . . italic_x start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ] that are not contained in already processed ranges. When all ranges are processed, points in X¯=[n]X𝒳X¯𝑋delimited-[]𝑛subscript𝑋𝒳𝑋\overline{X}=[n]\setminus\bigcup_{X\in\mathcal{X}}Xover¯ start_ARG italic_X end_ARG = [ italic_n ] ∖ ⋃ start_POSTSUBSCRIPT italic_X ∈ caligraphic_X end_POSTSUBSCRIPT italic_X are reported. We assume that the algorithm reports all points, and consider the other two options stated in the lemma in the end of the proof. Reporting is made during a partial depth-first traversal of the obtained tree 𝒯𝒳subscript𝒯𝒳\mathcal{T}_{\mathcal{X}}caligraphic_T start_POSTSUBSCRIPT caligraphic_X end_POSTSUBSCRIPT. This traversal skips subtrees rooted at zero-valued nodes and thus visits only O(|X¯|logn)𝑂¯𝑋𝑛O(|\overline{X}|\log n)italic_O ( | over¯ start_ARG italic_X end_ARG | roman_log italic_n ) nodes, including |X¯|¯𝑋|\overline{X}|| over¯ start_ARG italic_X end_ARG | leaves. Hence the reporting costs O(|X¯|logn)=O(𝗈𝗎𝗍𝗉𝗎𝗍logn)𝑂¯𝑋𝑛𝑂𝗈𝗎𝗍𝗉𝗎𝗍𝑛O(|\overline{X}|\log n)=O(\mathsf{output}\cdot\log n)italic_O ( | over¯ start_ARG italic_X end_ARG | roman_log italic_n ) = italic_O ( sansserif_output ⋅ roman_log italic_n ) time. Let us describe the processing phase.

The value of a node is initialized as the number of points in its range. The call 𝖠𝖽𝖽([n],X)𝖠𝖽𝖽delimited-[]𝑛𝑋\mathsf{Add}([n],X)sansserif_Add ( [ italic_n ] , italic_X ) is then performed for each X𝒳𝑋𝒳X\in\mathcal{X}italic_X ∈ caligraphic_X, where the recursive function 𝖠𝖽𝖽𝖠𝖽𝖽\mathsf{Add}sansserif_Add is defined as follows.

  • 𝖠𝖽𝖽(𝗇𝗈𝖽𝖾[x1..x2],𝗋𝖺𝗇𝗀𝖾X):\mathsf{Add}(\mathsf{node}\ [x_{1}..x_{2}],\mathsf{range}\ X):sansserif_Add ( sansserif_node [ italic_x start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT . . italic_x start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ] , sansserif_range italic_X ) :

    • If [x1..x2]X[x_{1}..x_{2}]\subseteq X[ italic_x start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT . . italic_x start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ] ⊆ italic_X, set [x1..x2].f=0[x_{1}..x_{2}].f=0[ italic_x start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT . . italic_x start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ] . italic_f = 0

    • If [x1..x2]X[x_{1}..x_{2}]\cap X\neq\varnothing[ italic_x start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT . . italic_x start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ] ∩ italic_X ≠ ∅ and [x1..x2].f>0[x_{1}..x_{2}].f>0[ italic_x start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT . . italic_x start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ] . italic_f > 0, set [x1..x2].f=𝖠𝖽𝖽([x1..x2].𝑙𝑒𝑓𝑡)+𝖠𝖽𝖽([x1..x2].𝑟𝑖𝑔ℎ𝑡)[x_{1}..x_{2}].f=\mathsf{Add}([x_{1}..x_{2}].\mathit{left})+\mathsf{Add}([x_{1% }..x_{2}].\mathit{right})[ italic_x start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT . . italic_x start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ] . italic_f = sansserif_Add ( [ italic_x start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT . . italic_x start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ] . italic_left ) + sansserif_Add ( [ italic_x start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT . . italic_x start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ] . italic_right )

    • Return [x1..x2].f[x_{1}..x_{2}].f[ italic_x start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT . . italic_x start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ] . italic_f

If a value [x1..x2].f[x_{1}..x_{2}].f[ italic_x start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT . . italic_x start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ] . italic_f equals |[n](X1Xk)|delimited-[]𝑛subscript𝑋1subscript𝑋𝑘|[n]\setminus(X_{1}\cup\cdots\cup X_{k})|| [ italic_n ] ∖ ( italic_X start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT ∪ ⋯ ∪ italic_X start_POSTSUBSCRIPT italic_k end_POSTSUBSCRIPT ) | after processing the ranges X1,,Xksubscript𝑋1subscript𝑋𝑘X_{1},\ldots,X_{k}italic_X start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT , … , italic_X start_POSTSUBSCRIPT italic_k end_POSTSUBSCRIPT, we call it correct. The following claim proves correctness of values in all nodes traversed during the reporting phase.

Claim 10.

After any sequence of calls 𝖠𝖽𝖽([n],X1),,𝖠𝖽𝖽([n],Xk)𝖠𝖽𝖽delimited-[]𝑛subscript𝑋1𝖠𝖽𝖽delimited-[]𝑛subscript𝑋𝑘\mathsf{Add}([n],X_{1}),\ldots,\mathsf{Add}([n],X_{k})sansserif_Add ( [ italic_n ] , italic_X start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT ) , … , sansserif_Add ( [ italic_n ] , italic_X start_POSTSUBSCRIPT italic_k end_POSTSUBSCRIPT ), every node [x1..x2]𝒯𝒳[x_{1}..x_{2}]\in\mathcal{T}_{\mathcal{X}}[ italic_x start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT . . italic_x start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ] ∈ caligraphic_T start_POSTSUBSCRIPT caligraphic_X end_POSTSUBSCRIPT has either a correct value or a zero-valued proper ancestor.

{claimproof}

The proof is by induction on k𝑘kitalic_k. The initialization of values proves the base case. For the step case, assume that the claim is true after processing Xisubscript𝑋𝑖X_{i}italic_X start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT and consider the call 𝖠𝖽𝖽([n],Xi+1)𝖠𝖽𝖽delimited-[]𝑛subscript𝑋𝑖1\mathsf{Add}([n],X_{i+1})sansserif_Add ( [ italic_n ] , italic_X start_POSTSUBSCRIPT italic_i + 1 end_POSTSUBSCRIPT ). Since zero values never change and the recursion does not reach below a zero-valued node, it is sufficient to consider an arbitrary node [x1..x2][x_{1}..x_{2}][ italic_x start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT . . italic_x start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ] with nonzero values in it and in all its ancestors. If [x1..x2]Xi+1[x_{1}..x_{2}]\subseteq X_{i+1}[ italic_x start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT . . italic_x start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ] ⊆ italic_X start_POSTSUBSCRIPT italic_i + 1 end_POSTSUBSCRIPT, then the value of either this node or one of its ancestors is correctly set to 00 during the call. If [x1..x2]Xi+1=[x_{1}..x_{2}]\cap X_{i+1}=\varnothing[ italic_x start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT . . italic_x start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ] ∩ italic_X start_POSTSUBSCRIPT italic_i + 1 end_POSTSUBSCRIPT = ∅, the value [x1..x2].f[x_{1}..x_{2}].f[ italic_x start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT . . italic_x start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ] . italic_f remains unchanged which is correct by the inductive hypothesis. In both cases, the step case holds. In the remaining case, the value [x1..x2].f[x_{1}..x_{2}].f[ italic_x start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT . . italic_x start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ] . italic_f was set to the sum of the values of its children. If an incorrect value was assigned, then earlier during the call another incorrect value was assigned to a child of [x1..x2][x_{1}..x_{2}][ italic_x start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT . . italic_x start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ]. But if an incorrect assignment is necessarily preceded by another incorrect assignment, then no incorrect assignment can happen. Hence, all values set as the sums of their children’s values are assigned correctly. The step case is proved. By Claim 10, in the reporting phase the algorithm meets only correct values and thus correctly reports the set ¯X¯absent𝑋\overline{}Xover¯ start_ARG end_ARG italic_X. During the call 𝖠𝖽𝖽([n],X)𝖠𝖽𝖽delimited-[]𝑛𝑋\mathsf{Add}([n],X)sansserif_Add ( [ italic_n ] , italic_X ), at most 4444 nodes at each level are touched. Thus, each range X𝒳𝑋𝒳X\in\mathcal{X}italic_X ∈ caligraphic_X is processed in O(logn)𝑂𝑛O(\log n)italic_O ( roman_log italic_n ) time, and the whole processing phase requires O(|𝒳|logn)𝑂𝒳𝑛O(|\mathcal{X}|\log n)italic_O ( | caligraphic_X | roman_log italic_n ) time.

Processing of all x𝑥xitalic_x-ranges stored in 𝒯𝒯\mathcal{T}caligraphic_T.

For a node t𝒯𝑡𝒯t\in\mathcal{T}italic_t ∈ caligraphic_T, let 𝒳tsubscript𝒳𝑡\mathcal{X}_{t}caligraphic_X start_POSTSUBSCRIPT italic_t end_POSTSUBSCRIPT be the set of x𝑥xitalic_x-ranges stored in all nodes on the path from the root to t𝑡titalic_t. The algorithm traverses 𝒯𝒯\mathcal{T}caligraphic_T depth first, creating the tree 𝒯𝒳tsubscript𝒯subscript𝒳𝑡\mathcal{T}_{\mathcal{X}_{t}}caligraphic_T start_POSTSUBSCRIPT caligraphic_X start_POSTSUBSCRIPT italic_t end_POSTSUBSCRIPT end_POSTSUBSCRIPT on the first visit to t𝑡titalic_t and deleting it on the last visit. When t=y𝑡𝑦t=yitalic_t = italic_y is a leaf, the reporting phase is run for 𝒯𝒳ysubscript𝒯subscript𝒳𝑦\mathcal{T}_{\mathcal{X}_{y}}caligraphic_T start_POSTSUBSCRIPT caligraphic_X start_POSTSUBSCRIPT italic_y end_POSTSUBSCRIPT end_POSTSUBSCRIPT, during which \mathcal{R}caligraphic_R-free points with the second coordinate y𝑦yitalic_y are reported. In order to save space and construction time, the trees 𝒯𝒳tsubscript𝒯subscript𝒳𝑡\mathcal{T}_{\mathcal{X}_{t}}caligraphic_T start_POSTSUBSCRIPT caligraphic_X start_POSTSUBSCRIPT italic_t end_POSTSUBSCRIPT end_POSTSUBSCRIPT are built and stored as versions of a persistent data structure 𝒟𝒟\mathcal{D}caligraphic_D.

Let us briefly explain how it works. We first build the tree 𝒯𝒳[n]subscript𝒯subscript𝒳delimited-[]𝑛\mathcal{T}_{\mathcal{X}_{[n]}}caligraphic_T start_POSTSUBSCRIPT caligraphic_X start_POSTSUBSCRIPT [ italic_n ] end_POSTSUBSCRIPT end_POSTSUBSCRIPT, processing all ranges stored in the root of 𝒯𝒯\mathcal{T}caligraphic_T. To every node of this tree we assign its version index, equal to 0; version 0 is now the current version. Then we start a depth-first traversal of 𝒯𝒯\mathcal{T}caligraphic_T, adding and deleting versions as follows.

The index of the current version is always the depth of the currently traversed node of 𝒯𝒯\mathcal{T}caligraphic_T. To copy a node v𝑣vitalic_v means to create a new node vsuperscript𝑣v^{\prime}italic_v start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT with v.𝑙𝑒𝑓𝑡=v.𝑙𝑒𝑓𝑡formulae-sequencesuperscript𝑣𝑙𝑒𝑓𝑡𝑣𝑙𝑒𝑓𝑡v^{\prime}.\mathit{left}=v.\mathit{left}italic_v start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT . italic_left = italic_v . italic_left, v.𝑟𝑖𝑔ℎ𝑡=v.𝑟𝑖𝑔ℎ𝑡formulae-sequencesuperscript𝑣𝑟𝑖𝑔ℎ𝑡𝑣𝑟𝑖𝑔ℎ𝑡v^{\prime}.\mathit{right}=v.\mathit{right}italic_v start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT . italic_right = italic_v . italic_right, v.f=v.fformulae-sequencesuperscript𝑣𝑓𝑣𝑓v^{\prime}.f=v.fitalic_v start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT . italic_f = italic_v . italic_f, and v.𝑣𝑒𝑟𝑠𝑖𝑜𝑛formulae-sequencesuperscript𝑣𝑣𝑒𝑟𝑠𝑖𝑜𝑛v^{\prime}.\mathit{version}italic_v start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT . italic_version being the current version. Reaching node tsuperscript𝑡t^{\prime}italic_t start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT from its parent t𝑡titalic_t, we create a new version as follows. We copy the root v𝑣vitalic_v of the version 𝒯𝒳tsubscript𝒯subscript𝒳𝑡\mathcal{T}_{\mathcal{X}_{t}}caligraphic_T start_POSTSUBSCRIPT caligraphic_X start_POSTSUBSCRIPT italic_t end_POSTSUBSCRIPT end_POSTSUBSCRIPT into vsuperscript𝑣v^{\prime}italic_v start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT. Now we can navigate 𝒯𝒳tsubscript𝒯subscript𝒳𝑡\mathcal{T}_{\mathcal{X}_{t}}caligraphic_T start_POSTSUBSCRIPT caligraphic_X start_POSTSUBSCRIPT italic_t end_POSTSUBSCRIPT end_POSTSUBSCRIPT using the root vsuperscript𝑣v^{\prime}italic_v start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT instead of v𝑣vitalic_v. Starting from vsuperscript𝑣v^{\prime}italic_v start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT, we process all ranges stored in tsuperscript𝑡t^{\prime}italic_t start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT with with a simple modification of the function 𝖠𝖽𝖽(,)𝖠𝖽𝖽\mathsf{Add}(\cdot,\cdot)sansserif_Add ( ⋅ , ⋅ ). This modification, when calling to a child x𝑥xitalic_x of the currently processed node u𝑢uitalic_u, checks if x.𝑣𝑒𝑟𝑠𝑖𝑜𝑛formulae-sequence𝑥𝑣𝑒𝑟𝑠𝑖𝑜𝑛x.\mathit{version}italic_x . italic_version is current; if not, it copies x𝑥xitalic_x to xsuperscript𝑥x^{\prime}italic_x start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT, updates the link of u𝑢uitalic_u from x𝑥xitalic_x to xsuperscript𝑥x^{\prime}italic_x start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT, and then runs the recursive call for xsuperscript𝑥x^{\prime}italic_x start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT. When all ranges stored in tsuperscript𝑡t^{\prime}italic_t start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT are processed, we have the tree 𝒯𝒳tsubscript𝒯subscript𝒳superscript𝑡\mathcal{T}_{\mathcal{X}_{t^{\prime}}}caligraphic_T start_POSTSUBSCRIPT caligraphic_X start_POSTSUBSCRIPT italic_t start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT end_POSTSUBSCRIPT end_POSTSUBSCRIPT with the root vsuperscript𝑣v^{\prime}italic_v start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT.

When visiting t𝑡titalic_t for the last time, the algorithm deletes all nodes of the current version, starting from the root. The time spent for all deletions in the same as for all creations of new copies, so we can ignore it. Note that at every moment all existing versions correspond to the nodes on the path from the root to the current node of 𝒯𝒯\mathcal{T}caligraphic_T. In particular, all version indices are different. In order to return to the previous version after deleting the current one, it suffices to store all roots in an array of version indices.

Every call to the modified function 𝖠𝖽𝖽([n],)𝖠𝖽𝖽delimited-[]𝑛\mathsf{Add}([n],\cdot)sansserif_Add ( [ italic_n ] , ⋅ ) still takes O(logn)𝑂𝑛O(\log n)italic_O ( roman_log italic_n ) time as in the original implementation, since what we added is just O(1)𝑂1O(1)italic_O ( 1 ) copying operations per level of the tree (recall that at most 4 nodes per level of the tree are touched during this call). As 𝒯𝒯\mathcal{T}caligraphic_T contains O(nlogn)𝑂𝑛𝑛O(n\log n)italic_O ( italic_n roman_log italic_n ) ranges, and each range is added to 𝒟𝒟\mathcal{D}caligraphic_D only once, the processing time is O(nlog2n)𝑂𝑛superscript2𝑛O(n\log^{2}n)italic_O ( italic_n roman_log start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT italic_n ). As every two reporting phases output disjoint sets of points, the reporting costs O(n+𝗈𝗎𝗍𝗉𝗎𝗍logn)𝑂𝑛𝗈𝗎𝗍𝗉𝗎𝗍𝑛O(n+\mathsf{output}\cdot\log n)italic_O ( italic_n + sansserif_output ⋅ roman_log italic_n ) time. This gives the time bounds stated in the lemma.

It remains to consider the other two modes of reporting: report the minimal x𝑥xitalic_x for each y𝑦yitalic_y and report all pairs with (x+y)m𝑥𝑦𝑚(x+y)\leq m( italic_x + italic_y ) ≤ italic_m. As we traverse the leaves of any tree 𝒯𝒳subscript𝒯𝒳\mathcal{T}_{\mathcal{X}}caligraphic_T start_POSTSUBSCRIPT caligraphic_X end_POSTSUBSCRIPT in increasing order, we just stop the depth-first traversal of the reporting phase at the moment when all required pairs (x,y)𝑥𝑦(x,y)( italic_x , italic_y ) with the given y𝑦yitalic_y are reported. Thus, the reporting costs the same O(n+𝗈𝗎𝗍𝗉𝗎𝗍logn)𝑂𝑛𝗈𝗎𝗍𝗉𝗎𝗍𝑛O(n+\mathsf{output}\cdot\log n)italic_O ( italic_n + sansserif_output ⋅ roman_log italic_n ) time as in the general case. If we report at most one x𝑥xitalic_x for each y𝑦yitalic_y, then 𝗈𝗎𝗍𝗉𝗎𝗍=O(n)𝗈𝗎𝗍𝗉𝗎𝗍𝑂𝑛\mathsf{output}=O(n)sansserif_output = italic_O ( italic_n ). The lemma is proved.

Appendix F Reporting All 2-Covers Up To A Given Length

In this section, we prove Theorem 1.1. The algorithm operates in two phases. First, the algorithm reports all non-highly periodic 2222-covers (see Section F.1). Then, the algorithm uses the non-highly periodic 2222-covers to find all the highly periodic covers (see Section F.2).

F.1 Report All Non-Highly Periodic 2-Covers

In this section we prove the following lemma.

Lemma F.1.

Let S𝑆Sitalic_S be a string. There exists an algorithm that reports all non-highly periodic 2222-covers of S𝑆Sitalic_S (and may report also some highly periodic 2222-covers as well) in O(nlog4n+𝗈𝗎𝗍𝗉𝗎𝗍logn)𝑂𝑛superscript4𝑛𝗈𝗎𝗍𝗉𝗎𝗍𝑛O(n\log^{4}n+\mathsf{output}\cdot\log n)italic_O ( italic_n roman_log start_POSTSUPERSCRIPT 4 end_POSTSUPERSCRIPT italic_n + sansserif_output ⋅ roman_log italic_n ) time.

We split the proof of Lemma F.1 into two parts, one for reporting prefix-suffix 2222-covers, and one for reporting border-substring 2222-covers.

F.1.1 Report All Non-Highly Periodic Prefix-Suffix 2-Covers

The following lemma is useful in this section. In essence, we claim that the dimension of the ranges of Lemma 3.1 are actually 1111 (and not 2222) in the case in which f𝑓fitalic_f is an endpoint of S𝑆Sitalic_S.

Lemma F.2.

Let i[n]𝑖delimited-[]𝑛i\in[n]italic_i ∈ [ italic_n ] be an index and let k𝑘k\in\mathbb{N}italic_k ∈ blackboard_N. There exists a set subscript\mathcal{I}_{\ell}caligraphic_I start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT of O(1)𝑂1O(1)italic_O ( 1 ) intervals such that for any z𝑧z\in\mathbb{N}italic_z ∈ blackboard_N where 𝗉𝗋𝖾𝖿=S[1..1+z]𝗉𝗋𝖾𝖿𝑆delimited-[]1..1𝑧\mathsf{pref}=S[1..1+z]sansserif_pref = italic_S [ 1..1 + italic_z ] has z[1.5k..1.5k+1]𝑧delimited-[]superscript1.5𝑘superscript..1.5𝑘1z\in[1.5^{k}..1.5^{k+1}]italic_z ∈ [ 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT ..1.5 start_POSTSUPERSCRIPT italic_k + 1 end_POSTSUPERSCRIPT ] we have:

  1. 1.

    If zII𝑧subscript𝐼subscript𝐼z\in\bigcup_{I\in\mathcal{I}_{\ell}}Iitalic_z ∈ ⋃ start_POSTSUBSCRIPT italic_I ∈ caligraphic_I start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT end_POSTSUBSCRIPT italic_I, then 𝗉𝗋𝖾𝖿𝗉𝗋𝖾𝖿\mathsf{pref}sansserif_pref covers i𝑖iitalic_i.

  2. 2.

    If 𝗉𝗋𝖾𝖿𝗉𝗋𝖾𝖿\mathsf{pref}sansserif_pref is a non-highly periodic string covering i𝑖iitalic_i, then zII𝑧subscript𝐼subscript𝐼z\in\bigcup_{I\in\mathcal{I}_{\ell}}Iitalic_z ∈ ⋃ start_POSTSUBSCRIPT italic_I ∈ caligraphic_I start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT end_POSTSUBSCRIPT italic_I.

Symmetrically, there exists a set rsubscript𝑟\mathcal{I}_{r}caligraphic_I start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT of O(1)𝑂1O(1)italic_O ( 1 ) intervals such that for any z𝑧z\in\mathbb{N}italic_z ∈ blackboard_N where 𝗌𝗎𝖿𝖿=S[nz..n]\mathsf{suff}=S[n-z..n]sansserif_suff = italic_S [ italic_n - italic_z . . italic_n ] has z[1.5k..1.5k+1]𝑧delimited-[]superscript1.5𝑘superscript..1.5𝑘1z\in[1.5^{k}..1.5^{k+1}]italic_z ∈ [ 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT ..1.5 start_POSTSUPERSCRIPT italic_k + 1 end_POSTSUPERSCRIPT ] we have:

  1. 1.

    If zIrI𝑧subscript𝐼subscript𝑟𝐼z\in\bigcup_{I\in\mathcal{I}_{r}}Iitalic_z ∈ ⋃ start_POSTSUBSCRIPT italic_I ∈ caligraphic_I start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT end_POSTSUBSCRIPT italic_I, then 𝗌𝗎𝖿𝖿𝗌𝗎𝖿𝖿\mathsf{suff}sansserif_suff covers i𝑖iitalic_i.

  2. 2.

    If 𝗌𝗎𝖿𝖿𝗌𝗎𝖿𝖿\mathsf{suff}sansserif_suff is a non-highly periodic string covering i𝑖iitalic_i, then zIrI𝑧subscript𝐼subscript𝑟𝐼z\in\bigcup_{I\in\mathcal{I}_{r}}Iitalic_z ∈ ⋃ start_POSTSUBSCRIPT italic_I ∈ caligraphic_I start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT end_POSTSUBSCRIPT italic_I.

Moreover, subscript\mathcal{I}_{\ell}caligraphic_I start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT and rsubscript𝑟\mathcal{I}_{r}caligraphic_I start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT can be computed in O(log2n)𝑂superscript2𝑛O(\log^{2}n)italic_O ( roman_log start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT italic_n ) time.

Proof F.3.

We use Lemma 3.1 with f=1𝑓1f=1italic_f = 1, i𝑖iitalic_i and k𝑘kitalic_k to obtain a set subscript\mathcal{R}_{\ell}caligraphic_R start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT of rectangles. Let ={[a..b][c..d]×[a..b] and 0[c..d]}\mathcal{I}_{\ell}=\{[a..b]\mid[c..d]\times[a..b]\in\mathcal{R}\textit{ and }0% \in[c..d]\}caligraphic_I start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT = { [ italic_a . . italic_b ] ∣ [ italic_c . . italic_d ] × [ italic_a . . italic_b ] ∈ caligraphic_R and 0 ∈ [ italic_c . . italic_d ] }. We use again Lemma 3.1 with f=n𝑓𝑛f=nitalic_f = italic_n, i𝑖iitalic_i and k𝑘kitalic_k to obtain a set rsubscript𝑟\mathcal{R}_{r}caligraphic_R start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT of rectangles. Let r={[a..b]c[a..b]×[c..d] and 0[c..d]}\mathcal{I}_{r}=\{[a..b]\mid\exists_{c}[a..b]\times[c..d]\in\mathcal{R}\textit% { and }0\in[c..d]\}caligraphic_I start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT = { [ italic_a . . italic_b ] ∣ ∃ start_POSTSUBSCRIPT italic_c end_POSTSUBSCRIPT [ italic_a . . italic_b ] × [ italic_c . . italic_d ] ∈ caligraphic_R and 0 ∈ [ italic_c . . italic_d ] }.

Note that every point (,r)𝑟(\ell,r)( roman_ℓ , italic_r ) such that S[1..1+r]=S[1..1+z]𝑆delimited-[]1..1𝑟𝑆delimited-[]1..1𝑧S[1-\ell..1+r]=S[1..1+z]italic_S [ 1 - roman_ℓ ..1 + italic_r ] = italic_S [ 1..1 + italic_z ] must have =00\ell=0roman_ℓ = 0 and z=r𝑧𝑟z=ritalic_z = italic_r. Therefore, zII𝑧subscript𝐼𝐼z\in\bigcup_{I\in\mathcal{I}}Iitalic_z ∈ ⋃ start_POSTSUBSCRIPT italic_I ∈ caligraphic_I end_POSTSUBSCRIPT italic_I if and only if (0,z)RrR0𝑧subscript𝑅subscript𝑟𝑅(0,z)\in\bigcup_{R\in\mathcal{R}_{r}}R( 0 , italic_z ) ∈ ⋃ start_POSTSUBSCRIPT italic_R ∈ caligraphic_R start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT end_POSTSUBSCRIPT italic_R. The claim immediately follows from Lemma 3.1 (the proof for rsubscript𝑟\mathcal{I}_{r}caligraphic_I start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT is symmetric).

Now, we are ready to prove the following lemma, which yields the reporting mechanism stated in Lemma F.1 for prefix-suffix 2222-covers.

Lemma F.4.

Let S𝑆Sitalic_S be a string and m𝑚mitalic_m an integer. There is an algorithm that reports a set 𝒞msubscriptsuperscript𝒞𝑚\mathcal{C}^{\prime}_{m}caligraphic_C start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT italic_m end_POSTSUBSCRIPT of 2222-covers of length at most m𝑚mitalic_m such that every non non-highly periodic prefix-suffix 2222-covers of S𝑆Sitalic_S is in 𝒞msubscriptsuperscript𝒞𝑚\mathcal{C}^{\prime}_{m}caligraphic_C start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT italic_m end_POSTSUBSCRIPT in O(nlog4n+𝗈𝗎𝗍𝗉𝗎𝗍logn)𝑂𝑛superscript4𝑛𝗈𝗎𝗍𝗉𝗎𝗍𝑛O(n\log^{4}n+\mathsf{output}\cdot\log n)italic_O ( italic_n roman_log start_POSTSUPERSCRIPT 4 end_POSTSUPERSCRIPT italic_n + sansserif_output ⋅ roman_log italic_n ) time.

Proof F.5.

The algorithm starts with the common preprocess phase described in Section 3. The algorithm iterates over all pairs of integers (k,kr)subscript𝑘subscript𝑘𝑟(k_{\ell},k_{r})( italic_k start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT , italic_k start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ) such that 1.5knsuperscript1.5subscript𝑘𝑛1.5^{k_{\ell}}\leq n1.5 start_POSTSUPERSCRIPT italic_k start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT end_POSTSUPERSCRIPT ≤ italic_n and 1.5krnsuperscript1.5subscript𝑘𝑟𝑛1.5^{k_{r}}\leq n1.5 start_POSTSUPERSCRIPT italic_k start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT end_POSTSUPERSCRIPT ≤ italic_n. Let (k,kr)subscript𝑘subscript𝑘𝑟(k_{\ell},k_{r})( italic_k start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT , italic_k start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ) be such a pair, the algorithm processes (k,kr)subscript𝑘subscript𝑘𝑟(k_{\ell},k_{r})( italic_k start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT , italic_k start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ) as follows. For every i[n]𝑖delimited-[]𝑛i\in[n]italic_i ∈ [ italic_n ] the algorithm applies Lemma F.2 with k=k𝑘subscript𝑘k=k_{\ell}italic_k = italic_k start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT to get subscript\mathcal{I}_{\ell}caligraphic_I start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT and with k=kr𝑘subscript𝑘𝑟k=k_{r}italic_k = italic_k start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT to get rsubscript𝑟\mathcal{I}_{r}caligraphic_I start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT. Let i={I×[..]I}{[..]×IIr}\mathcal{R}_{i}=\{I\times[-\infty..\infty]\mid I\in\mathcal{I}_{\ell}\}\cup\{[% -\infty..\infty]\times I\mid I\in\mathcal{I}_{r}\}caligraphic_R start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT = { italic_I × [ - ∞ . . ∞ ] ∣ italic_I ∈ caligraphic_I start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT } ∪ { [ - ∞ . . ∞ ] × italic_I ∣ italic_I ∈ caligraphic_I start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT }. The algorithm uses Lemma C.1 to compute a set ¯isubscript¯𝑖\overline{\mathcal{R}}_{i}over¯ start_ARG caligraphic_R end_ARG start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT of rectangles such that R¯iR=[..]2RiR\bigcup_{R\in\overline{\mathcal{R}}_{i}}R=[-\infty..\infty]^{2}\setminus% \bigcup_{R\in\mathcal{R}_{i}}R⋃ start_POSTSUBSCRIPT italic_R ∈ over¯ start_ARG caligraphic_R end_ARG start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT end_POSTSUBSCRIPT italic_R = [ - ∞ . . ∞ ] start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT ∖ ⋃ start_POSTSUBSCRIPT italic_R ∈ caligraphic_R start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT end_POSTSUBSCRIPT italic_R in O(1)𝑂1O(1)italic_O ( 1 ) time. Let 𝖵𝖺𝗅𝗂𝖽={[1.5k..1.5k+1]×[1.5kr..1.5kr+1]}𝖵𝖺𝗅𝗂𝖽delimited-[]superscript1.5subscript𝑘superscript..1.5subscript𝑘1delimited-[]superscript1.5subscript𝑘𝑟superscript..1.5subscript𝑘𝑟1\mathsf{Valid}=\{[1.5^{k_{\ell}}..1.5^{k_{\ell}+1}]\times[1.5^{k_{r}}..1.5^{k_% {r}+1}]\}sansserif_Valid = { [ 1.5 start_POSTSUPERSCRIPT italic_k start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT end_POSTSUPERSCRIPT ..1.5 start_POSTSUPERSCRIPT italic_k start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT + 1 end_POSTSUPERSCRIPT ] × [ 1.5 start_POSTSUPERSCRIPT italic_k start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT end_POSTSUPERSCRIPT ..1.5 start_POSTSUPERSCRIPT italic_k start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT + 1 end_POSTSUPERSCRIPT ] }, and let 𝖵𝖺𝗅𝗂𝖽¯¯𝖵𝖺𝗅𝗂𝖽\overline{\mathsf{Valid}}over¯ start_ARG sansserif_Valid end_ARG be the inverse set of rectangles computed by Lemma C.1. Then, the algorithm computes ¯=(i[n]i¯)(𝖵𝖺𝗅𝗂𝖽¯)¯subscript𝑖delimited-[]𝑛¯subscript𝑖¯𝖵𝖺𝗅𝗂𝖽\overline{\mathcal{R}}=(\bigcup_{i\in[n]}\overline{\mathcal{R}_{i}})\cup(% \overline{\mathsf{Valid}})over¯ start_ARG caligraphic_R end_ARG = ( ⋃ start_POSTSUBSCRIPT italic_i ∈ [ italic_n ] end_POSTSUBSCRIPT over¯ start_ARG caligraphic_R start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT end_ARG ) ∪ ( over¯ start_ARG sansserif_Valid end_ARG ). Finally, the algorithm uses the third component of Lemma 5.2 with ¯¯\overline{\mathcal{R}}over¯ start_ARG caligraphic_R end_ARG and m2𝑚2m-2italic_m - 2 as a bound for x+y𝑥𝑦x+yitalic_x + italic_y. For every free point (,r)𝑟(\ell,r)( roman_ℓ , italic_r ) with respect to ¯¯\overline{\mathcal{R}}over¯ start_ARG caligraphic_R end_ARG reported by Lemma 5.2, the algorithm reports (S[1..1+],S[nr..n])(S[1..1+\ell],S[n-r..n])( italic_S [ 1..1 + roman_ℓ ] , italic_S [ italic_n - italic_r . . italic_n ] ) as a 2222-cover of S𝑆Sitalic_S.

Correctness.

Let (𝗉𝗋𝖾𝖿,𝗌𝗎𝖿𝖿)𝗉𝗋𝖾𝖿𝗌𝗎𝖿𝖿(\mathsf{pref},\mathsf{suff})( sansserif_pref , sansserif_suff ) be a non-highly periodic 2222-cover with 𝗉𝗋𝖾𝖿=S[1..1+]𝗉𝗋𝖾𝖿𝑆delimited-[]1..1\mathsf{pref}=S[1..1+\ell]sansserif_pref = italic_S [ 1..1 + roman_ℓ ], 𝗌𝗎𝖿𝖿=S[nr..n]\mathsf{suff}=S[n-r..n]sansserif_suff = italic_S [ italic_n - italic_r . . italic_n ] and +r+2m𝑟2𝑚\ell+r+2\leq mroman_ℓ + italic_r + 2 ≤ italic_m. Let ksubscript𝑘k_{\ell}italic_k start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT and krsubscript𝑘𝑟k_{r}italic_k start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT be the integers such that [1.5k..1.5k+1]delimited-[]superscript1.5subscript𝑘superscript..1.5subscript𝑘1\ell\in[1.5^{k_{\ell}}..1.5^{k_{\ell}+1}]roman_ℓ ∈ [ 1.5 start_POSTSUPERSCRIPT italic_k start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT end_POSTSUPERSCRIPT ..1.5 start_POSTSUPERSCRIPT italic_k start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT + 1 end_POSTSUPERSCRIPT ] and r[1.5kr..1.5kr+1]𝑟delimited-[]superscript1.5subscript𝑘𝑟superscript..1.5subscript𝑘𝑟1r\in[1.5^{k_{r}}..1.5^{k_{r}+1}]italic_r ∈ [ 1.5 start_POSTSUPERSCRIPT italic_k start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT end_POSTSUPERSCRIPT ..1.5 start_POSTSUPERSCRIPT italic_k start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT + 1 end_POSTSUPERSCRIPT ]. Clearly, (,r)[1.5k..1.5k+1]×[1.5kr..1.5kr+1]𝑟delimited-[]superscript1.5subscript𝑘superscript..1.5subscript𝑘1delimited-[]superscript1.5subscript𝑘𝑟superscript..1.5subscript𝑘𝑟1(\ell,r)\in[1.5^{k_{\ell}}..1.5^{k_{\ell}+1}]\times[1.5^{k_{r}}..1.5^{k_{r}+1}]( roman_ℓ , italic_r ) ∈ [ 1.5 start_POSTSUPERSCRIPT italic_k start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT end_POSTSUPERSCRIPT ..1.5 start_POSTSUPERSCRIPT italic_k start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT + 1 end_POSTSUPERSCRIPT ] × [ 1.5 start_POSTSUPERSCRIPT italic_k start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT end_POSTSUPERSCRIPT ..1.5 start_POSTSUPERSCRIPT italic_k start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT + 1 end_POSTSUPERSCRIPT ]. Additionally, since (𝗉𝗋𝖾𝖿,𝗌𝗎𝖿𝖿)𝗉𝗋𝖾𝖿𝗌𝗎𝖿𝖿(\mathsf{pref},\mathsf{suff})( sansserif_pref , sansserif_suff ) covers i𝑖iitalic_i for every i[n]𝑖delimited-[]𝑛i\in[n]italic_i ∈ [ italic_n ], it must be the case that (,r)R𝑟𝑅(\ell,r)\notin R( roman_ℓ , italic_r ) ∉ italic_R for every Ri¯𝑅¯subscript𝑖R\in\overline{\mathcal{R}_{i}}italic_R ∈ over¯ start_ARG caligraphic_R start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT end_ARG by Lemma F.2. It follows that (,r)𝑟(\ell,r)( roman_ℓ , italic_r ) is a free point with respect to ¯¯\overline{\mathcal{R}}over¯ start_ARG caligraphic_R end_ARG with +rm2𝑟𝑚2\ell+r\leq m-2roman_ℓ + italic_r ≤ italic_m - 2, as required.

Let (𝗉𝗋𝖾𝖿,𝗌𝗎𝖿𝖿)𝗉𝗋𝖾𝖿𝗌𝗎𝖿𝖿(\mathsf{pref},\mathsf{suff})( sansserif_pref , sansserif_suff ) be a pair reported by the algorithm with 𝗉𝗋𝖾𝖿=S[1..1+]𝗉𝗋𝖾𝖿𝑆delimited-[]1..1\mathsf{pref}=S[1..1+\ell]sansserif_pref = italic_S [ 1..1 + roman_ℓ ] and 𝗌𝗎𝖿𝖿=S[nr..n]\mathsf{suff}=S[n-r..n]sansserif_suff = italic_S [ italic_n - italic_r . . italic_n ]. Let (k,kr)subscript𝑘subscript𝑘𝑟(k_{\ell},k_{r})( italic_k start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT , italic_k start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ) be the pair such that (𝗉𝗋𝖾𝖿,𝗌𝗎𝖿𝖿)𝗉𝗋𝖾𝖿𝗌𝗎𝖿𝖿(\mathsf{pref},\mathsf{suff})( sansserif_pref , sansserif_suff ) was reported by the instance of Lemma 5.2 created when processing (k,kr)subscript𝑘subscript𝑘𝑟(k_{\ell},k_{r})( italic_k start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT , italic_k start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ). Let ¯¯\overline{\mathcal{R}}over¯ start_ARG caligraphic_R end_ARG and 𝖵𝖺𝗅𝗂𝖽¯¯𝖵𝖺𝗅𝗂𝖽\overline{\mathsf{Valid}}over¯ start_ARG sansserif_Valid end_ARG be the sets computed when processing (k,kr)subscript𝑘subscript𝑘𝑟(k_{\ell},k_{r})( italic_k start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT , italic_k start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ). Since (𝗉𝗋𝖾𝖿,𝗌𝗎𝖿𝖿)𝗉𝗋𝖾𝖿𝗌𝗎𝖿𝖿(\mathsf{pref},\mathsf{suff})( sansserif_pref , sansserif_suff ) was reported, it must be the case that (,r)𝑟(\ell,r)( roman_ℓ , italic_r ) is free with respect to ¯¯\overline{\mathcal{R}}over¯ start_ARG caligraphic_R end_ARG. Since 𝖵𝖺𝗅𝗂𝖽¯¯¯𝖵𝖺𝗅𝗂𝖽¯\overline{\mathsf{Valid}}\subseteq\overline{\mathcal{R}}over¯ start_ARG sansserif_Valid end_ARG ⊆ over¯ start_ARG caligraphic_R end_ARG it must be that (,r)𝖵𝖺𝗅𝗂𝖽𝑟𝖵𝖺𝗅𝗂𝖽(\ell,r)\in\mathsf{Valid}( roman_ℓ , italic_r ) ∈ sansserif_Valid which means [1.5k..1.5k+1]delimited-[]superscript1.5subscript𝑘superscript..1.5subscript𝑘1\ell\in[1.5^{k_{\ell}}..1.5^{k_{\ell}+1}]roman_ℓ ∈ [ 1.5 start_POSTSUPERSCRIPT italic_k start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT end_POSTSUPERSCRIPT ..1.5 start_POSTSUPERSCRIPT italic_k start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT + 1 end_POSTSUPERSCRIPT ] and r[1.5kr..1.5kr+1]𝑟delimited-[]superscript1.5subscript𝑘𝑟superscript..1.5subscript𝑘𝑟1r\in[1.5^{k_{r}}..1.5^{k_{r}+1}]italic_r ∈ [ 1.5 start_POSTSUPERSCRIPT italic_k start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT end_POSTSUPERSCRIPT ..1.5 start_POSTSUPERSCRIPT italic_k start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT + 1 end_POSTSUPERSCRIPT ]. Moreover, for every i[n]𝑖delimited-[]𝑛i\in[n]italic_i ∈ [ italic_n ] we have (,r)Ri¯R𝑟subscript𝑅¯subscript𝑖𝑅(\ell,r)\notin\bigcup_{R\in\overline{\mathcal{R}_{i}}}R( roman_ℓ , italic_r ) ∉ ⋃ start_POSTSUBSCRIPT italic_R ∈ over¯ start_ARG caligraphic_R start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT end_ARG end_POSTSUBSCRIPT italic_R and therefore (,r)RiR𝑟subscript𝑅subscript𝑖𝑅(\ell,r)\in\bigcup_{R\in\mathcal{R}_{i}}R( roman_ℓ , italic_r ) ∈ ⋃ start_POSTSUBSCRIPT italic_R ∈ caligraphic_R start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT end_POSTSUBSCRIPT italic_R. It follows from Lemma F.2 that (𝗉𝗋𝖾𝖿,𝗌𝗎𝖿𝖿)𝗉𝗋𝖾𝖿𝗌𝗎𝖿𝖿(\mathsf{pref},\mathsf{suff})( sansserif_pref , sansserif_suff ) covers i𝑖iitalic_i. It follows that (𝗉𝗋𝖾𝖿,𝗌𝗎𝖿𝖿)𝗉𝗋𝖾𝖿𝗌𝗎𝖿𝖿(\mathsf{pref},\mathsf{suff})( sansserif_pref , sansserif_suff ) is a 2222-cover. Finally, the point (,r)𝑟(\ell,r)( roman_ℓ , italic_r ) satisfies +rm2𝑟𝑚2\ell+r\leq m-2roman_ℓ + italic_r ≤ italic_m - 2 which leads to |𝗉𝗋𝖾𝖿|+|𝗌𝗎𝖿𝖿|=+r+2m𝗉𝗋𝖾𝖿𝗌𝗎𝖿𝖿𝑟2𝑚|\mathsf{pref}|+|\mathsf{suff}|=\ell+r+2\leq m| sansserif_pref | + | sansserif_suff | = roman_ℓ + italic_r + 2 ≤ italic_m as required.

Complexity.

The preprocess of Section 3 is computed in O(nlog2n)𝑂𝑛superscript2𝑛O(n\log^{2}n)italic_O ( italic_n roman_log start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT italic_n ) time. When a pair (k,kr)subscript𝑘subscript𝑘𝑟(k_{\ell},k_{r})( italic_k start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT , italic_k start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ) is processed, Lemma F.2 and Lemma C.1 are applied O(n)𝑂𝑛O(n)italic_O ( italic_n ) times, which takes O(nlog2n)𝑂𝑛superscript2𝑛O(n\log^{2}n)italic_O ( italic_n roman_log start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT italic_n ) time. Then, Lemma 5.2 is applied on a set of Θ(n)Θ𝑛\Theta(n)roman_Θ ( italic_n ) rectangles, which takes O(nlog3n+𝗈𝗎𝗍𝗉𝗎𝗍(k,kr)log2n)𝑂𝑛superscript3𝑛subscript𝗈𝗎𝗍𝗉𝗎𝗍subscript𝑘subscript𝑘𝑟superscript2𝑛O(n\log^{3}n+\mathsf{output}_{(k_{\ell},k_{r})}\cdot\log^{2}n)italic_O ( italic_n roman_log start_POSTSUPERSCRIPT 3 end_POSTSUPERSCRIPT italic_n + sansserif_output start_POSTSUBSCRIPT ( italic_k start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT , italic_k start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ) end_POSTSUBSCRIPT ⋅ roman_log start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT italic_n ) time, with 𝗈𝗎𝗍𝗉𝗎𝗍(k,kr)subscript𝗈𝗎𝗍𝗉𝗎𝗍subscript𝑘subscript𝑘𝑟\mathsf{output}_{(k_{\ell},k_{r})}sansserif_output start_POSTSUBSCRIPT ( italic_k start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT , italic_k start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ) end_POSTSUBSCRIPT being the set of free points with respect to the rectangles ¯¯\overline{\mathcal{R}}over¯ start_ARG caligraphic_R end_ARG created for the pair (k,kr)subscript𝑘subscript𝑘𝑟(k_{\ell},k_{r})( italic_k start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT , italic_k start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ). Due to the inclusion of 𝖵𝖺𝗅𝗂𝖽¯¯𝖵𝖺𝗅𝗂𝖽\overline{\mathsf{Valid}}over¯ start_ARG sansserif_Valid end_ARG in ¯¯\overline{\mathcal{R}}over¯ start_ARG caligraphic_R end_ARG, we have that every reported point (,r)𝑟(\ell,r)( roman_ℓ , italic_r ) has [1.5k..1.5k+1]delimited-[]superscript1.5subscript𝑘superscript..1.5subscript𝑘1\ell\in[1.5^{k_{\ell}}..1.5^{k_{\ell}+1}]roman_ℓ ∈ [ 1.5 start_POSTSUPERSCRIPT italic_k start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT end_POSTSUPERSCRIPT ..1.5 start_POSTSUPERSCRIPT italic_k start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT + 1 end_POSTSUPERSCRIPT ] and r[1.5kr..1.5kr+1]𝑟delimited-[]superscript1.5subscript𝑘𝑟superscript..1.5subscript𝑘𝑟1r\in[1.5^{k_{r}}..1.5^{k_{r}+1}]italic_r ∈ [ 1.5 start_POSTSUPERSCRIPT italic_k start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT end_POSTSUPERSCRIPT ..1.5 start_POSTSUPERSCRIPT italic_k start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT + 1 end_POSTSUPERSCRIPT ]. It immediately follows that every point is reported at most once. It follows from Lemma F.2 that every free point (,r)𝑟(\ell,r)( roman_ℓ , italic_r ) with respect to ¯¯\overline{\mathcal{R}}over¯ start_ARG caligraphic_R end_ARG corresponds to a 2222-cover (S[1..1+],S[nr..n])(S[1..1+\ell],S[n-r..n])( italic_S [ 1..1 + roman_ℓ ] , italic_S [ italic_n - italic_r . . italic_n ] ). Notice that the same 2222-cover may be reported at most twice - in the case in which S[1..1+]=S[n..n]S[1..1+\ell]=S[n-\ell..n]italic_S [ 1..1 + roman_ℓ ] = italic_S [ italic_n - roman_ℓ . . italic_n ] and S[1..1+r]=S[nr..n]S[1..1+r]=S[n-r..n]italic_S [ 1..1 + italic_r ] = italic_S [ italic_n - italic_r . . italic_n ]. It follows that the accumulated size of 𝗈𝗎𝗍𝗉𝗎𝗍(k,kr)subscript𝗈𝗎𝗍𝗉𝗎𝗍subscript𝑘subscript𝑘𝑟\mathsf{output}_{(k_{\ell},k_{r})}sansserif_output start_POSTSUBSCRIPT ( italic_k start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT , italic_k start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ) end_POSTSUBSCRIPT across all pairs (k,kr)subscript𝑘subscript𝑘𝑟(k_{\ell},k_{r})( italic_k start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT , italic_k start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ) is bounded by 2𝗈𝗎𝗍𝗉𝗎𝗍2𝗈𝗎𝗍𝗉𝗎𝗍2\cdot\mathsf{output}2 ⋅ sansserif_output, with 𝗈𝗎𝗍𝗉𝗎𝗍𝗈𝗎𝗍𝗉𝗎𝗍\mathsf{output}sansserif_output being the set of 2222-covers reported by the algorithm. In conclusion, the total running time is bounded by O(nlog4+𝗈𝗎𝗍𝗉𝗎𝗍logn)𝑂𝑛superscript4𝗈𝗎𝗍𝗉𝗎𝗍𝑛O(n\log^{4}+\mathsf{output}\cdot\log n)italic_O ( italic_n roman_log start_POSTSUPERSCRIPT 4 end_POSTSUPERSCRIPT + sansserif_output ⋅ roman_log italic_n ) due to the existence of O(log2n)𝑂superscript2𝑛O(\log^{2}n)italic_O ( roman_log start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT italic_n ) pairs (k,kr)subscript𝑘subscript𝑘𝑟(k_{\ell},k_{r})( italic_k start_POSTSUBSCRIPT roman_ℓ end_POSTSUBSCRIPT , italic_k start_POSTSUBSCRIPT italic_r end_POSTSUBSCRIPT ).

F.1.2 Report All Non-Highly Periodic Border-Substring 2-Covers

We proceed to prove that all non-highly periodic border-substring 2222-covers can be reported efficiently.

Lemma F.6.

Let S𝑆Sitalic_S be a string and m𝑚mitalic_m be an integer. There exists an algorithm that reports a set 𝒞msubscript𝒞𝑚\mathcal{C}_{m}caligraphic_C start_POSTSUBSCRIPT italic_m end_POSTSUBSCRIPT of 2222-covers with length at most m𝑚mitalic_m, such that every non-highly periodic border-substring 2222-covers of S𝑆Sitalic_S is in 𝒞msubscript𝒞𝑚\mathcal{C}_{m}caligraphic_C start_POSTSUBSCRIPT italic_m end_POSTSUBSCRIPT. The running time of the algorithm is O(nlog4n+𝗈𝗎𝗍𝗉𝗎𝗍log2n)𝑂𝑛superscript4𝑛𝗈𝗎𝗍𝗉𝗎𝗍superscript2𝑛O(n\log^{4}n+\mathsf{output}\cdot\log^{2}n)italic_O ( italic_n roman_log start_POSTSUPERSCRIPT 4 end_POSTSUPERSCRIPT italic_n + sansserif_output ⋅ roman_log start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT italic_n ).

Proof F.7.

The algorithm starts with the common preprocessing phase described in Section 3. We also assume that all 1111-covers are found in advance via the linear time algorithm of Moore and Smyth [23, 24].. Every 2222-cover containing a 1111-cover is implicitly reported via the corresponding 1111-cover. The algorithm iterates every pair (b,k)𝑏𝑘(b,k)( italic_b , italic_k ) such that S[1..b]S[1..b]italic_S [ 1 . . italic_b ] is a non-highly periodic border and k𝑘kitalic_k is an integer such that 1.5knsuperscript1.5𝑘𝑛1.5^{k}\leq n1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT ≤ italic_n as follows. The algorithm finds all occurrences of S[1..b]S[1..b]italic_S [ 1 . . italic_b ] in S𝑆Sitalic_S using Lemma 2.11. If every index in S𝑆Sitalic_S is covered by S[1..b]S[1..b]italic_S [ 1 . . italic_b ], the algorithm simply ignores S[1..b]S[1..b]italic_S [ 1 . . italic_b ], as all pairs including it are implicitly reported as the 1111-cover S[1..b]S[1..b]italic_S [ 1 . . italic_b ] If S[1..b]S[1..b]italic_S [ 1 . . italic_b ] is not a 1111-cover, the algorithm picks an arbitrary index f𝑓fitalic_f not covered by S[1..b]S[1..b]italic_S [ 1 . . italic_b ]. For every index i𝑖iitalic_i that is not covered by S[1..b]S[1..b]italic_S [ 1 . . italic_b ], the algorithm applies Lemma 3.1 with f𝑓fitalic_f,i𝑖iitalic_i, and k𝑘kitalic_k to obtain a set isubscript𝑖\mathcal{R}_{i}caligraphic_R start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT of rectangles. The algorithm then applies Lemma C.1 to obtain a set of O(1)𝑂1O(1)italic_O ( 1 ) rectangles ¯isubscript¯𝑖\overline{\mathcal{R}}_{i}over¯ start_ARG caligraphic_R end_ARG start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT such that R¯iR=[..]2RiR\bigcup_{R\in\overline{\mathcal{R}}_{i}}R=[-\infty..\infty]^{2}\setminus% \bigcup_{R\in\mathcal{R}_{i}}R⋃ start_POSTSUBSCRIPT italic_R ∈ over¯ start_ARG caligraphic_R end_ARG start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT end_POSTSUBSCRIPT italic_R = [ - ∞ . . ∞ ] start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT ∖ ⋃ start_POSTSUBSCRIPT italic_R ∈ caligraphic_R start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT end_POSTSUBSCRIPT italic_R, The algorithm then creates a set 𝖵𝖺𝗅𝗂𝖽¯¯𝖵𝖺𝗅𝗂𝖽\overline{\mathsf{Valid}}over¯ start_ARG sansserif_Valid end_ARG of O(n)𝑂𝑛O(n)italic_O ( italic_n ) rectangles such that a point (,r)𝑟(\ell,r)( roman_ℓ , italic_r ) is not in a rectangle of 𝖵𝖺𝗅𝗂𝖽¯¯𝖵𝖺𝗅𝗂𝖽\overline{\mathsf{Valid}}over¯ start_ARG sansserif_Valid end_ARG if and only if +r+1[1.5k..1.5k+1]𝑟1delimited-[]superscript1.5𝑘superscript..1.5𝑘1\ell+r+1\in[1.5^{k}..1.5^{k+1}]roman_ℓ + italic_r + 1 ∈ [ 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT ..1.5 start_POSTSUPERSCRIPT italic_k + 1 end_POSTSUPERSCRIPT ]. Note that there is a set of O(1.5k)=O(n)𝑂superscript1.5𝑘𝑂𝑛O(1.5^{k})=O(n)italic_O ( 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT ) = italic_O ( italic_n ) rectangles that satisfy this constraint. Finally, the algorithm applies the third variant of Lemma 5.2 on the set R¯=(iU¯i)(𝖵𝖺𝗅𝗂𝖽¯)¯𝑅subscript𝑖𝑈subscript¯𝑖¯𝖵𝖺𝗅𝗂𝖽\overline{R}=(\bigcup_{i\in U}\overline{\mathcal{R}}_{i})\cup(\overline{% \mathsf{Valid}})over¯ start_ARG italic_R end_ARG = ( ⋃ start_POSTSUBSCRIPT italic_i ∈ italic_U end_POSTSUBSCRIPT over¯ start_ARG caligraphic_R end_ARG start_POSTSUBSCRIPT italic_i end_POSTSUBSCRIPT ) ∪ ( over¯ start_ARG sansserif_Valid end_ARG ) with U𝑈Uitalic_U being the set of indices not covered by S[1..b]S[1..b]italic_S [ 1 . . italic_b ] and m1b𝑚1𝑏m-1-bitalic_m - 1 - italic_b the bound on x+y𝑥𝑦x+yitalic_x + italic_y. For every free point (,r)𝑟(\ell,r)( roman_ℓ , italic_r ) obtained by Lemma 5.2, the algorithm reports the pair (S[1..b],S[f..f+r])(S[1..b],S[f-\ell..f+r])( italic_S [ 1 . . italic_b ] , italic_S [ italic_f - roman_ℓ . . italic_f + italic_r ] ) as a 2222-cover.

Correctness

Let (𝖻𝗈𝗋,𝗌𝗎𝖻)𝖻𝗈𝗋𝗌𝗎𝖻(\mathsf{bor},\mathsf{sub})( sansserif_bor , sansserif_sub ) be a non-highly periodic border-substring 2222-cover with 𝖻𝗈𝗋=S[1..b]\mathsf{bor}=S[1..b]sansserif_bor = italic_S [ 1 . . italic_b ] and b+|𝗌𝗎𝖻|m𝑏𝗌𝗎𝖻𝑚b+|\mathsf{sub}|\leq mitalic_b + | sansserif_sub | ≤ italic_m. If every index in S𝑆Sitalic_S is covered by an occurrence of 𝖻𝗈𝗋𝖻𝗈𝗋\mathsf{bor}sansserif_bor, the pair is report implicitly via the 1111-cover 𝖻𝗈𝗋𝖻𝗈𝗋\mathsf{bor}sansserif_bor. Otherwise, the index f𝑓fitalic_f not covered by 𝖻𝗈𝗋𝖻𝗈𝗋\mathsf{bor}sansserif_bor picked by the algorithm must be covered by 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub. It follows that 𝗌𝗎𝖻=[f..f+r]\mathsf{sub}=[f-\ell..f+r]sansserif_sub = [ italic_f - roman_ℓ . . italic_f + italic_r ] for some ,r𝑟\ell,r\in\mathbb{N}roman_ℓ , italic_r ∈ blackboard_N. Let k𝑘kitalic_k be the integer such that |𝗌𝗎𝖻|=+r1[1.5k..1.5k+1]𝗌𝗎𝖻𝑟1delimited-[]superscript1.5𝑘superscript..1.5𝑘1|\mathsf{sub}|=\ell+r-1\in[1.5^{k}..1.5^{k+1}]| sansserif_sub | = roman_ℓ + italic_r - 1 ∈ [ 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT ..1.5 start_POSTSUPERSCRIPT italic_k + 1 end_POSTSUPERSCRIPT ]. Let ¯¯\overline{\mathcal{R}}over¯ start_ARG caligraphic_R end_ARG be the set of rectangles on which the algorithm applied Lemma 5.2 when the pair (b,k)𝑏𝑘(b,k)( italic_b , italic_k ) was processed. It is clear from the definition of 𝖵𝖺𝗅𝗂𝖽¯¯𝖵𝖺𝗅𝗂𝖽\overline{\mathsf{Valid}}over¯ start_ARG sansserif_Valid end_ARG that the point (,r)𝑟(\ell,r)( roman_ℓ , italic_r ) is not in a rectangle of 𝖵𝖺𝗅𝗂𝖽¯¯𝖵𝖺𝗅𝗂𝖽\overline{\mathsf{Valid}}over¯ start_ARG sansserif_Valid end_ARG. Note that every index in S𝑆Sitalic_S not covered by 𝖻𝗈𝗋𝖻𝗈𝗋\mathsf{bor}sansserif_bor must be covered by 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub. It follows from Lemma 3.1 and from |𝗌𝗎𝖻|[1.5k..1.5k+1]𝗌𝗎𝖻delimited-[]superscript1.5𝑘superscript..1.5𝑘1|\mathsf{sub}|\in[1.5^{k}..1.5^{k+1}]| sansserif_sub | ∈ [ 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT ..1.5 start_POSTSUPERSCRIPT italic_k + 1 end_POSTSUPERSCRIPT ] that (,r)𝑟(\ell,r)( roman_ℓ , italic_r ) is a free point with respect to ¯¯\overline{\mathcal{R}}over¯ start_ARG caligraphic_R end_ARG, and therefore the pair (S[1..b],S[f..f+r])(S[1..b],S[f-\ell..f+r])( italic_S [ 1 . . italic_b ] , italic_S [ italic_f - roman_ℓ . . italic_f + italic_r ] ) is reported as a 2222-cover. Finally the point (+r)𝑟(\ell+r)( roman_ℓ + italic_r ) respects the bound of the third variant of Definition 5.1, since |𝖻𝗈𝗋|+|𝗌𝗎𝖻|=b+r++1m𝖻𝗈𝗋𝗌𝗎𝖻𝑏𝑟1𝑚|\mathsf{bor}|+|\mathsf{sub}|=b+r+\ell+1\leq m| sansserif_bor | + | sansserif_sub | = italic_b + italic_r + roman_ℓ + 1 ≤ italic_m as required.

Now, let (S[1..b],S[f..f+r])(S[1..b],S[f-\ell..f+r])( italic_S [ 1 . . italic_b ] , italic_S [ italic_f - roman_ℓ . . italic_f + italic_r ] ) be a pair reported by the algorithm. Let k𝑘kitalic_k be the unique integer such that +r1[1.5k..1.5k+1]𝑟1delimited-[]superscript1.5𝑘superscript..1.5𝑘1\ell+r-1\in[1.5^{k}..1.5^{k+1}]roman_ℓ + italic_r - 1 ∈ [ 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT ..1.5 start_POSTSUPERSCRIPT italic_k + 1 end_POSTSUPERSCRIPT ]. Since the pair (S[1..b],S[f..f+r](S[1..b],S[f-\ell..f+r]( italic_S [ 1 . . italic_b ] , italic_S [ italic_f - roman_ℓ . . italic_f + italic_r ] was reported by the algorithm, it must be the case that (,r)𝑟(\ell,r)( roman_ℓ , italic_r ) is a free point with respect to a set of rectangles ¯¯\overline{\mathcal{R}}over¯ start_ARG caligraphic_R end_ARG created when processing some pair (b,k)𝑏superscript𝑘(b,k^{\prime})( italic_b , italic_k start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT ). Due to the inclusion of 𝖵𝖺𝗅𝗂𝖽¯¯𝖵𝖺𝗅𝗂𝖽\overline{\mathsf{Valid}}over¯ start_ARG sansserif_Valid end_ARG in ¯¯\overline{\mathcal{R}}over¯ start_ARG caligraphic_R end_ARG and the uniqueness of k𝑘kitalic_k, it must be the case that k=ksuperscript𝑘𝑘k^{\prime}=kitalic_k start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT = italic_k. According to Lemma 3.1, it follows from (,r)𝑟(\ell,r)( roman_ℓ , italic_r ) being a free point with respect to ¯¯\overline{\mathcal{R}}over¯ start_ARG caligraphic_R end_ARG and from +r+1[1.5k..1.5k+1]𝑟1delimited-[]superscript1.5𝑘superscript..1.5𝑘1\ell+r+1\in[1.5^{k}..1.5^{k+1}]roman_ℓ + italic_r + 1 ∈ [ 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT ..1.5 start_POSTSUPERSCRIPT italic_k + 1 end_POSTSUPERSCRIPT ] that S[f..f+r]S[f-\ell..f+r]italic_S [ italic_f - roman_ℓ . . italic_f + italic_r ] covers every index that is not covered by S[1..b]S[1..b]italic_S [ 1 . . italic_b ], so (S[1..b],S[f..f+r])(S[1..b],S[f-\ell..f+r])( italic_S [ 1 . . italic_b ] , italic_S [ italic_f - roman_ℓ . . italic_f + italic_r ] ) is indeed a 2222-cover. Finally, the bound on +r𝑟\ell+rroman_ℓ + italic_r on any reported point ensures that |S[1..b]|+S[f..f+r]|=b++r+1m|S[1..b]|+S[f-\ell..f+r]|=b+\ell+r+1\leq m| italic_S [ 1 . . italic_b ] | + italic_S [ italic_f - roman_ℓ . . italic_f + italic_r ] | = italic_b + roman_ℓ + italic_r + 1 ≤ italic_m as required.

Complexity

The preprocessing of Section 3 is carried in O(nlog2n)𝑂𝑛superscript2𝑛O(n\log^{2}n)italic_O ( italic_n roman_log start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT italic_n ) time. The set of all non-highly periodic borders is computed in O(n)𝑂𝑛O(n)italic_O ( italic_n ) time as in Section D.2. For every 1111-cover S[1..b]S[1..b]italic_S [ 1 . . italic_b ] identified the algorithm, the algorithm applies Lemma 2.14 to report all 2222-covers containing S[1..b]S[1..b]italic_S [ 1 . . italic_b ] in time O(𝗈𝗎𝗍𝗉𝗎𝗍b)𝑂subscript𝗈𝗎𝗍𝗉𝗎𝗍𝑏O(\mathsf{output}_{b})italic_O ( sansserif_output start_POSTSUBSCRIPT italic_b end_POSTSUBSCRIPT ) with 𝗈𝗎𝗍𝗉𝗎𝗍bsubscript𝗈𝗎𝗍𝗉𝗎𝗍𝑏\mathsf{output}_{b}sansserif_output start_POSTSUBSCRIPT italic_b end_POSTSUBSCRIPT being the number of such borders. We proceed to consider non-highly periodic borders S[1..b]S[1..b]italic_S [ 1 . . italic_b ] that are not 1111-covers. For every pair (b,k)𝑏𝑘(b,k)( italic_b , italic_k ), the algorithm applies pattern matching once, and creates O(n)𝑂𝑛O(n)italic_O ( italic_n ) rectangles using Lemma 3.1. The time complexity of these operations sums up to O(nlog2n)𝑂𝑛superscript2𝑛O(n\log^{2}n)italic_O ( italic_n roman_log start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT italic_n ). Then, the algorithm finds free point on a set of Θ(n)Θ𝑛\Theta(n)roman_Θ ( italic_n ) rectangles using Lemma 5.2. The time complexity of this part of the algorithm is O(nlog3n+log2n𝗈𝗎𝗍𝗉𝗎𝗍b,k)𝑂𝑛superscript3𝑛superscript2𝑛subscript𝗈𝗎𝗍𝗉𝗎𝗍𝑏𝑘O(n\log^{3}n+\log^{2}n\cdot\mathsf{output}_{b,k})italic_O ( italic_n roman_log start_POSTSUPERSCRIPT 3 end_POSTSUPERSCRIPT italic_n + roman_log start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT italic_n ⋅ sansserif_output start_POSTSUBSCRIPT italic_b , italic_k end_POSTSUBSCRIPT ) with 𝗈𝗎𝗍𝗉𝗎𝗍b,ksubscript𝗈𝗎𝗍𝗉𝗎𝗍𝑏𝑘\mathsf{output}_{b,k}sansserif_output start_POSTSUBSCRIPT italic_b , italic_k end_POSTSUBSCRIPT being the number of free points with respect to the rectangle set ¯¯\overline{\mathcal{R}}over¯ start_ARG caligraphic_R end_ARG created when processing (b,k)𝑏𝑘(b,k)( italic_b , italic_k ). We bound the number of times that a 2222-cover (S[1..b],𝗌𝗎𝖻)(S[1..b],\mathsf{sub})( italic_S [ 1 . . italic_b ] , sansserif_sub ) can be reported by the algorithm via a free point. Let k𝑘kitalic_k be the unique integer such that |𝗌𝗎𝖻|[1.5k..1.5k+1]𝗌𝗎𝖻delimited-[]superscript1.5𝑘superscript..1.5𝑘1|\mathsf{sub}|\in[1.5^{k}..1.5^{k+1}]| sansserif_sub | ∈ [ 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT ..1.5 start_POSTSUPERSCRIPT italic_k + 1 end_POSTSUPERSCRIPT ]. When the pair (b,k)𝑏𝑘(b,k)( italic_b , italic_k ) is being processed, (S[1..b],𝗌𝗎𝖻)(S[1..b],\mathsf{sub})( italic_S [ 1 . . italic_b ] , sansserif_sub ) may be reported multiple times due to multiple occurrences of 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub in the proximity of f𝑓fitalic_f. More specifically, there may be two (or more) points (1,r1)subscript1subscript𝑟1(\ell_{1},r_{1})( roman_ℓ start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT , italic_r start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT ) and (2,r2)subscript2subscript𝑟2(\ell_{2},r_{2})( roman_ℓ start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT , italic_r start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ) such that S[f1..f+r1]=S[f2..f+r2]=𝗌𝗎𝖻S[f-\ell_{1}..f+r_{1}]=S[f-\ell_{2}..f+r_{2}]=\mathsf{sub}italic_S [ italic_f - roman_ℓ start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT . . italic_f + italic_r start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT ] = italic_S [ italic_f - roman_ℓ start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT . . italic_f + italic_r start_POSTSUBSCRIPT 2 end_POSTSUBSCRIPT ] = sansserif_sub. We claim that there are at most 6666 such points. This is since due to Lemma 3.1, we have 𝗉𝖾𝗋(𝗌𝗎𝖻)1.5k4𝗉𝖾𝗋𝗌𝗎𝖻superscript1.5𝑘4\mathsf{per}(\mathsf{sub})\geq\frac{1.5^{k}}{4}sansserif_per ( sansserif_sub ) ≥ divide start_ARG 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT end_ARG start_ARG 4 end_ARG (due to (,r)𝑟(\ell,r)( roman_ℓ , italic_r ) being a free point with respect to ¯¯\overline{\mathcal{R}}over¯ start_ARG caligraphic_R end_ARG). It follows that two starting indices of occurrences of 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub must be at least 1.5k4superscript1.5𝑘4\frac{1.5^{k}}{4}divide start_ARG 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT end_ARG start_ARG 4 end_ARG indices apart. Since |𝗌𝗎𝖻|1.5k+1𝗌𝗎𝖻superscript1.5𝑘1|\mathsf{sub}|\leq 1.5^{k+1}| sansserif_sub | ≤ 1.5 start_POSTSUPERSCRIPT italic_k + 1 end_POSTSUPERSCRIPT we have that at most 1.5k+1/1.5k4=6superscript1.5𝑘1superscript1.5𝑘461.5^{k+1}/\frac{1.5^{k}}{4}=61.5 start_POSTSUPERSCRIPT italic_k + 1 end_POSTSUPERSCRIPT / divide start_ARG 1.5 start_POSTSUPERSCRIPT italic_k end_POSTSUPERSCRIPT end_ARG start_ARG 4 end_ARG = 6 occurrences of 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub can touch the index f𝑓fitalic_f. The 2222-cover (S[1..b],𝗌𝗎𝖻)(S[1..b],\mathsf{sub})( italic_S [ 1 . . italic_b ] , sansserif_sub ) can also be reported with reversed roles i.e. with S[1..b]S[1..b]italic_S [ 1 . . italic_b ] acting as the substring and 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub as the border. This can only happen when processing the pair (b,k)superscript𝑏superscript𝑘(b^{\prime},k^{\prime})( italic_b start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT , italic_k start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT ) with ksuperscript𝑘k^{\prime}italic_k start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT being the unique integer such that b[1.5k..1.5k+1]𝑏delimited-[]superscript1.5superscript𝑘superscript..1.5superscript𝑘1b\in[1.5^{k^{\prime}}..1.5^{k^{\prime}+1}]italic_b ∈ [ 1.5 start_POSTSUPERSCRIPT italic_k start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT end_POSTSUPERSCRIPT ..1.5 start_POSTSUPERSCRIPT italic_k start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT + 1 end_POSTSUPERSCRIPT ] and S[1..b]=𝗌𝗎𝖻S[1..b^{\prime}]=\mathsf{sub}italic_S [ 1 . . italic_b start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT ] = sansserif_sub. Due to the same reasoning as before, the 2222-cover can be reported at most O(1)𝑂1O(1)italic_O ( 1 ) times when the pair (b,k)superscript𝑏superscript𝑘(b^{\prime},k^{\prime})( italic_b start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT , italic_k start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT ) is processed. It follows from the above analysis that across all pairs (b,k)𝑏𝑘(b,k)( italic_b , italic_k ), a 2222-cover can be reported at most 12=O(1)12𝑂112=O(1)12 = italic_O ( 1 ) times. It follows that the total contribution of the O(log2n𝗈𝗎𝗍𝗉𝗎𝗍(b,k))𝑂superscript2𝑛subscript𝗈𝗎𝗍𝗉𝗎𝗍𝑏𝑘O(\log^{2}n\cdot\mathsf{output}_{(b,k)})italic_O ( roman_log start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT italic_n ⋅ sansserif_output start_POSTSUBSCRIPT ( italic_b , italic_k ) end_POSTSUBSCRIPT ) component to the time complexity is O(log2n𝗈𝗎𝗍𝗉𝗎𝗍)𝑂superscript2𝑛𝗈𝗎𝗍𝗉𝗎𝗍O(\log^{2}n\cdot\mathsf{output})italic_O ( roman_log start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT italic_n ⋅ sansserif_output ). In conclusion, the algorithm runs in time O(nlog4n+logn𝗈𝗎𝗍𝗉𝗎𝗍)𝑂𝑛superscript4𝑛𝑛𝗈𝗎𝗍𝗉𝗎𝗍O(n\log^{4}n+\log n\cdot\mathsf{output})italic_O ( italic_n roman_log start_POSTSUPERSCRIPT 4 end_POSTSUPERSCRIPT italic_n + roman_log italic_n ⋅ sansserif_output ) (due to the existence of O(log2n)𝑂superscript2𝑛O(\log^{2}n)italic_O ( roman_log start_POSTSUPERSCRIPT 2 end_POSTSUPERSCRIPT italic_n ) pairs (b,k)𝑏𝑘(b,k)( italic_b , italic_k )).

Note that Lemma F.1 follows directly from Lemmas F.4 and F.6.

F.2 Report All Highly-Periodic 2-Covers

We are now ready to prove Theorem 1.1.

Proof F.8 (Proof of Theorem 1.1).

The algorithm starts by applying Lemma F.1 to obtain and report every non-highly periodic 2222-cover of S𝑆Sitalic_S with length bounded by m𝑚mitalic_m. In addition, the algorithm constructs the 2222-cover oracle of Theorem 1.3. In particular, we assume that the algorithm has access to 𝖣𝗂𝖼𝗍𝖣𝗂𝖼𝗍\mathsf{Dict}sansserif_Dict and 𝖣𝗂𝖼𝗍qsubscript𝖣𝗂𝖼𝗍𝑞\mathsf{Dict}_{q}sansserif_Dict start_POSTSUBSCRIPT italic_q end_POSTSUBSCRIPT presented in Section D.2. The key idea for obtaining the rest of the 2222-covers is attempting to extend short periodic strings components of non-highly periodic 2222-covers.

Prefix-Suffix.

To streamline the presentation, we present two operators for extending a periodic substring.

Definition F.9.

Let S𝑆Sitalic_S be a string and let 𝗉𝗋𝖾𝖿=S[1..p]\mathsf{pref}=S[1..p]sansserif_pref = italic_S [ 1 . . italic_p ] be a ρ𝜌\rhoitalic_ρ-periodic prefix of S𝑆Sitalic_S. The Operator 𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽(𝗉𝗋𝖾𝖿)𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽𝗉𝗋𝖾𝖿\mathsf{PrefExtend}(\mathsf{pref})sansserif_PrefExtend ( sansserif_pref ) is defined as follows.

𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽(𝗉𝗋𝖾𝖿)={S[1..p+ρ]if S[1..p+ρ] is ρ-periodic𝖤𝗇𝖽otherwise.\mathsf{PrefExtend}(\mathsf{pref})=\begin{cases}S[1..p+\rho]&\textsf{if }S[1..% p+\rho]\textsf{ is }\rho\textsf{-periodic}\\ \mathsf{End}&\text{otherwise.}\end{cases}sansserif_PrefExtend ( sansserif_pref ) = { start_ROW start_CELL italic_S [ 1 . . italic_p + italic_ρ ] end_CELL start_CELL if italic_S [ 1 . . italic_p + italic_ρ ] is italic_ρ -periodic end_CELL end_ROW start_ROW start_CELL sansserif_End end_CELL start_CELL otherwise. end_CELL end_ROW

Similarly, the operator 𝖲𝗎𝖿𝖿𝖤𝗑𝗍𝖾𝗇𝖽(.)\mathsf{SuffExtend}(.)sansserif_SuffExtend ( . ) is defined on a ρ𝜌\rhoitalic_ρ-periodic suffix 𝗌𝗎𝖿𝖿=S[s..n]\mathsf{suff}=S[s..n]sansserif_suff = italic_S [ italic_s . . italic_n ] as follows.

𝖲𝗎𝖿𝖿𝖤𝗑𝗍𝖾𝗇𝖽(𝗉𝗋𝖾𝖿)={S[sρ..n]if S[sρ..n] is ρ-periodic𝖤𝗇𝖽otherwise.\mathsf{SuffExtend}(\mathsf{pref})=\begin{cases}S[s-\rho..n]&\textsf{if }S[s-% \rho..n]\textsf{ is }\rho\textsf{-periodic}\\ \mathsf{End}&\text{otherwise.}\end{cases}sansserif_SuffExtend ( sansserif_pref ) = { start_ROW start_CELL italic_S [ italic_s - italic_ρ . . italic_n ] end_CELL start_CELL if italic_S [ italic_s - italic_ρ . . italic_n ] is italic_ρ -periodic end_CELL end_ROW start_ROW start_CELL sansserif_End end_CELL start_CELL otherwise. end_CELL end_ROW

We slightly abuse notation by interpreting 𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽0(X)superscript𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽0𝑋\mathsf{PrefExtend}^{0}(X)sansserif_PrefExtend start_POSTSUPERSCRIPT 0 end_POSTSUPERSCRIPT ( italic_X ) (i.e applying the operator zero times on X𝑋Xitalic_X) as X𝑋Xitalic_X even for an aperiodic prefix X𝑋Xitalic_X, on which the operator is not defined (a similar abuse is allowed for suffixes). Observe that an 𝖨𝖯𝖬𝖨𝖯𝖬\mathsf{IPM}sansserif_IPM data structure of Lemma 2.13 can be used to obtain 𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽\mathsf{PrefExtend}sansserif_PrefExtend and 𝖲𝗎𝖿𝖿𝖤𝗑𝗍𝖾𝗇𝖽𝖲𝗎𝖿𝖿𝖤𝗑𝗍𝖾𝗇𝖽\mathsf{SuffExtend}sansserif_SuffExtend in constant time.

Finally, we make the following observation which is a direct implication of Lemma 2.4. {observation} Let (X,Y)𝑋𝑌(X,Y)( italic_X , italic_Y ) be a highly periodic prefix-suffix 2222-cover of S𝑆Sitalic_S. There is a unique core prefix-suffix 2222-cover (X,Y)superscript𝑋superscript𝑌(X^{\prime},Y^{\prime})( italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT , italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT ) and two non-negative integers p,s𝑝𝑠p,sitalic_p , italic_s such that 𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽p(X)=Xsuperscript𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽𝑝superscript𝑋𝑋\mathsf{PrefExtend}^{p}(X^{\prime})=Xsansserif_PrefExtend start_POSTSUPERSCRIPT italic_p end_POSTSUPERSCRIPT ( italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT ) = italic_X and 𝖲𝗎𝖿𝖿𝖤𝗑𝗍𝖾𝗇𝖽s(Y)=Ysuperscript𝖲𝗎𝖿𝖿𝖤𝗑𝗍𝖾𝗇𝖽𝑠superscript𝑌𝑌\mathsf{SuffExtend}^{s}(Y^{\prime})=Ysansserif_SuffExtend start_POSTSUPERSCRIPT italic_s end_POSTSUPERSCRIPT ( italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT ) = italic_Y.

Furthermore, for every (s,p)[0..s]×[0..p](s^{\prime},p^{\prime})\in[0..s]\times[0..p]( italic_s start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT , italic_p start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT ) ∈ [ 0 . . italic_s ] × [ 0 . . italic_p ] the pair (𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽p(X),𝖲𝗎𝖿𝖿𝖤𝗑𝗍𝖾𝗇𝖽s(Y))superscript𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽superscript𝑝superscript𝑋superscript𝖲𝗎𝖿𝖿𝖤𝗑𝗍𝖾𝗇𝖽superscript𝑠superscript𝑌(\mathsf{PrefExtend}^{p^{\prime}}(X^{\prime}),\mathsf{SuffExtend}^{s^{\prime}}% (Y^{\prime}))( sansserif_PrefExtend start_POSTSUPERSCRIPT italic_p start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT end_POSTSUPERSCRIPT ( italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT ) , sansserif_SuffExtend start_POSTSUPERSCRIPT italic_s start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT end_POSTSUPERSCRIPT ( italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT ) ) is also a 2222-cover.

The uniqueness of (X,Y)superscript𝑋superscript𝑌(X^{\prime},Y^{\prime})( italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT , italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT ) arises from the fact that both operations preserve the period of the prefix/suffix when applied to a periodic prefix/suffix. Therefore, in order to obtain X𝑋Xitalic_X from some Xsuperscript𝑋X^{\prime}italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT, we either have to start from X=Xsuperscript𝑋𝑋X^{\prime}=Xitalic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT = italic_X and apply 𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽\mathsf{PrefExtend}sansserif_PrefExtend zero times if X𝑋Xitalic_X is aperiodic, or start with a short periodic prefix Xsuperscript𝑋X^{\prime}italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT with the same period as X𝑋Xitalic_X (which is unique). The same reasoning applies to Y𝑌Yitalic_Y.

The algorithm processes every non-highly periodic prefix-suffix 2222-cover (𝗉𝗋𝖾𝖿=S[1..p],𝗌𝗎𝖿𝖿=S[ns+1..n])(\mathsf{pref}=S[1..p],\mathsf{suff}=S[n-s+1..n])( sansserif_pref = italic_S [ 1 . . italic_p ] , sansserif_suff = italic_S [ italic_n - italic_s + 1 . . italic_n ] ) with the following goal: Report all prefix suffix 2222-covers (X,Y)𝑋𝑌(X,Y)( italic_X , italic_Y ) with length bounded by m𝑚mitalic_m such that X=𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽p(𝗉𝗋𝖾𝖿)𝑋superscript𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽𝑝𝗉𝗋𝖾𝖿X=\mathsf{PrefExtend}^{p}(\mathsf{pref})italic_X = sansserif_PrefExtend start_POSTSUPERSCRIPT italic_p end_POSTSUPERSCRIPT ( sansserif_pref ) and Y=𝖲𝗎𝖿𝖿𝖤𝗑𝗍𝖾𝗇𝖽s(𝗌𝗎𝖿𝖿)𝑌superscript𝖲𝗎𝖿𝖿𝖤𝗑𝗍𝖾𝗇𝖽𝑠𝗌𝗎𝖿𝖿Y=\mathsf{SuffExtend}^{s}(\mathsf{suff})italic_Y = sansserif_SuffExtend start_POSTSUPERSCRIPT italic_s end_POSTSUPERSCRIPT ( sansserif_suff ) for some naturals p𝑝pitalic_p and s𝑠sitalic_s. It follows directly from Section F.2 that all required prefix-suffix two covers are found by applying such process to every core pair.

We proceed to describe the algorithm for processing a core pair (𝗉𝗋𝖾𝖿,𝗌𝗎𝖿𝖿)𝗉𝗋𝖾𝖿𝗌𝗎𝖿𝖿(\mathsf{pref},\mathsf{suff})( sansserif_pref , sansserif_suff ).

  • If both 𝗉𝗋𝖾𝖿𝗉𝗋𝖾𝖿\mathsf{pref}sansserif_pref and 𝗌𝗎𝖿𝖿𝗌𝗎𝖿𝖿\mathsf{suff}sansserif_suff are aperiodic - the algorithm does not apply further processing to it.

  • If 𝗉𝗋𝖾𝖿𝗉𝗋𝖾𝖿\mathsf{pref}sansserif_pref is short ρ𝜌\rhoitalic_ρ-periodic and 𝗌𝗎𝖿𝖿𝗌𝗎𝖿𝖿\mathsf{suff}sansserif_suff is aperiodic, the algorithm initialize an iterator p=1𝑝1p=1italic_p = 1 initiates a loop. In every step of the loop, the algorithm sets Xp=𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽p(𝗉𝗋𝖾𝖿)subscript𝑋𝑝superscript𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽𝑝𝗉𝗋𝖾𝖿X_{p}=\mathsf{PrefExtend}^{p}(\mathsf{pref})italic_X start_POSTSUBSCRIPT italic_p end_POSTSUBSCRIPT = sansserif_PrefExtend start_POSTSUPERSCRIPT italic_p end_POSTSUPERSCRIPT ( sansserif_pref ) and checks if Xp𝖤𝗇𝖽subscript𝑋𝑝𝖤𝗇𝖽X_{p}\neq\mathsf{End}italic_X start_POSTSUBSCRIPT italic_p end_POSTSUBSCRIPT ≠ sansserif_End, |Xp|+|𝗌𝗎𝖿𝖿|msubscript𝑋𝑝𝗌𝗎𝖿𝖿𝑚|X_{p}|+|\mathsf{suff}|\leq m| italic_X start_POSTSUBSCRIPT italic_p end_POSTSUBSCRIPT | + | sansserif_suff | ≤ italic_m and (Xp,𝗌𝗎𝖿𝖿)subscript𝑋𝑝𝗌𝗎𝖿𝖿(X_{p},\mathsf{suff})( italic_X start_POSTSUBSCRIPT italic_p end_POSTSUBSCRIPT , sansserif_suff ) is a 2222-cover of S𝑆Sitalic_S. If all of the conditions are satisfied, the algorithm reports (Xp,𝗌𝗎𝖿𝖿)subscript𝑋𝑝𝗌𝗎𝖿𝖿(X_{p},\mathsf{suff})( italic_X start_POSTSUBSCRIPT italic_p end_POSTSUBSCRIPT , sansserif_suff ) as a 2222-cover, assigns pp+1𝑝𝑝1p\leftarrow p+1italic_p ← italic_p + 1 and repeats the loop. If one of the conditions is false - the loop is terminated.

  • If 𝗉𝗋𝖾𝖿𝗉𝗋𝖾𝖿\mathsf{pref}sansserif_pref is aperiodic and 𝗌𝗎𝖿𝖿𝗌𝗎𝖿𝖿\mathsf{suff}sansserif_suff is short ρ𝜌\rhoitalic_ρ-periodic the algorithm processes (𝗉𝗋𝖾𝖿,𝗌𝗎𝖿𝖿)𝗉𝗋𝖾𝖿𝗌𝗎𝖿𝖿(\mathsf{pref},\mathsf{suff})( sansserif_pref , sansserif_suff ) in a symmetric manner to the previous case, extending 𝗌𝗎𝖿𝖿𝗌𝗎𝖿𝖿\mathsf{suff}sansserif_suff using 𝖲𝗎𝖿𝖿𝖤𝗑𝗍𝖾𝗇𝖽𝖲𝗎𝖿𝖿𝖤𝗑𝗍𝖾𝗇𝖽\mathsf{SuffExtend}sansserif_SuffExtend.

  • If both 𝗉𝗋𝖾𝖿𝗉𝗋𝖾𝖿\mathsf{pref}sansserif_pref and 𝗌𝗎𝖿𝖿𝗌𝗎𝖿𝖿\mathsf{suff}sansserif_suff are short periodic, the algorithm applies the extensions of the previous two cases in a nested loop fashion as follows. The algorithm initiates two iterators p=0𝑝0p=0italic_p = 0 and s=0𝑠0s=0italic_s = 0. In every step of the loop, the algorithm sets Xp=𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽p(X)subscript𝑋𝑝superscript𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽𝑝𝑋X_{p}=\mathsf{PrefExtend}^{p}(X)italic_X start_POSTSUBSCRIPT italic_p end_POSTSUBSCRIPT = sansserif_PrefExtend start_POSTSUPERSCRIPT italic_p end_POSTSUPERSCRIPT ( italic_X ) and Yp=𝖲𝗎𝖿𝖿𝖤𝗑𝗍𝖾𝗇𝖽s(Y)subscript𝑌𝑝superscript𝖲𝗎𝖿𝖿𝖤𝗑𝗍𝖾𝗇𝖽𝑠𝑌Y_{p}=\mathsf{SuffExtend}^{s}(Y)italic_Y start_POSTSUBSCRIPT italic_p end_POSTSUBSCRIPT = sansserif_SuffExtend start_POSTSUPERSCRIPT italic_s end_POSTSUPERSCRIPT ( italic_Y ). The algorithm checks if Xp𝖤𝗇𝖽subscript𝑋𝑝𝖤𝗇𝖽X_{p}\neq\mathsf{End}italic_X start_POSTSUBSCRIPT italic_p end_POSTSUBSCRIPT ≠ sansserif_End, Yp𝖤𝗇𝖽subscript𝑌𝑝𝖤𝗇𝖽Y_{p}\neq\mathsf{End}italic_Y start_POSTSUBSCRIPT italic_p end_POSTSUBSCRIPT ≠ sansserif_End, |Xp|+|Yp|msubscript𝑋𝑝subscript𝑌𝑝𝑚|X_{p}|+|Y_{p}|\leq m| italic_X start_POSTSUBSCRIPT italic_p end_POSTSUBSCRIPT | + | italic_Y start_POSTSUBSCRIPT italic_p end_POSTSUBSCRIPT | ≤ italic_m and (Xp,Yp)subscript𝑋𝑝subscript𝑌𝑝(X_{p},Y_{p})( italic_X start_POSTSUBSCRIPT italic_p end_POSTSUBSCRIPT , italic_Y start_POSTSUBSCRIPT italic_p end_POSTSUBSCRIPT ) is a 2222-cover. If all conditions are satisfied - the algorithm reports (Xp,Yp)subscript𝑋𝑝subscript𝑌𝑝(X_{p},Y_{p})( italic_X start_POSTSUBSCRIPT italic_p end_POSTSUBSCRIPT , italic_Y start_POSTSUBSCRIPT italic_p end_POSTSUBSCRIPT ) as a 2222-cover, assigns pp+1𝑝𝑝1p\leftarrow p+1italic_p ← italic_p + 1, and repeats the loop. If one of the conditions fails, and p0𝑝0p\neq 0italic_p ≠ 0, the algorithm assigns p0𝑝0p\leftarrow 0italic_p ← 0 and ss+1𝑠𝑠1s\leftarrow s+1italic_s ← italic_s + 1, and repeats the loop. If one of the conditions fails and p=0𝑝0p=0italic_p = 0, the loop terminates.

Correctness.

The correctness of the algorithm arises naturally from Section F.2. Clearly, every pair (X,Y)𝑋𝑌(X,Y)( italic_X , italic_Y ) reported by the algorithm is a 2222-cover with length bounded by m𝑚mitalic_m, as these conditions are explicitly tested for every reported pair.

It remains to show that all required pairs are reported. Let (X,Y)𝑋𝑌(X,Y)( italic_X , italic_Y ) be a prefix suffix highly periodic 2222-cover with length bounded by m𝑚mitalic_m. Let (X,Y)superscript𝑋superscript𝑌(X^{\prime},Y^{\prime})( italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT , italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT ) be the unique core pair derived from Section F.2 and psuperscript𝑝p^{*}italic_p start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT and ssuperscript𝑠s^{*}italic_s start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT the integers such that X=𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽p(X)𝑋superscript𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽superscript𝑝superscript𝑋X=\mathsf{PrefExtend}^{p^{*}}(X^{\prime})italic_X = sansserif_PrefExtend start_POSTSUPERSCRIPT italic_p start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT end_POSTSUPERSCRIPT ( italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT ) and Y=𝖲𝗎𝖿𝖿𝖤𝗑𝗍𝖾𝗇𝖽s(Y)𝑌superscript𝖲𝗎𝖿𝖿𝖤𝗑𝗍𝖾𝗇𝖽superscript𝑠superscript𝑌Y=\mathsf{SuffExtend}^{s^{*}}(Y^{\prime})italic_Y = sansserif_SuffExtend start_POSTSUPERSCRIPT italic_s start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT end_POSTSUPERSCRIPT ( italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT ). From now on, we assume that both X𝑋Xitalic_X and Y𝑌Yitalic_Y are highly periodic. The analysis required for the case in which only one of X𝑋Xitalic_X and Y𝑌Yitalic_Y is highly periodic is derived from the analysis for the case in which both are highly periodic. In this case, both Xsuperscript𝑋X^{\prime}italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT and Ysuperscript𝑌Y^{\prime}italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT are short periodic with the same periods of X𝑋Xitalic_X and of Y𝑌Yitalic_Y, respectively. When the pair (X,Y)superscript𝑋superscript𝑌(X^{\prime},Y^{\prime})( italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT , italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT ) is processed by the algorithm, we claim that for every s<ssuperscript𝑠superscript𝑠s^{\prime}<s^{*}italic_s start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT < italic_s start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT the pair (X0,Ys)subscriptsuperscript𝑋0subscriptsuperscript𝑌superscript𝑠(X^{\prime}_{0},Y^{\prime}_{s^{\prime}})( italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT 0 end_POSTSUBSCRIPT , italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT italic_s start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT end_POSTSUBSCRIPT ) satisfies all conditions checked by the loop.

  1. 1.

    X0=X𝖤𝗇𝖽subscriptsuperscript𝑋0superscript𝑋𝖤𝗇𝖽X^{\prime}_{0}=X^{\prime}\neq\mathsf{End}italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT 0 end_POSTSUBSCRIPT = italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT ≠ sansserif_End.

  2. 2.

    Ys𝖤𝗇𝖽subscriptsuperscript𝑌superscript𝑠𝖤𝗇𝖽Y^{\prime}_{s^{\prime}}\neq\mathsf{End}italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT italic_s start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT end_POSTSUBSCRIPT ≠ sansserif_End since Ys𝖤𝗇𝖽subscriptsuperscript𝑌superscript𝑠𝖤𝗇𝖽Y^{\prime}_{s^{*}}\neq\mathsf{End}italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT italic_s start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT end_POSTSUBSCRIPT ≠ sansserif_End, and 𝖲𝗎𝖿𝖿𝖤𝗑𝗍𝖾𝗇𝖽(𝖤𝗇𝖽)𝖲𝗎𝖿𝖿𝖤𝗑𝗍𝖾𝗇𝖽𝖤𝗇𝖽\mathsf{SuffExtend}(\mathsf{End})sansserif_SuffExtend ( sansserif_End ) is undefined.

  3. 3.

    |X0|+|Ys|msubscriptsuperscript𝑋0subscriptsuperscript𝑌superscript𝑠𝑚|X^{\prime}_{0}|+|Y^{\prime}_{s^{\prime}}|\leq m| italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT 0 end_POSTSUBSCRIPT | + | italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT italic_s start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT end_POSTSUBSCRIPT | ≤ italic_m. Since for every s<ssuperscript𝑠superscript𝑠s^{\prime}<s^{*}italic_s start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT < italic_s start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT such that Ys𝖤𝗇𝖽subscriptsuperscript𝑌superscript𝑠𝖤𝗇𝖽Y^{\prime}_{s^{*}}\neq\mathsf{End}italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT italic_s start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT end_POSTSUBSCRIPT ≠ sansserif_End and Ys𝖤𝗇𝖽subscriptsuperscript𝑌superscript𝑠𝖤𝗇𝖽Y^{\prime}_{s^{\prime}}\neq\mathsf{End}italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT italic_s start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT end_POSTSUBSCRIPT ≠ sansserif_End we have |Ys|<|Ys|subscriptsuperscript𝑌superscript𝑠subscriptsuperscript𝑌superscript𝑠|Y^{\prime}_{s^{\prime}}|<|Y^{\prime}_{s^{*}}|| italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT italic_s start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT end_POSTSUBSCRIPT | < | italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT italic_s start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT end_POSTSUBSCRIPT |, and we have |X|+|Y|=|Xp|+|Ys|m𝑋𝑌subscriptsuperscript𝑋superscript𝑝subscriptsuperscript𝑌superscript𝑠𝑚|X|+|Y|=|X^{\prime}_{p^{*}}|+|Y^{\prime}_{s^{*}}|\leq m| italic_X | + | italic_Y | = | italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT italic_p start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT end_POSTSUBSCRIPT | + | italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT italic_s start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT end_POSTSUBSCRIPT | ≤ italic_m.

  4. 4.

    (X0,Ys)subscriptsuperscript𝑋0subscriptsuperscript𝑌superscript𝑠(X^{\prime}_{0},Y^{\prime}_{s^{\prime}})( italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT 0 end_POSTSUBSCRIPT , italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT italic_s start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT end_POSTSUBSCRIPT ) is a 2222-cover due to Section F.2.

It follows that the iterator s𝑠sitalic_s of the loop will reach the value ssuperscript𝑠s^{*}italic_s start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT. A similar analysis shows that when reaching ssuperscript𝑠s^{*}italic_s start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT and starting the process of increasing p𝑝pitalic_p, the p𝑝pitalic_p iterator of the loop would reach the value psuperscript𝑝p^{*}italic_p start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT. At this point, the pair (X,Y)=(Xp,Ys)𝑋𝑌subscriptsuperscript𝑋superscript𝑝subscriptsuperscript𝑌superscript𝑠(X,Y)=(X^{\prime}_{p^{*}},Y^{\prime}_{s^{*}})( italic_X , italic_Y ) = ( italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT italic_p start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT end_POSTSUBSCRIPT , italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT italic_s start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT end_POSTSUBSCRIPT ) is reported, as required.

Complexity.

We analyze the complexity of the nested loop executed in the case in which both X𝑋Xitalic_X and Y𝑌Yitalic_Y are short periodic. The complexity of the rest of the cases arises from the same arguments. When processing a pair (X,Y)𝑋𝑌(X,Y)( italic_X , italic_Y ), each step of every loop is dominated by the query to the oracle - Obtaining 𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽p(X)superscript𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽𝑝𝑋\mathsf{PrefExtend}^{p}(X)sansserif_PrefExtend start_POSTSUPERSCRIPT italic_p end_POSTSUPERSCRIPT ( italic_X ) or 𝖲𝗎𝖿𝖿𝖤𝗑𝗍𝖾𝗇𝖽s(Y)superscript𝖲𝗎𝖿𝖿𝖤𝗑𝗍𝖾𝗇𝖽𝑠𝑌\mathsf{SuffExtend}^{s}(Y)sansserif_SuffExtend start_POSTSUPERSCRIPT italic_s end_POSTSUPERSCRIPT ( italic_Y ) can be done in constant time using 𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽p1(X)superscript𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽𝑝1𝑋\mathsf{PrefExtend}^{p-1}(X)sansserif_PrefExtend start_POSTSUPERSCRIPT italic_p - 1 end_POSTSUPERSCRIPT ( italic_X ) of 𝖲𝗎𝖿𝖿𝖤𝗑𝗍𝖾𝗇𝖽s1(Y)superscript𝖲𝗎𝖿𝖿𝖤𝗑𝗍𝖾𝗇𝖽𝑠1𝑌\mathsf{SuffExtend}^{s-1}(Y)sansserif_SuffExtend start_POSTSUPERSCRIPT italic_s - 1 end_POSTSUPERSCRIPT ( italic_Y ) and the rest of the checks consist of integer comparisons. Each 2222-oracle query on a pair (Xp,Ys)subscript𝑋𝑝subscript𝑌𝑠(X_{p},Y_{s})( italic_X start_POSTSUBSCRIPT italic_p end_POSTSUBSCRIPT , italic_Y start_POSTSUBSCRIPT italic_s end_POSTSUBSCRIPT ) to the oracle that reports True can be charged on the reported pair (Xp,Ys)subscript𝑋𝑝subscript𝑌𝑠(X_{p},Y_{s})( italic_X start_POSTSUBSCRIPT italic_p end_POSTSUBSCRIPT , italic_Y start_POSTSUBSCRIPT italic_s end_POSTSUBSCRIPT ). Due to the uniqueness of the core pair (X,Y)𝑋𝑌(X,Y)( italic_X , italic_Y ) with respect to (Xp,Ys)subscript𝑋𝑝subscript𝑌𝑠(X_{p},Y_{s})( italic_X start_POSTSUBSCRIPT italic_p end_POSTSUBSCRIPT , italic_Y start_POSTSUBSCRIPT italic_s end_POSTSUBSCRIPT ), the total number of such queries is 𝗈𝗎𝗍𝗉𝗎𝗍𝗈𝗎𝗍𝗉𝗎𝗍\mathsf{output}sansserif_output. Each query on a pair (Xp,Ys)subscript𝑋𝑝subscript𝑌𝑠(X_{p},Y_{s})( italic_X start_POSTSUBSCRIPT italic_p end_POSTSUBSCRIPT , italic_Y start_POSTSUBSCRIPT italic_s end_POSTSUBSCRIPT ) that reports False is charged on the pair (X0,Ys)subscript𝑋0subscript𝑌𝑠(X_{0},Y_{s})( italic_X start_POSTSUBSCRIPT 0 end_POSTSUBSCRIPT , italic_Y start_POSTSUBSCRIPT italic_s end_POSTSUBSCRIPT ). If p>0𝑝0p>0italic_p > 0, it means that the pair (X0,Ys)subscript𝑋0subscript𝑌𝑠(X_{0},Y_{s})( italic_X start_POSTSUBSCRIPT 0 end_POSTSUBSCRIPT , italic_Y start_POSTSUBSCRIPT italic_s end_POSTSUBSCRIPT ) was reported as a 2222-cover. It follows that the total number of such queries on pairs (Xp,Ys)subscript𝑋𝑝subscript𝑌𝑠(X_{p},Y_{s})( italic_X start_POSTSUBSCRIPT italic_p end_POSTSUBSCRIPT , italic_Y start_POSTSUBSCRIPT italic_s end_POSTSUBSCRIPT ) with p>0𝑝0p>0italic_p > 0 is bounded by 𝗈𝗎𝗍𝗉𝗎𝗍𝗈𝗎𝗍𝗉𝗎𝗍\mathsf{output}sansserif_output across all core pairs. If p=0𝑝0p=0italic_p = 0, the loop terminates after the query. It follows that there is at most one such query for every processed core pair, and their total number across all pairs is bounded by 𝗈𝗎𝗍𝗉𝗎𝗍𝗈𝗎𝗍𝗉𝗎𝗍\mathsf{output}sansserif_output as well. It follows that the overall complexity of processing all pairs is bounded by O(𝗈𝗎𝗍𝗉𝗎𝗍log3)𝑂𝗈𝗎𝗍𝗉𝗎𝗍superscript3O(\mathsf{output}\cdot\log^{3})italic_O ( sansserif_output ⋅ roman_log start_POSTSUPERSCRIPT 3 end_POSTSUPERSCRIPT ) as required.

Border-Substring.

The algorithm proceeds to report all border-substring 2222-covers. The reporting is carried out following the same principles as in the prefix-suffix case. However, extending the periodic substrings requires a more careful treatment.

We define the following operator for extending substrings.

Definition F.10.

Let S𝑆Sitalic_S be a string and let 𝗌𝗎𝖻=S[i..j]\mathsf{sub}=S[i..j]sansserif_sub = italic_S [ italic_i . . italic_j ] be a ρ𝜌\rhoitalic_ρ-periodic prefix of S𝑆Sitalic_S. The Operator 𝖲𝗎𝖻𝖤𝗑𝗍𝖾𝗇𝖽(𝗌𝗎𝖻)𝖲𝗎𝖻𝖤𝗑𝗍𝖾𝗇𝖽𝗌𝗎𝖻\mathsf{SubExtend}(\mathsf{sub})sansserif_SubExtend ( sansserif_sub ) is defined as follows.

𝖲𝗎𝖻𝖤𝗑𝗍𝖾𝗇𝖽(𝗌𝗎𝖻)={S[i..j+ρ]if S[i..j+ρ] is ρ-periodicS[iρ..j]if S[iρ..j] is ρ-periodic and S[i..j+ρ] is not𝖤𝗇𝖽otherwise.\mathsf{SubExtend}(\mathsf{sub})=\begin{cases}S[i..j+\rho]&\textsf{if }S[i..j+% \rho]\textsf{ is }\rho\textsf{-periodic}\\ S[i-\rho..j]&\textsf{if }S[i-\rho..j]\textsf{ is }\rho\textsf{-periodic and }S% [i..j+\rho]\textsf{ is not}\\ \mathsf{End}&\text{otherwise.}\end{cases}sansserif_SubExtend ( sansserif_sub ) = { start_ROW start_CELL italic_S [ italic_i . . italic_j + italic_ρ ] end_CELL start_CELL if italic_S [ italic_i . . italic_j + italic_ρ ] is italic_ρ -periodic end_CELL end_ROW start_ROW start_CELL italic_S [ italic_i - italic_ρ . . italic_j ] end_CELL start_CELL if italic_S [ italic_i - italic_ρ . . italic_j ] is italic_ρ -periodic and italic_S [ italic_i . . italic_j + italic_ρ ] is not end_CELL end_ROW start_ROW start_CELL sansserif_End end_CELL start_CELL otherwise. end_CELL end_ROW

Again, we slightly abuse notation by allowing 𝖲𝗎𝖻𝖤𝗑𝗍𝖾𝗇𝖽0(X)=Xsuperscript𝖲𝗎𝖻𝖤𝗑𝗍𝖾𝗇𝖽0𝑋𝑋\mathsf{SubExtend}^{0}(X)=Xsansserif_SubExtend start_POSTSUPERSCRIPT 0 end_POSTSUPERSCRIPT ( italic_X ) = italic_X, even for non-periodic substrings X𝑋Xitalic_X. Note that unlike 𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽\mathsf{PrefExtend}sansserif_PrefExtend and 𝖲𝗎𝖿𝖿𝖤𝗑𝗍𝖾𝗇𝖽𝖲𝗎𝖿𝖿𝖤𝗑𝗍𝖾𝗇𝖽\mathsf{SuffExtend}sansserif_SuffExtend, the operator 𝖲𝗎𝖻𝖤𝗑𝗍𝖾𝗇𝖽𝖲𝗎𝖻𝖤𝗑𝗍𝖾𝗇𝖽\mathsf{SubExtend}sansserif_SubExtend is sensitive to the exact location of the substring 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub in S𝑆Sitalic_S. However, it is still easy to obtain 𝖲𝗎𝖻𝖤𝗑𝗍𝖾𝗇𝖽(𝗌𝗎𝖻)𝖲𝗎𝖻𝖤𝗑𝗍𝖾𝗇𝖽𝗌𝗎𝖻\mathsf{SubExtend}(\mathsf{sub})sansserif_SubExtend ( sansserif_sub ) from 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub using 𝖨𝖯𝖬Ssubscript𝖨𝖯𝖬𝑆\mathsf{IPM}_{S}sansserif_IPM start_POSTSUBSCRIPT italic_S end_POSTSUBSCRIPT in constant time.

The following observation parallels Section F.2 and arises due to the same arguments. {observation} Let (X,Y)𝑋𝑌(X,Y)( italic_X , italic_Y ) be a highly periodic border-substring 2222-cover of S𝑆Sitalic_S such that X𝑋Xitalic_X is not a 1111-cover. Let f𝑓fitalic_f be an index not covered by X𝑋Xitalic_X. There is a unique core border-substring 2222-cover (X,Y)superscript𝑋superscript𝑌(X^{\prime},Y^{\prime})( italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT , italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT ) and two non-negative integers b,s𝑏𝑠b,sitalic_b , italic_s such that 𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽b(X)=Xsuperscript𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽𝑏superscript𝑋𝑋\mathsf{PrefExtend}^{b}(X^{\prime})=Xsansserif_PrefExtend start_POSTSUPERSCRIPT italic_b end_POSTSUPERSCRIPT ( italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT ) = italic_X and 𝖲𝗎𝖻𝖤𝗑𝗍𝖾𝗇𝖽s(Y)=Ysuperscript𝖲𝗎𝖻𝖤𝗑𝗍𝖾𝗇𝖽𝑠superscript𝑌𝑌\mathsf{SubExtend}^{s}(Y^{\prime})=Ysansserif_SubExtend start_POSTSUPERSCRIPT italic_s end_POSTSUPERSCRIPT ( italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT ) = italic_Y. Where the 𝖲𝗎𝖻𝖤𝗑𝗍𝖾𝗇𝖽𝖲𝗎𝖻𝖤𝗑𝗍𝖾𝗇𝖽\mathsf{SubExtend}sansserif_SubExtend operations applied to Ysuperscript𝑌Y^{\prime}italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT are with respect to an occurrence of Ysuperscript𝑌Y^{\prime}italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT covering f𝑓fitalic_f.

Furthermore, for every (s,p)[0..s]×[0..p](s^{\prime},p^{\prime})\in[0..s]\times[0..p]( italic_s start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT , italic_p start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT ) ∈ [ 0 . . italic_s ] × [ 0 . . italic_p ] the pair (𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽p(X),𝖲𝗎𝖿𝖿𝖤𝗑𝗍𝖾𝗇𝖽s(Y))superscript𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽superscript𝑝superscript𝑋superscript𝖲𝗎𝖿𝖿𝖤𝗑𝗍𝖾𝗇𝖽superscript𝑠superscript𝑌(\mathsf{PrefExtend}^{p^{\prime}}(X^{\prime}),\mathsf{SuffExtend}^{s^{\prime}}% (Y^{\prime}))( sansserif_PrefExtend start_POSTSUPERSCRIPT italic_p start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT end_POSTSUPERSCRIPT ( italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT ) , sansserif_SuffExtend start_POSTSUPERSCRIPT italic_s start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT end_POSTSUPERSCRIPT ( italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT ) ) is also a 2222-cover.

Not that an occurrence of Ysuperscript𝑌Y^{\prime}italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT covering f𝑓fitalic_f must exist, as Y𝑌Yitalic_Y must cover f𝑓fitalic_f, and Ysuperscript𝑌Y^{\prime}italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT covers every index covered by Y𝑌Yitalic_Y by Lemma 2.4.

As an initial step, the algorithm uses the O(n)𝑂𝑛O(n)italic_O ( italic_n ) time algorithm of Smyth [28] to find all 1111-covers of S𝑆Sitalic_S. For every 1111-cover 𝖻𝗈𝗋𝖻𝗈𝗋\mathsf{bor}sansserif_bor of S𝑆Sitalic_S, all pairs (𝖻𝗈𝗋,𝗌𝗎𝖻)𝖻𝗈𝗋𝗌𝗎𝖻(\mathsf{bor},\mathsf{sub})( sansserif_bor , sansserif_sub ) are considered to be reported implicitly via 𝖻𝗈𝗋𝖻𝗈𝗋\mathsf{bor}sansserif_bor.

We now proceed to treat the non-trivial case. As in the prefix-suffix , the algorithm processes every core pair (X,Y)𝑋𝑌(X,Y)( italic_X , italic_Y ) and reports all pairs that arise from (X,Y)𝑋𝑌(X,Y)( italic_X , italic_Y ) via Section F.2.

  • If both 𝖻𝗈𝗋𝖻𝗈𝗋\mathsf{bor}sansserif_bor and 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub are aperiodic, the algorithm does not apply any further processing.

  • If 𝖻𝗈𝗋𝖻𝗈𝗋\mathsf{bor}sansserif_bor is short ρ𝜌\rhoitalic_ρ-periodic and 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub is aperiodic, the algorithm attempts to extend 𝖻𝗈𝗋𝖻𝗈𝗋\mathsf{bor}sansserif_bor similarly to the prefix extension process described in the prefix-suffix case with the following modification. As a preliminary step, the algorithm obtains p=𝖣𝗂𝖼𝗍q[b]superscript𝑝subscript𝖣𝗂𝖼𝗍𝑞delimited-[]𝑏p^{*}=\mathsf{Dict}_{q}[b]italic_p start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT = sansserif_Dict start_POSTSUBSCRIPT italic_q end_POSTSUBSCRIPT [ italic_b ] (see Section D.2). Let 𝖻𝗈𝗋=S[1..2ρ+db]𝖻𝗈𝗋𝑆delimited-[]1..2𝜌subscript𝑑𝑏\mathsf{bor}=S[1..2\rho+d_{b}]sansserif_bor = italic_S [ 1..2 italic_ρ + italic_d start_POSTSUBSCRIPT italic_b end_POSTSUBSCRIPT ] for db[0..ρ1]d_{b}\in[0..\rho-1]italic_d start_POSTSUBSCRIPT italic_b end_POSTSUBSCRIPT ∈ [ 0 . . italic_ρ - 1 ]. The algorithm starts the process of extending 𝖻𝗈𝗋𝖻𝗈𝗋\mathsf{bor}sansserif_bor from 𝖻𝗈𝗋=S[1..qρ+db]\mathsf{bor}^{\prime}=S[1..q^{*}\cdot\rho+d_{b}]sansserif_bor start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT = italic_S [ 1 . . italic_q start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT ⋅ italic_ρ + italic_d start_POSTSUBSCRIPT italic_b end_POSTSUBSCRIPT ] i.e. setting the initial value of p𝑝pitalic_p as qsuperscript𝑞q^{*}italic_q start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT. In essence, the algorithm ‘skips’ any extension of 𝖻𝗈𝗋𝖻𝗈𝗋\mathsf{bor}sansserif_bor that is a 1111-cover. All 2222-covers containing a 1111-cover are reported implicitly. If q=superscript𝑞q^{*}=\inftyitalic_q start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT = ∞, the algorithm does not apply any further processing to the pair (𝖻𝗈𝗋,𝗌𝗎𝖻)𝖻𝗈𝗋𝗌𝗎𝖻(\mathsf{bor},\mathsf{sub})( sansserif_bor , sansserif_sub ).

  • If 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub is short ρ𝜌\rhoitalic_ρ-periodic and 𝖻𝗈𝗋𝖻𝗈𝗋\mathsf{bor}sansserif_bor is aperiodic, the algorithm attempts to extend 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub as follows. Let f=fb𝑓subscript𝑓𝑏f=f_{b}italic_f = italic_f start_POSTSUBSCRIPT italic_b end_POSTSUBSCRIPT be an index not covered by S[1..b]S[1..b]italic_S [ 1 . . italic_b ] extracted from 𝖣𝗂𝖼𝗍𝖣𝗂𝖼𝗍\mathsf{Dict}sansserif_Dict (see Section D.2). If f=𝑓f=\inftyitalic_f = ∞, 𝖻𝗈𝗋𝖻𝗈𝗋\mathsf{bor}sansserif_bor is a 1111-cover and all pairs containing 𝖻𝗈𝗋𝖻𝗈𝗋\mathsf{bor}sansserif_bor are reported implicitly.

    Otherwise, f𝑓fitalic_f is an index not covered by 𝖻𝗈𝗋𝖻𝗈𝗋\mathsf{bor}sansserif_bor. The algorithm finds an occurrence of 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub covering f𝑓fitalic_f using the 𝖨𝖯𝖬Ssubscript𝖨𝖯𝖬𝑆\mathsf{IPM}_{S}sansserif_IPM start_POSTSUBSCRIPT italic_S end_POSTSUBSCRIPT data structure of Lemma 2.13, let 𝗌𝗎𝖻=S[f..f+r]\mathsf{sub}=S[f-\ell..f+r]sansserif_sub = italic_S [ italic_f - roman_ℓ . . italic_f + italic_r ] for some integers ,r𝑟\ell,rroman_ℓ , italic_r (note that such \ellroman_ℓ and r𝑟ritalic_r must exist since (𝖻𝗈𝗋,𝗌𝗎𝖻)𝖻𝗈𝗋𝗌𝗎𝖻(\mathsf{bor},\mathsf{sub})( sansserif_bor , sansserif_sub ) is a 2222-cover). The algorithm starts the extension process from Y1=S[f..f+r]Y_{1}=S[f-\ell..f+r]italic_Y start_POSTSUBSCRIPT 1 end_POSTSUBSCRIPT = italic_S [ italic_f - roman_ℓ . . italic_f + italic_r ]. It initializes an iterator b=1𝑏1b=1italic_b = 1, and the loop is carried as in the prefix suffix case, replacing 𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽\mathsf{PrefExtend}sansserif_PrefExtend with 𝖲𝗎𝖻𝖤𝗑𝗍𝖾𝗇𝖽𝖲𝗎𝖻𝖤𝗑𝗍𝖾𝗇𝖽\mathsf{SubExtend}sansserif_SubExtend.

  • If 𝖻𝗈𝗋𝖻𝗈𝗋\mathsf{bor}sansserif_bor is short ρbsubscript𝜌𝑏\rho_{b}italic_ρ start_POSTSUBSCRIPT italic_b end_POSTSUBSCRIPT-periodic and 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub is short ρssubscript𝜌𝑠\rho_{s}italic_ρ start_POSTSUBSCRIPT italic_s end_POSTSUBSCRIPT-periodic, the algorithm processes (𝖻𝗈𝗋,𝗌𝗎𝖻)𝖻𝗈𝗋𝗌𝗎𝖻(\mathsf{bor},\mathsf{sub})( sansserif_bor , sansserif_sub ) in a nested loop fashion as follows. We present this case in more detail, to provide a full picture of the previous cases as well. First, the algorithm finds the minimal value q𝑞qitalic_q such that Xq=𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽q(𝖻𝗈𝗋)subscript𝑋𝑞superscript𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽𝑞𝖻𝗈𝗋X_{q}=\mathsf{PrefExtend}^{q}(\mathsf{bor})italic_X start_POSTSUBSCRIPT italic_q end_POSTSUBSCRIPT = sansserif_PrefExtend start_POSTSUPERSCRIPT italic_q end_POSTSUPERSCRIPT ( sansserif_bor ) is not a 1111-cover. This is done using 𝖣𝗂𝖼𝗍qsubscript𝖣𝗂𝖼𝗍𝑞\mathsf{Dict}_{q}sansserif_Dict start_POSTSUBSCRIPT italic_q end_POSTSUBSCRIPT. If no such value exists - the algorithm halts the processing of the pair (𝖻𝗈𝗋,𝗌𝗎𝖻)𝖻𝗈𝗋𝗌𝗎𝖻(\mathsf{bor},\mathsf{sub})( sansserif_bor , sansserif_sub ). If this value exists, the algorithm sets an iterator bq𝑏𝑞b\leftarrow qitalic_b ← italic_q. Next, the algorithm uses 𝖣𝗂𝖼𝗍𝖣𝗂𝖼𝗍\mathsf{Dict}sansserif_Dict to find an index f𝑓fitalic_f not covered by Xqsubscript𝑋𝑞X_{q}italic_X start_POSTSUBSCRIPT italic_q end_POSTSUBSCRIPT and applies internal pattern matching (Lemma 2.13) on the text S[f|𝗌𝗎𝖻|..f+|𝗌𝗎𝖻|]S[f-|\mathsf{sub}|..f+|\mathsf{sub}|]italic_S [ italic_f - | sansserif_sub | . . italic_f + | sansserif_sub | ] to find an occurrence of 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub. If no such occurrence is found, the algorithm terminates the process of the pair (𝖻𝗈𝗋,𝗌𝗎𝖻)𝖻𝗈𝗋𝗌𝗎𝖻(\mathsf{bor},\mathsf{sub})( sansserif_bor , sansserif_sub ). Otherwise, the algorithm uses the occurrence to find two non-negative integers ,r𝑟\ell,rroman_ℓ , italic_r such that 𝗌𝗎𝖻=S[f..f+r]\mathsf{sub}=S[f-\ell..f+r]sansserif_sub = italic_S [ italic_f - roman_ℓ . . italic_f + italic_r ]. The algorithm then sets Y0=S[f..f+r]Y_{0}=S[f-\ell..f+r]italic_Y start_POSTSUBSCRIPT 0 end_POSTSUBSCRIPT = italic_S [ italic_f - roman_ℓ . . italic_f + italic_r ] and initializes a secondary iterator s=0𝑠0s=0italic_s = 0. Now, the algorithm starts running the loop. At every step of the loop, the algorithm sets Xb=𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽b(𝖻𝗈𝗋)subscript𝑋𝑏superscript𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽𝑏𝖻𝗈𝗋X_{b}=\mathsf{PrefExtend}^{b}(\mathsf{bor})italic_X start_POSTSUBSCRIPT italic_b end_POSTSUBSCRIPT = sansserif_PrefExtend start_POSTSUPERSCRIPT italic_b end_POSTSUPERSCRIPT ( sansserif_bor ) and Ys=𝖲𝗎𝖻𝖤𝗑𝗍𝖾𝗇𝖽s(𝗌𝗎𝖻)subscript𝑌𝑠superscript𝖲𝗎𝖻𝖤𝗑𝗍𝖾𝗇𝖽𝑠𝗌𝗎𝖻Y_{s}=\mathsf{SubExtend}^{s}(\mathsf{sub})italic_Y start_POSTSUBSCRIPT italic_s end_POSTSUBSCRIPT = sansserif_SubExtend start_POSTSUPERSCRIPT italic_s end_POSTSUPERSCRIPT ( sansserif_sub ). The algorithm then checks if Ys𝖤𝗇𝖽subscript𝑌𝑠𝖤𝗇𝖽Y_{s}\neq\mathsf{End}italic_Y start_POSTSUBSCRIPT italic_s end_POSTSUBSCRIPT ≠ sansserif_End, Xb𝖤𝗇𝖽subscript𝑋𝑏𝖤𝗇𝖽X_{b}\neq\mathsf{End}italic_X start_POSTSUBSCRIPT italic_b end_POSTSUBSCRIPT ≠ sansserif_End, Xbsubscript𝑋𝑏X_{b}italic_X start_POSTSUBSCRIPT italic_b end_POSTSUBSCRIPT is a border, |Xb|+|Ys|msubscript𝑋𝑏subscript𝑌𝑠𝑚|X_{b}|+|Y_{s}|\leq m| italic_X start_POSTSUBSCRIPT italic_b end_POSTSUBSCRIPT | + | italic_Y start_POSTSUBSCRIPT italic_s end_POSTSUBSCRIPT | ≤ italic_m, and if (Xb,Ys)subscript𝑋𝑏subscript𝑌𝑠(X_{b},Y_{s})( italic_X start_POSTSUBSCRIPT italic_b end_POSTSUBSCRIPT , italic_Y start_POSTSUBSCRIPT italic_s end_POSTSUBSCRIPT ) is a 2222-cover. If all the conditions are true, the algorithm reports the pair (Xb,Ys)subscript𝑋𝑏subscript𝑌𝑠(X_{b},Y_{s})( italic_X start_POSTSUBSCRIPT italic_b end_POSTSUBSCRIPT , italic_Y start_POSTSUBSCRIPT italic_s end_POSTSUBSCRIPT ), assigns ss+1𝑠𝑠1s\leftarrow s+1italic_s ← italic_s + 1, and repeats the loop. If one of the conditions is False, and s0𝑠0s\neq 0italic_s ≠ 0, the algorithm sets s0𝑠0s\leftarrow 0italic_s ← 0 and bb+1𝑏𝑏1b\leftarrow b+1italic_b ← italic_b + 1 and repeats the loop. If one of the conditions is False and s=0𝑠0s=0italic_s = 0, the loop terminates.

Correctness and complexity follow due to similar arguments as in the prefix-suffix case.

Correctness.

The correctness of the algorithm arises naturally from Section F.2. Clearly, every pair (Xb,Ys)subscript𝑋𝑏subscript𝑌𝑠(X_{b},Y_{s})( italic_X start_POSTSUBSCRIPT italic_b end_POSTSUBSCRIPT , italic_Y start_POSTSUBSCRIPT italic_s end_POSTSUBSCRIPT ) reported by the algorithm is a 2222-cover with length bounded by m𝑚mitalic_m, as these conditions are explicitly tested for every reported pair.

It remains to show that all required pairs are reported. Let (X,Y)𝑋𝑌(X,Y)( italic_X , italic_Y ) be a prefix suffix highly periodic 2222-cover with length bounded by m𝑚mitalic_m. If X𝑋Xitalic_X is a 1111-cover, the pair (X,Y)𝑋𝑌(X,Y)( italic_X , italic_Y ) reported implicitly via X𝑋Xitalic_X. For the rest of the analysis we assume that X𝑋Xitalic_X is not a 1111-cover. Therefore, there is a unique (X,Y)superscript𝑋superscript𝑌(X^{\prime},Y^{\prime})( italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT , italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT ) core pair derived from Section F.2 and bsuperscript𝑏b^{*}italic_b start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT and ssuperscript𝑠s^{*}italic_s start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT the integers such that X=𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽b(X)𝑋superscript𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽superscript𝑏superscript𝑋X=\mathsf{PrefExtend}^{b^{*}}(X^{\prime})italic_X = sansserif_PrefExtend start_POSTSUPERSCRIPT italic_b start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT end_POSTSUPERSCRIPT ( italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT ) and Y=𝖲𝗎𝖿𝖿𝖤𝗑𝗍𝖾𝗇𝖽s(Y)𝑌superscript𝖲𝗎𝖿𝖿𝖤𝗑𝗍𝖾𝗇𝖽superscript𝑠superscript𝑌Y=\mathsf{SuffExtend}^{s^{*}}(Y^{\prime})italic_Y = sansserif_SuffExtend start_POSTSUPERSCRIPT italic_s start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT end_POSTSUPERSCRIPT ( italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT ). From now on, we assume that both X𝑋Xitalic_X and Y𝑌Yitalic_Y are highly periodic. The analysis required for the case in which only one of X𝑋Xitalic_X and Y𝑌Yitalic_Y is highly periodic is derived from the analysis for the case in which both are highly periodic. In this case, both Xsuperscript𝑋X^{\prime}italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT and Ysuperscript𝑌Y^{\prime}italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT are short periodic with the same periods of X𝑋Xitalic_X and of Y𝑌Yitalic_Y, respectively. When the pair (X,Y)superscript𝑋superscript𝑌(X^{\prime},Y^{\prime})( italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT , italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT ) is processed by the algorithm the initial value of the iterator b𝑏bitalic_b is set to q𝑞qitalic_q the minimal integer such that 𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽q(𝖻𝗈𝗋)superscript𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽𝑞𝖻𝗈𝗋\mathsf{PrefExtend}^{q}(\mathsf{bor})sansserif_PrefExtend start_POSTSUPERSCRIPT italic_q end_POSTSUPERSCRIPT ( sansserif_bor ) is not a 1111-cover. By definition, qb𝑞superscript𝑏q\leq b^{*}italic_q ≤ italic_b start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT. The algorithm then retrieves an index f𝑓fitalic_f not covered by Xqsubscript𝑋𝑞X_{q}italic_X start_POSTSUBSCRIPT italic_q end_POSTSUBSCRIPT from 𝖣𝗂𝖼𝗍𝖣𝗂𝖼𝗍\mathsf{Dict}sansserif_Dict. Note that f𝑓fitalic_f is also not covered by X𝑋Xitalic_X due to Lemma 2.4. There must be an occurrence of 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub covering the index f𝑓fitalic_f, since there is an occurrence of Y𝑌Yitalic_Y covering f𝑓fitalic_f, and 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub is obtainable from Y𝑌Yitalic_Y by removing 𝗉𝖾𝗋(Y)𝗉𝖾𝗋𝑌\mathsf{per}(Y)sansserif_per ( italic_Y ) a certain number of times (Lemma 2.4 then implies that 𝗌𝗎𝖻𝗌𝗎𝖻\mathsf{sub}sansserif_sub covers f𝑓fitalic_f). It follows from the above discussion that the loop will be initialized successfully.

We claim that for every b[q..b1]b^{\prime}\in[q..b^{*}-1]italic_b start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT ∈ [ italic_q . . italic_b start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT - 1 ] the pair (Xb,Y0)subscriptsuperscript𝑋superscript𝑏subscriptsuperscript𝑌0(X^{\prime}_{b^{\prime}},Y^{\prime}_{0})( italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT italic_b start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT end_POSTSUBSCRIPT , italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT 0 end_POSTSUBSCRIPT ) satisfies all conditions checked by the loop.

  1. 1.

    Y0=Y𝖤𝗇𝖽subscriptsuperscript𝑌0superscript𝑌𝖤𝗇𝖽Y^{\prime}_{0}=Y^{\prime}\neq\mathsf{End}italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT 0 end_POSTSUBSCRIPT = italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT ≠ sansserif_End.

  2. 2.

    Xb𝖤𝗇𝖽subscriptsuperscript𝑋superscript𝑏𝖤𝗇𝖽X^{\prime}_{b^{\prime}}\neq\mathsf{End}italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT italic_b start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT end_POSTSUBSCRIPT ≠ sansserif_End since Xb𝖤𝗇𝖽subscriptsuperscript𝑋superscript𝑏𝖤𝗇𝖽X^{\prime}_{b^{*}}\neq\mathsf{End}italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT italic_b start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT end_POSTSUBSCRIPT ≠ sansserif_End, and 𝖲𝗎𝖿𝖿𝖤𝗑𝗍𝖾𝗇𝖽(𝖤𝗇𝖽)𝖲𝗎𝖿𝖿𝖤𝗑𝗍𝖾𝗇𝖽𝖤𝗇𝖽\mathsf{SuffExtend}(\mathsf{End})sansserif_SuffExtend ( sansserif_End ) is undefined.

  3. 3.

    Xbsubscriptsuperscript𝑋superscript𝑏X^{\prime}_{b^{\prime}}italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT italic_b start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT end_POSTSUBSCRIPT is a border since Xb=Xsubscriptsuperscript𝑋superscript𝑏𝑋X^{\prime}_{b^{*}}=Xitalic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT italic_b start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT end_POSTSUBSCRIPT = italic_X is a border and Xbsubscriptsuperscript𝑋superscript𝑏X^{\prime}_{b^{\prime}}italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT italic_b start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT end_POSTSUBSCRIPT can be obtained from X𝑋Xitalic_X by removing a suffix of length ρ=𝗉𝖾𝗋(X)𝜌𝗉𝖾𝗋𝑋\rho=\mathsf{per}(X)italic_ρ = sansserif_per ( italic_X ) a certain number of times (while remaining with a string with length at least ρ𝜌\rhoitalic_ρ). Due to Lemma 2.4, in every such step we are left with a string covering the previous one. In particular, a cover of a border must be a border.

  4. 4.

    |Xb|+|Y0|msubscriptsuperscript𝑋𝑏subscriptsuperscript𝑌0𝑚|X^{\prime}_{b}|+|Y^{\prime}_{0}|\leq m| italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT italic_b end_POSTSUBSCRIPT | + | italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT 0 end_POSTSUBSCRIPT | ≤ italic_m. Since for every b<bsuperscript𝑏superscript𝑏b^{\prime}<b^{*}italic_b start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT < italic_b start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT such that Xb𝖤𝗇𝖽subscriptsuperscript𝑋superscript𝑏𝖤𝗇𝖽X^{\prime}_{b^{*}}\neq\mathsf{End}italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT italic_b start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT end_POSTSUBSCRIPT ≠ sansserif_End and Xb𝖤𝗇𝖽subscriptsuperscript𝑋superscript𝑏𝖤𝗇𝖽X^{\prime}_{b^{\prime}}\neq\mathsf{End}italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT italic_b start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT end_POSTSUBSCRIPT ≠ sansserif_End we have |Xb|<|Xb|subscriptsuperscript𝑋superscript𝑏subscriptsuperscript𝑋superscript𝑏|X^{\prime}_{b^{\prime}}|<|X^{\prime}_{b^{*}}|| italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT italic_b start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT end_POSTSUBSCRIPT | < | italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT italic_b start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT end_POSTSUBSCRIPT |, and we have |X|+|Y|=|Xb|+|Ys|m𝑋𝑌subscriptsuperscript𝑋superscript𝑏subscriptsuperscript𝑌superscript𝑠𝑚|X|+|Y|=|X^{\prime}_{b^{*}}|+|Y^{\prime}_{s^{*}}|\leq m| italic_X | + | italic_Y | = | italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT italic_b start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT end_POSTSUBSCRIPT | + | italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT italic_s start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT end_POSTSUBSCRIPT | ≤ italic_m.

  5. 5.

    (X0,Ys)subscriptsuperscript𝑋0subscriptsuperscript𝑌superscript𝑠(X^{\prime}_{0},Y^{\prime}_{s^{\prime}})( italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT 0 end_POSTSUBSCRIPT , italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT italic_s start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT end_POSTSUBSCRIPT ) is a 2222-cover due to Section F.2.

It follows that the iterator b𝑏bitalic_b of the loop will reach the value bsuperscript𝑏b^{*}italic_b start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT. A similar analysis shows that when reaching bsuperscript𝑏b^{*}italic_b start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT and starting the process of increasing s𝑠sitalic_s, the s𝑠sitalic_s iterator of the loop would reach the value ssuperscript𝑠s^{*}italic_s start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT. At this point, the pair (X,Y)=(Xp,Ys)𝑋𝑌subscriptsuperscript𝑋superscript𝑝subscriptsuperscript𝑌superscript𝑠(X,Y)=(X^{\prime}_{p^{*}},Y^{\prime}_{s^{*}})( italic_X , italic_Y ) = ( italic_X start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT italic_p start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT end_POSTSUBSCRIPT , italic_Y start_POSTSUPERSCRIPT ′ end_POSTSUPERSCRIPT start_POSTSUBSCRIPT italic_s start_POSTSUPERSCRIPT ∗ end_POSTSUPERSCRIPT end_POSTSUBSCRIPT ) is reported, as required.

Complexity.

We analyze the complexity of the nested loop executed in the case in which both X𝑋Xitalic_X and Y𝑌Yitalic_Y are short periodic. The complexity of the rest of the cases arises from the same arguments. When processing a pair (X,Y)𝑋𝑌(X,Y)( italic_X , italic_Y ), each step of every loop is dominated by the query to the oracle - Obtaining 𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽b(X)superscript𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽𝑏𝑋\mathsf{PrefExtend}^{b}(X)sansserif_PrefExtend start_POSTSUPERSCRIPT italic_b end_POSTSUPERSCRIPT ( italic_X ) or 𝖲𝗎𝖻𝖤𝗑𝗍𝖾𝗇𝖽s(Y)superscript𝖲𝗎𝖻𝖤𝗑𝗍𝖾𝗇𝖽𝑠𝑌\mathsf{SubExtend}^{s}(Y)sansserif_SubExtend start_POSTSUPERSCRIPT italic_s end_POSTSUPERSCRIPT ( italic_Y ) can be done in constant time using 𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽b1(X)superscript𝖯𝗋𝖾𝖿𝖤𝗑𝗍𝖾𝗇𝖽𝑏1𝑋\mathsf{PrefExtend}^{b-1}(X)sansserif_PrefExtend start_POSTSUPERSCRIPT italic_b - 1 end_POSTSUPERSCRIPT ( italic_X ) or 𝖲𝗎𝖻𝖤𝗑𝗍𝖾𝗇𝖽s1(Y)superscript𝖲𝗎𝖻𝖤𝗑𝗍𝖾𝗇𝖽𝑠1𝑌\mathsf{SubExtend}^{s-1}(Y)sansserif_SubExtend start_POSTSUPERSCRIPT italic_s - 1 end_POSTSUPERSCRIPT ( italic_Y ) and the rest of the checks consist of integer comparisons. There is also an additional cost for initializing the loop - extracting q𝑞qitalic_q and f𝑓fitalic_f from 𝖣𝗂𝖼𝗍qsubscript𝖣𝗂𝖼𝗍𝑞\mathsf{Dict}_{q}sansserif_Dict start_POSTSUBSCRIPT italic_q end_POSTSUBSCRIPT and from 𝖣𝗂𝖼𝗍𝖣𝗂𝖼𝗍\mathsf{Dict}sansserif_Dict, and applying internal pattern matching. Each of these operations is carried out in constant time. Each 2222-oracle query on a pair (Xb,Ys)subscript𝑋𝑏subscript𝑌𝑠(X_{b},Y_{s})( italic_X start_POSTSUBSCRIPT italic_b end_POSTSUBSCRIPT , italic_Y start_POSTSUBSCRIPT italic_s end_POSTSUBSCRIPT ) to the oracle that reports True can be charged on the reported pair (Xb,Ys)subscript𝑋𝑏subscript𝑌𝑠(X_{b},Y_{s})( italic_X start_POSTSUBSCRIPT italic_b end_POSTSUBSCRIPT , italic_Y start_POSTSUBSCRIPT italic_s end_POSTSUBSCRIPT ). Due to the uniqueness of the core pair (X,Y)𝑋𝑌(X,Y)( italic_X , italic_Y ) with respect to (Xb,Ys)subscript𝑋𝑏subscript𝑌𝑠(X_{b},Y_{s})( italic_X start_POSTSUBSCRIPT italic_b end_POSTSUBSCRIPT , italic_Y start_POSTSUBSCRIPT italic_s end_POSTSUBSCRIPT ), the total number of such queries is 𝗈𝗎𝗍𝗉𝗎𝗍𝗈𝗎𝗍𝗉𝗎𝗍\mathsf{output}sansserif_output. Each query on a pair (Xb,Ys)subscript𝑋𝑏subscript𝑌𝑠(X_{b},Y_{s})( italic_X start_POSTSUBSCRIPT italic_b end_POSTSUBSCRIPT , italic_Y start_POSTSUBSCRIPT italic_s end_POSTSUBSCRIPT ) that reports False is charged on the pair (Xb,Y0)subscript𝑋𝑏subscript𝑌0(X_{b},Y_{0})( italic_X start_POSTSUBSCRIPT italic_b end_POSTSUBSCRIPT , italic_Y start_POSTSUBSCRIPT 0 end_POSTSUBSCRIPT ). If s>0𝑠0s>0italic_s > 0, it means that the pair (Xb,Y0)subscript𝑋𝑏subscript𝑌0(X_{b},Y_{0})( italic_X start_POSTSUBSCRIPT italic_b end_POSTSUBSCRIPT , italic_Y start_POSTSUBSCRIPT 0 end_POSTSUBSCRIPT ) was reported as a 2222-cover. It follows that the total number of such queries on pairs (Xb,Ys)subscript𝑋𝑏subscript𝑌𝑠(X_{b},Y_{s})( italic_X start_POSTSUBSCRIPT italic_b end_POSTSUBSCRIPT , italic_Y start_POSTSUBSCRIPT italic_s end_POSTSUBSCRIPT ) with s>0𝑠0s>0italic_s > 0 is bounded by 𝗈𝗎𝗍𝗉𝗎𝗍𝗈𝗎𝗍𝗉𝗎𝗍\mathsf{output}sansserif_output across all core pairs. If s=0𝑠0s=0italic_s = 0, the loop terminates after the query. It follows that there is at most one such query for every processed core pair, and their total number across all pairs is bounded by 𝗈𝗎𝗍𝗉𝗎𝗍𝗈𝗎𝗍𝗉𝗎𝗍\mathsf{output}sansserif_output as well. It follows that the overall complexity of processing all pairs is bounded by O(𝗈𝗎𝗍𝗉𝗎𝗍log3)𝑂𝗈𝗎𝗍𝗉𝗎𝗍superscript3O(\mathsf{output}\cdot\log^{3})italic_O ( sansserif_output ⋅ roman_log start_POSTSUPERSCRIPT 3 end_POSTSUPERSCRIPT ) as required.

In conclusion, the algorithm reports all prefix-suffix 2222-covers and border substring 2222-covers with the required length bound. The overall running time, including the preprocessing step dominated by constructing the 2222-cover oracle, is O(nlog5n+𝗈𝗎𝗍𝗉𝗎𝗍log3n)𝑂𝑛superscript5𝑛𝗈𝗎𝗍𝗉𝗎𝗍superscript3𝑛O(n\log^{5}n+\mathsf{output}\cdot\log^{3}n)italic_O ( italic_n roman_log start_POSTSUPERSCRIPT 5 end_POSTSUPERSCRIPT italic_n + sansserif_output ⋅ roman_log start_POSTSUPERSCRIPT 3 end_POSTSUPERSCRIPT italic_n ), as required.