Dynamic and Internal Longest Common Substring

Given two strings S and T, each of length at most n, the longest common substring (LCS) problem is to find a longest substring common to S and T. This is a classical problem in computer science with an O(n)\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$\mathcal {O}(n)$$\end{document}-time solution. In the fully dynamic setting, edit operations are allowed in either of the two strings, and the problem is to find an LCS after each edit. We present the first solution to the fully dynamic LCS problem requiring sublinear time in n per edit operation. In particular, we show how to find an LCS after each edit operation in O~(n2/3)\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$\tilde{\mathcal {O}}(n^{2/3})$$\end{document} time, after O~(n)\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$\tilde{\mathcal {O}}(n)$$\end{document}-time and space preprocessing. This line of research has been recently initiated in a somewhat restricted dynamic variant by Amir et al. [SPIRE 2017]. More specifically, the authors presented an O~(n)\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$\tilde{\mathcal {O}}(n)$$\end{document}-sized data structure that returns an LCS of the two strings after a single edit operation (that is reverted afterwards) in O~(1)\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$\tilde{\mathcal {O}}(1)$$\end{document} time. At CPM 2018, three papers (Abedin et al., Funakoshi et al., and Urabe et al.) studied analogously restricted dynamic variants of problems on strings; specifically, computing the longest palindrome and the Lyndon factorization of a string after a single edit operation. We develop dynamic sublinear-time algorithms for both of these problems as well. We also consider internal LCS queries, that is, queries in which we are to return an LCS of a pair of substrings of S and T. We show that answering such queries is hard in general and propose efficient data structures for several restricted cases.


Introduction
Given two strings S and T, each of length at most n, the longest common substring (LCS) problem, also known as the longest common factor problem, is to find a longest substring common to S and T. This is a classical problem in theoretical computer science. Knuth had conjectured that the LCS problem is in (n log n) . In 1973 Weiner solved it in the optimal O(n) time [78] designing a data structure that was later called the suffix tree (see also [41]). Knuth declared Weiner's algorithm the "Algorithm of the Year" [16]. Since O(n) time is optimal for this problem, a series of studies have been dedicated in improving the working space [63,73]. The LCS problem has also been studied under Hamming and edit distance. We refer the interested reader to [2,17,28,72,75,76] and references therein.
In [72], Starikovskaya mentions that an answer to the LCS problem "is not robust and can vary significantly when the input strings are changed even by one character", implicitly posing the following question: Can we compute an LCS after editing S or T in o(n) time?

Example 1
The length of an LCS of S and T below is doubled when substitution S [4] ∶= is performed. The next substitution, T [3] ∶= , halves the length of an LCS.
Amir et al. [11] introduced a restricted dynamic variant, where any single edit operation is allowed and is reverted afterwards. We call this problem LCS after One edit. They presented an Õ (n)-sized data structure that can be constructed in Õ (n) time supporting Õ (1)-time computation of an LCS, after one edit operation is applied on S. Abedin et al. [3] improved the complexities of this data structure by log O(1) n factors. Two other restricted variants of the dynamic LCS problem were considered by Amir and Boneh in [8]. In both variants substitutions were allowed in one of the strings; one was of decremental nature and in the other one the complexity was parameterized by the period of the static string.
This work initiated a new line of research on analogously restricted dynamic variants of problems on strings. A string is called palindrome if it the same as its reverse. A string is called Lyndon if it is smaller lexicographically than all its suffixes [64]. Computing a longest palindrome and a longest Lyndon substring of a string after a single edit have been recently studied in [48] (see also [49]) and in [77], respectively.
In this paper we make substantial progress: we show a strongly sublinear-time solution for the general version of the LCS problem, namely, the fully dynamic case of the LCS problem. Given two strings S and T, the problem is to answer the following type of queries in an on-line manner: perform an edit operation (substitution, insertion, or deletion) on S or on T and then return an LCS of the new S and T. We call this problem fuLLy dynamiC LCS. We also develop fully dynamic sublineartime algorithms for computing a longest palindrome and for maintaining the Lyndon factorization of a string.
Below we mention some known results on dynamic and internal problems on strings.
Dynamic Pattern Matching Finding all occ occurrences of a pattern of length m in a static text can be performed in the optimal O(m + occ) time using suffix trees, which can be constructed in linear time [41,78]. In the fully dynamic setting, the problem is to compute the new set of occurrences when allowing for edit operations anywhere on the text. A considerable amount of work has been carried out on this problem [42,43,53]. The first data structure with polylogarithmic update time and time-optimal queries was shown by Sahinalp and Vishkin [70]. The update time was later improved by Alstrup et al. [7] at the expense of slightly suboptimal query time. The state of the art is the data structure by Gawrychowski et al. [51] supporting time-optimal queries with O(log 2 n) time for updates. Clifford et al. [35] have recently shown upper and lower bounds for variants of exact matching with wildcard characters, inner product, and Hamming distance.
Dynamic String Collection with Comparison The problem is to maintain a dynamic collection of strings of total length n supporting the following operations: adding a string to , adding the concatenation of two strings from to , splitting a string from and adding the two residual strings in , and returning the length of the longest common prefix of two strings from . This line of research was initiated by Sundar and Tarjan [74]. Data structures supporting updates in polylogarithmic time were presented by Mehlhorn et al. [67] and Alstrup et al. [7]. Finally, Gawrychowski et al. [51] proposed an optimal solution with O(log n)-time updates, where n is the total length of all strings in the collection. Charalampopoulos et al. [33] recently presented efficient algorithms for approximate pattern matching, under both the Hamming and edit distances, over such a collection of strings, maintained dynamically as in [51].
Dynamic Maintenance of Repetitions Squares are strings of the form XX. In [10], the authors show how to maintain squares in a dynamic string S of length n in n o (1) time per operation. A modification of this algorithm, with the same time complexity per operation, allows them to determine in Õ (1) time whether a queried substring of S is periodic, and if so, compute its period.
Dynamic String Alignment A dynamic version of the string alignment problem, which is a generalization of the well-known longest common subsequence problem, was recently studied in [32] (see also [55] for a practical algorithm). The authors showed that when the alignment weights are integers bounded in absolute value by some w = n O (1) , an optimal string alignment can be maintained in Õ (n ⋅ min{ √ n, w}) time per update. This is conditionally optimal on SETH for constant w-up to polylogarithmic factors-due to the lower bounds on computing such an alignment in the static case [1,18,25].

3
Internal Pattern Matching In the so-called internal model-the name was coined by Kociumaka et al. in [62]-one is to answer queries about substrings of a given text. Problems that have been studied in this model include pattern matching, longest common prefix, periodicity, minimal lexicographic rotation and dictionary matching; see [61] for an overview and [30,31,60,62]. We explore internal queries relevant to the considered dynamic problems and in several cases we use them as building blocks in our dynamic algorithms.
Our Results and Techniques We make the following contributions: 1. A study of internal LCS queries: hardness of the general case conditional on the hardness of set disjointness and efficient data structures for useful restricted cases, based on ingredients such as the suffix tree, heavy-path decomposition and orthogonal range queries (see Sect. 3). 2. An efficient data structure for a natural generalization of the LCS after One edit, where we allow one edit in each of the strings. This solution relies on internal LCS queries and string periodicity (see Sect. 4). 3. The first fully dynamic algorithm for the LCS problem that works in strongly sublinear time per edit operation in any of the two strings. Specifically, for two strings, each of length up to n, it computes an LCS after each edit operation in Õ (n 2∕3 ) time after Õ (n)-time and space preprocessing. We employ small difference covers in order to decompose our problem in the cases of the LCS being short or long and treat each case separately (see Sect. 5). 1 We show that a simple modification to our algorithm can maintain a longest repeat of a string S of length n in the same complexities (see Sect. 6). 4. A general scheme for dynamic problems on strings. This scheme relies on having efficient data structures for the (static) internal counterparts (see Sect. 7). We show two following applications of this scheme. 5. A fully dynamic algorithm for computing a longest palindrome substring of a string S requiring Õ ( √ n) time per edit. 2 We use the facts that the set of maximal palindrome substrings of a string has a linear size and that the lengths of suffix palindromes of a string can be represented as a logarithmic number of arithmetic progressions (see Sect. 8). 6. A fully dynamic algorithm, requiring Õ ( √ n) time per edit, for computing a longest Lyndon substring of string S as well as maintaining a representation of the Lyndon factorization of S that allows us to efficiently extract the t-th element of the factorization in Õ (1) time. The authors of [77] presented algorithms for computing a representation of a Lyndon factorization of a prefix of a string and of a suffix of a string in Õ (1) time after Õ (n) preprocessing. We carefully combine these two representations to obtain general internal computation of a representation of a Lyndon factorization in the same time bounds (see Sect. 9).

3
Algorithmica (2020) 82:3707-3743 A preliminary version of this paper appeared in [12]. Here, in particular, we greatly simplify the algorithm for the fully dynamic LCS problem, we provide a more detailed study of internal LCS queries and we include sections and proofs missing from the preliminary version due to space constraints. . j] is denoted by S (j) and a suffix S[i . . n] is denoted by S (i) . A substring of S is called proper if it is shorter than S. We denote the reverse string of S by S R = S[n]S[n − 1] … S [1] . By ST, S k , and S ∞ we denote the concatenation of strings S and T, k copies of string S, and infinitely many copies of string S, respectively. If a string B is both a proper prefix and a proper suffix of string S, then B is called a border of S.

Preliminaries
String S has a period p if and only if it has a border of length n − p . We refer to the smallest period as the period of the string and, analogously, to the longest border as the border of the string.
Suffix Tree The suffix tree T(S) of a string S of length n is a compact trie representing all suffixes of S. The branching nodes of the trie as well as the terminal nodes, that correspond to suffixes of S, become explicit nodes of the suffix tree, while the other nodes are implicit. Each edge of the suffix tree can be viewed as an upward maximal path of implicit nodes starting with an explicit node. Moreover, each node belongs to a unique path of that kind. Thus, each node of the trie can be represented in the suffix tree by the edge it belongs to and an index within the corresponding path. We let L(v) denote the path-label of a node v, i.e., the concatenation of the edge labels along the path from the root to v. We say that v is path-labelled L(v) . Additionally, D(v) = |L(v)| is used to denote the string-depth of node v. A terminal node v such that L(v) = S (i) for some 1 ≤ i ≤ n is also labelled with index i. Each substring of S is uniquely represented by either an explicit or an implicit node of T(S) , called its locus. Once T(S) is constructed, it can be traversed in a depth-first manner to compute the string-depth D(v) for each explicit node v. The suffix tree of a string of length n, over an integer alphabet, can be computed in time and space O(n) [41]. Each explicit node of the suffix tree is able to access its (explicit) parent. In the case of integer alphabets, in order to access the child of an explicit node by the first character of its edge label in O(1) time, perfect hashing [47] can be used.
By (S, T) we denote the longest common prefix of S and T, by (S, T) we denote | (S, T)| , and by lcp(r, s) we denote (S (r) , S (s) ) . An O(n)-sized lowest common ancestor data structure can be constructed over the suffix tree of S in O(n) time [22], supporting lcp(r, s)-queries in O(1) time. A symmetric construction on S R (the reverse of S) can answer the so-called longest common suffix (textsflcs) 1 3 queries in the same complexity. The lcp and textsflcs queries are also known as longest common extension (LCE) queries.
Suffix Array The suffix array of a string S of length n, denoted by (S) , is an integer array of size n + 1 storing the starting positions of all (lexicographically) sorted suffixes of S, i.e. for all Note that we explicitly add the empty suffix to the array. The suffix array (S) corresponds to a pre-order traversal of all terminal nodes of the suffix tree T(S) . We define a generalized suffix array of S 1 , … , S k , denoted

Internal LCS Queries
In this section we consider LCS queries in the internal model. In the most general LCS queries, we are given strings S and T and upon query we are to report an LCS between a substring of S and a substring of T. We show a conditional lower bound for data structures for such queries in the next subsection. We then explore restricted versions of internal LCS queries and design efficient solutions for them. The developed data structures come handy in Sect. 4.

A Lower Bound Based on Set Disjointness
In the Set Disjointness problem, we are given a collection of m sets S 1 , S 2 , … , S m of total size N from some universe U for preprocessing in order to answer queries on the emptiness of the intersection of some two query sets from the collection. Goldstein et al. [52] demonstrated conditional hardness of Set Disjointness with regard to its space-query time tradeoff. Specifically, Goldstein et al. state the following conjecture.
Conjecture 1 (Strong Set Disjointness Conjecture) Any data structure for the Set Disjointness problem that answers queries in t time must use space ̃( N 2 ∕t 2 ). 3 Conjecture 1 is a generalization of the Set Disjointness conjecture stating that any data structure for the Set Disjointness problem with constant query time must use ̃( N 2 ) space [36,69]. Unconditional lower bounds for the space-time tradeoff of the Set Disjointness were proven by Dietz et al. [40] and Afshani and Nielsen [4] for specific models of computation. Specifically, the results of [4] imply that Conjecture 1 is true in the pointer machine model. Theorem 1 Any data structure answering internal LCS queries for two strings, each of length at most n, in t time must use ̃( n 2 ∕t 2 ) space, unless the Strong Set Disjointness Conjecture is false.
Proof We reduce the Set Disjointness problem to that of answering internal LCS queries as follows. Given sets S 1 , … , S m of total cardinality N, we construct a string T of length n = N that consists of the concatenation of the elements of the sets, so that each set S i corresponds to the substring T[a i . . The proof of Theorem 1 mimics the proof of Amir et al. [14] for the hardness of so-called two-range-LCP queries. In the two-range-LCP problem, one is to preprocess a string so that queries of the following type can be answered: given two ranges I and J, return max i∈I,j∈J (i, j) . Amir et al. [14] presented a data structure achieving the tradeoff stated in the above lower bound. Now note that a general internal LCS query can be reduced via binary search to O(log n) two-range-LCP queries as follows. The length of an LCS between S[a 1 . . Proposition 1 Given two strings of total length n and a parameter t ≤ √ n , there is an Õ (n 2 ∕t 2 )-size data structure that answers internal LCS queries in Õ (t) time.

Auxiliary Data Structures Over the Suffix Tree
We first recall some auxiliary data structures.
Orthogonal Range Maximum Queries Let P be a collection of n points in a D-dimensional grid with integer weights and coordinates of magnitude O(n) . In a D-dimensional orthogonal range maximum query , we are to report the maximum weight of a point from P in the rectangle. We assume that the point that attains this maximum is also computed. The following result is known. Data Structures for Trees We say that T is a weighted tree if it is a rooted tree with integer weights on nodes, denoted by D(v) , such that the weight of the root is zero and D(u) < D(v) if u is the parent of v. We say that a node v is a weighted ancestor of a node u at depth if v is the highest ancestor of u with weight of at least .

Lemma 2 ( [13])
After O(n)-time preprocessing, weighted ancestor queries for nodes of a weighted tree T of size n can be answered in O(log log n) time per query.
The following corollary applies Lemma 2 to the suffix tree. If T is a rooted tree, for each non-leaf node u of T the heavy edge (u, v) is an edge for which the subtree rooted at v has the maximal number of leaves (in case of several such subtrees, we fix one of them). A heavy path is a maximal path of heavy edges. The path from a node v in T to the root is composed of prefix fragments of heavy paths interleaved by single non-heavy (compact) edges. Here a prefix fragment of a path is a path connecting the topmost node of with any of its nodes. We denote this decomposition by H(v, T) . The following observation is known. In the heaviest induced ancestors (HIA) problem, introduced by Gagie et al. [50], we are given two weighted trees T 1 and T 2 with the same set of n leaves, numbered 1 through n, and are asked queries of the following form: given a node v 1 of T 1 and a node v 2 of T 2 , return an ancestor u 1 of v 1 and an ancestor u 2 of v 2 that have a leaf descendant with the same label, say i (we say that the ancestors u 1 and u 2 are induced by the leaf i), and maximum total weight. We also consider special HIA queries in which we are to find the heaviest ancestor of v 3−j that is induced with v j , for a specified j ∈ {1, 2} . Gagie et al. [50] provide several trade-offs for the space and query time of a data structure for answering HIA queries, some of which were recently improved by Abedin et al. [3]. All of them are based on heavy-path decompositions H(v 1 , T 1 ) , H(v 2 , T 2 ) . In the following lemma we use the variant of the data structure from Section 2.2 in [50], substituting the data structure used from [27] to the one from [6] to obtain a trade-off with a specified construction time. It can be readily verified that their technique answers special HIA queries within the same complexity.

Lemma 4 ( [50]
) HIA queries and special HIA queries over two weighted trees T 1 and T 2 of total size O(n) can be answered in O(log 2 n) time, using a data structure of size O(n log 2 n) that can be constructed in O(n log 2 n) time.
Amir et al. [11] observed that the problem of computing an LCS after a single edit operation at position i can be decomposed into two queries out of which we choose the one with the maximal answer: an occurrence of an LCS either avoids i or it covers i. The former case can be precomputed. The latter can be reduced to HIA queries over suffix trees. It can be formalized by our three SubStringS LCS problem.
In Sect. 3.4 we show a solution to the general version of the three SubStringS LCS problem using a generalization of the HIA queries. First, in Sect. 3.3, we show efficient data structures for answering simpler types of internal LCS queries.

Internal Queries for Special Substrings
We show how to answer internal LCS queries for a prefix or suffix of S and a prefix or suffix of T and for a substring of S and the whole T.
In the solutions we use the formula: We also apply the following observation to create range maximum queries data structures over points constructed from explicit nodes of the GST T(S, T).

Lemma 6 Let S and T be two strings of length at most n. After O(n log 2 n)-time and O(n log n)-space preprocessing, an LCS between any prefix or suffix of S and prefix or suffix of T can be computed in O(log n) time.
Proof For a node v of T(S, T) and U ∈ {S, T} we define: We assume that min � = ∞ and max � = −∞ . These values can be computed for all explicit nodes of T(S, T) in O(n) time in a bottom-up traversal of the tree. We only consider computing (S (a) , T (b) ) and (S (a) , T (b) ) as the remaining cases can be solved by considering the reversed strings.
In the first case formula (1) has an especially simple form: which lets us use orthogonal range maximum queries to evaluate it. For each explicit node v of T(S, T) with descendants from both S and T we create a point (x, y) with weight D(v) , where x = maxPref (v, S) and y = maxPref (v, T) . By Observation 1, the sought LCS length is the maximum weight of a point in the rectangle [a, n] × [b, n] . This lets us also recover the LCS itself. The complexity follows from Lemma 1.
In the second case the formula (1) becomes: The result is computed in one of two steps depending on which of the two terms produces the minimum. First let us consider the case that ( In the opposite case we can assume that the resulting internal LCS is a suffix of S (a) that does not occur earlier in S.
In both cases we use the 2d data structure of Lemma 1. In the end, we return max(r 1 , r 2 ) and the corresponding LCS. ◻ The following lemma provides an efficient solution for the other special case of internal LCS.

Lemma 7 Let S and T be two strings of length at most n. After O(n)-time preprocessing, one can compute an LCS between T and any substring of S in
The following fact was shown in [11]. Here we give a proof for completeness. The formula (1) can be written as: .
This lets us restate the previous formula as follows: The final formula for LCS length can be evaluated in O(1) time with a data structure for range maximum queries that can be constructed in linear time [22] on B [1], … , B[n] , provided that k 0 is known. This lets us also recover the LCS itself.
Computation of k 0 The condition for k 0 can be stated equivalently as We can do this in time O(log n) by performing a binary search for k in the range [a, b] of B ′ using O(n)-time preprocessing and O(1)-time range maximum queries [22]. ◻

Three Substrings LCS Queries
We show how to answer the general three SubStringS LCS queries. A solution to a special case of three SubStringS LCS queries with W = T was already implicitly presented by Amir et al. in [11]. It is based on the heaviest induced ancestors (HIA) problem on trees applied to the suffix tree of T. We generalize the HIA queries and use them to answer general three SubStringS LCS queries. The data structure for answering our generalization of HIA queries turns out to be quite technical. It relies on the construction of multidimensional grids for pairs of heavy paths (in heavy-path decompositions [71]) of the involved trees. Each query can be answered by interpreting the answer of O(log 2 n) orthogonal range maximum queries over such grids.
We use extended HIA queries that we define for two weighted trees T 1 and T 2 with the same set of n leaves, numbered 1 through n, as follows: given 1 ≤ a ≤ b ≤ |T| , a node v 1 of T 1 , and a node v 2 of T 2 , return an ancestor u 1 of v 1 and an ancestor u 2 of v 2 such that: We also consider special extended HIA queries, in which the condition u 1 = v 1 or the condition u 2 = v 2 is imposed. Both extended and special extended HIA queries can be answered efficiently using multidimensional range maximum queries. The motivation of the above definitions (extended and special extended HIA queries) becomes clearer in the proof of Lemma 9. Intuitively, the additional hardness is due to the fact that we use this type of queries to answer three SubStringS LCS for an arbitrary substring W = T[a . . b] instead of W = T . To this end, we have extended the HIA queries and present a data structure to answer them efficiently. The proposed data structure is based on a non-trivial combination of heavy-path decompositions and multidimensional range maximum data structures.

Lemma 8
Extended HIA queries and special extended HIA queries over two weighted trees T 1 and T 2 of total size O(n) can be answered in Õ (1) time after Õ (n) -time and space preprocessing.
Proof We defer answering special extended HIA queries until the end of the proof. Let us consider heavy paths in T 1 and T 2 . Let us assign to each heavy path in T j , for j = 1, 2 , a unique integer identifier of magnitude O(n) denoted by id( ) . For a heavy path and i ∈ {1, … , n} , by d( , i) we denote the depth of the lowest node of that has leaf i in its subtree.
We will create four collections of weighted points P I , P II , P III , P IV in 6d. Let i ∈ {1, … , n} . There are at most log n + 1 heavy paths on the path from leaf number i to the root of each of T 1 and T 2 . For each such pair of heavy paths, 1 in T 1 and 2 in T 2 , we denote d( j , i) , for j = 1, 2 , by d j and insert the point: Thus each collection contains O(n log 2 n) points. We perform preprocessing for range maximum queries on each of the collections by applying Lemma 1.
Assume that we are given an extended HIA query for v 1 , v 2 , a, and b (inspect Fig. 1 for an illustration). We consider all the prefix fragments of heavy paths in For each of the O(log 2 n) pairs of prefix fragments ′ 1 and ′ 2 in the decompositions of the root-to-v 1 and root-to-v 2 paths, respectively, we ask four range maximum queries, to obtain the following values: ) that corresponds to finding a pair of induced nodes ) that corresponds to finding a pair of induced nodes u 1 ∈ � 1 ⧵ {y 1 } and u 2 = y 2 ; 4. D(y 1 ) + D(y 2 ) + If an RMQ concerns an empty set of points, it is assumed to return −∞ . We return the point that yielded the maximal value.
Hence, an extended HIA query reduces to O(log 2 n) range maximum queries in collections of points in 6d of size O(n log 2 n) . A special extended HIA query can be answered in a simpler way by asking just O(log n) RMQs, where one of the prefix fragments ′ 1 , ′ 2 reduces always to a single node. The statement follows. ◻ The proof of the following lemma is very similar to the proof of Lemma 5; we simply use extended HIA queries instead of the regular ones.

Lemma 9
Let T be a string of length at most n. After Õ (n)-time preprocessing, we can answer three SubStringS LCS queries in Õ (1) time.
Proof We construct T 1 = T(T R ) , T 2 = T(T) , and the data structure for computing the loci of substrings (Corollary 1). The leaf corresponding to prefix T (i−1) in T 1 and to suffix T (i) in T 2 are labeled with i. Let W = T[a . . b] be an occurrence of W in T. If we treat T 1 and T 2 as weighted trees over the set of explicit nodes, then we have: Fig. 1 Illustration of the notations used to handle a pair of prefix fragments ′ 1 and ′ 2 . The descendant leaves of x j are implicitly partitioned at query time, by employing intervals I j and I ∞ j , according to whether their deepest ancestor in j is a strict ancestor of y j or not. For example, the pair of red nodes, induced by i, will be considered by the RMQ of type 1, while the pair of blue nodes, induced by i ′ , by the RMQ of type 2, assuming that the last two constraints of the respective are satisfied (Color figure online)

3
Claim Let u 1 and u 2 be explicit nodes of T 1 and T 2 , respectively, and . This implies that u 1 and u 2 are induced by i and that: (⇐) If u 1 and u 2 are induced by i, then L(u 1 ) R occurs as a suffix of T (i−1) and L(u 2 ) occurs as a prefix of T (i) . By the inequalities a ≤ i − d 1

LCS After One Substitution Per String
Let us now consider an extended version of the LCS after One edit problem, for simplicity restricted to substitutions.
To solve this problem we consider three cases depending on whether an occurrence of the LCS contains any of the changed positions in S and T. We prove the following result.

LCS Contains No Changed Position
It suffices to apply internal LCS queries of Lemma 6 four times: each time for one of S (i−1) , S (i+1) and one of T (j−1) , T (j+1) .

LCS Contains a Changed Position in Exactly One of the Strings
We use the following lemma that encapsulates one of the main techniques of [11]. It involves computing so-called ranges of substrings in the generalized suffix array of S and T and it relies on a result by Fischer et al. [45].
Let S be a string of length n. Given a substring U of S, we denote by range S (U) the range in the (S) that represents the suffixes of S that have U as a prefix. Every node u in the suffix tree T(S) corresponds to an range range S (L(u)). Proof Let X be a string of length n. We can precompute range X (L(u)) for all explicit nodes u in T(X) in O(n) time while performing a depth-first traversal of the tree. We use the following result by Fischer et al. [45].

Lemma 10
Claim ( [45]) Let P and Q be two substrings of X and assume that (X) is known.
Given range X (P) and range X (Q) , range X (PQ) can be computed in time O(log log n) after O(n log log n)-time and O(n)-space preprocessing.
We use the data structure of the claim for the generalized suffix array (S, T) . The range of a substring P is denoted as range S,T (P) . We assume that each element of (S As for the second part, it suffices to apply binary search over P to find the longest prefix P ′ of P that is a substring of T. If P � = P , we apply binary search to find the longest prefix Q ′ of Q such that PQ ′ is a substring of T. Binary searches result in additional log n-factor in the query complexity. The approach for computing the longest suffix is analogous. ◻ We now show how to compute the longest substring that contains the position i in S, but not the position j in T (the opposite case is symmetric). We first use Lemma 10(b) to compute two substrings, U and V, of T in O(log n log log n) time:

3
• U is the longest substring of T that is equal to a suffix of S[1 . . i − 1]; • V is the longest substring of T that is equal to a prefix of S[i + 1 . . |S|].
Our task then reduces to computing the longest substring of UV that crosses the boundary between U and V and is a substring of T (j−1) or of T (j+1) . We can compute it using two three SubStringS LCS queries: one with W = T (j−1) and one with W = T (j+1) , for which we rely on Lemma 5.

LCS Contains a Changed Position in Each of the Strings
A Prefix-Suffix Query gets as input two substrings X and Y of a string S of length n and an integer d and returns the lengths of all prefixes of X of length between d and 2d that are suffixes of Y. It is known that such a query returns an arithmetic sequence and if it has at least three elements, then its difference equals the period of all the corresponding prefixes-suffixes. Moreover, Kociumaka et al. [62] show that Prefix-Suffix Queries can be answered in O(1) time using a data structure of O(n) size, which can be constructed in O(n) time. By considering X = Y = U , this implies the two respective points of the lemma below. We next show an algorithm that finds a longest string S � [i . . i r ] = T � [j . . j r ] such that i ≤ i ≤ i r and j ≤ j ≤ j r for the given indices i, j. Let us assume that i − i ≤ j − j ; the symmetric case can be treated analogously. We have that U so both these cases need to be checked.) Note that these substrings do not contain any changed position. Any such U is a prefix of S (i+1) and a suffix of T (j−1) ; let U 0 denote the longest such string. Then, the possible candidates for U are U 0 and all its borders. For a border U of U 0 , we say that is an LCS aligned at U. We compute U 0 in time O(log n) by asking Prefix-Suffix Queries for X = S (i+1) , Y = T (j−1) in S#T and d = 2 r for all r = 0, 1, … , ⌊log j⌋ . We then consider the borders of U 0 in arithmetic sequences of their lengths; see Lemma 11. If an arithmetic sequence has at most two elements, we compute an LCS aligned at each of the borders in O(1) time by the above formula using LCE queries. Otherwise, let p be the difference of the arithmetic sequence, be its length, and u be its maximum element. Further let: The setting is presented in Fig. 3. It can be readily verified (inspect Fig. 3) that a longest common substring aligned at the border of length u − wp , for w ∈ [0, − 1] , is equal to Thus, a longest LCS aligned at a border whose length is in this arithmetic sequence is max −1 w=0 g(w) . The following observation facilitates efficient evaluation of this formula. By Observation 2, g(w) can be expressed as a piecewise linear function with O(1) pieces. Moreover, its exact formula can be computed using O(1) LCE queries on

Fully Dynamic LCS
Before we proceed to describe a solution to this problem we discuss how to answer LCE queries efficiently in a dynamic string. We resort to the main result of Gawrychowski et al. [51] for maintaining a string collection under the following operations.
• makestring(W): insert a non-empty string W; • concat(W 1 , W 2 ) : insert W 1 W 2 to , for W 1 , W 2 ∈ ; • split(W, i): split the string W at position i and insert both resulting strings to , for W ∈ ; • lcp (W 1 , W 2 ) : return the length of the longest common prefix of W 1 and W 2 , for W 1 , W 2 ∈ . Note that our results that make use of Lemma 12 hold w.h.p.

Remark 1
A lemma similar to the above, based on a simple application of Karp-Rabin fingerprints [58], and avoiding the use of the heavy machinery underlying Lemma 12, can be proved.
Let us now proceed to the fully dynamic LCS problem. We first consider the case where the length of the sought LCS is bounded by some d; we call this problem d -bOunded-Length LCS.

Lemma 14 d -bOunded-Length LCS can be solved in O(d log 2 n) time per operation after Õ (n)-time preprocessing, using Õ (n + kd) space for k performed operations.
Proof Let and be the multisets of d-length substrings and the d − 1 suffixes of length smaller than d of S and T, respectively. We will maintain balanced binary search trees (balanced BSTs) B , with respect to the lexicographical order, containing the elements of , for = , , stored as substrings. We can search in these balanced BSTs in O(log 2 n) time since a comparison in it is an lcp query, which requires O(log n) time by Lemma 13, possibly followed by a character comparison (which can be performed in O(log n) time using the data structure of [51]). Each node of B will maintain a counter denoting its multiplicity in . Let = ∪ ; we do not use in the algorithm, we just introduce it for conceptual convenience.

Observation 3 The length of the LCS of length at most d is equal to the maximum between pairs of consecutive substrings in (the sorted) that originate from different strings.
During preprocessing, we compute the 's of all pairs described in Observation 3 and store them in a max heap H. To each element of the heap, we store a pointer from the nodes u ∈ B , v ∈ B it originates from.
Each edit in S or T yields O(d) deletions and O(d) insertions of substrings in each of , and . We first perform deletions and then insertions. For each such operation, we have to check if it destroys or creates a pair of consecutive elements in (the sorted) , originating from different strings. We observe that upon the insertion/deletion of a string P, only pairs involving P, pred (P) , pred (P) , succ (P) and succ (P) may be involved, where pred, succ are predecessor and successor with respect to the lexicographical order. These elements can be identified in O(log 2 n) time. The max heap can then be updated using a constant number of LCE queries and heap updates. By Lemma 13, LCE queries (and heap updates) require O(log n) time each. Finally, we return the maximum element of the heap.
◻ We now focus on the case that the sought LCS is of length at least d. We say that a set (d) of positive integers is a d-cover if there is a constanttime computable function h such that for any positive integers i, j we have

Lemma 15 ( [26, 65]) For a positive integer d there is a d-cover
The intuition behind applying the d-cover in our setting is as follows (inspect also Fig. 4). Consider a position i on S and a position j on T. Note that i, j ∈ [1, n] . By the d-cover construction, we have that h(i, j) is within distance d and i + h(i, j), j + h(i, j) ∈ (d) . Thus if we want to find a longest common substring of length at least d, it suffices to compute longest common extensions to the left and to the right of only positions i � , j � ∈ (d) (black circles in Fig. 4) and then merge these partial results accordingly.
For this we use the following auxiliary problem that was introduced in [28].

Lemma 16 ( [28]) twO String famiLieS LCP can be solved in
Theorem 3 fuLLy dynamiC LCS on two strings, each of length up to n, can be solved in Õ (n 2∕3 ) time per edit operation w.h.p., using Õ (n) space, after Õ (n)-time preprocessing.
Proof Let us consider an integer d ∈ [1, n] . For lengths up to d, we use the algorithm for the d-bOunded-Length LCS problem of Lemma 14. If this problem indicates that there is a solution of length at least d, we proceed to the second step. Let Lemma 15). We consider the following families of pairs of strings: We define as the family of strings that occur in the pairs from and . Then maxPairLCP( , ) equals the length of the sought LCS, provided that it is at least d.
Note that | |, | |, | | are O(n∕ √ d) . A compact trie T( ) can be constructed in Õ (| | log | |) time by sorting all the strings (using -queries) and then adding them to the trie in lexicographic order; see [37]. We then use the solution to twO String famiLieS LCP which takes Õ (n∕ √ d) time. We set d = ⌊n 2∕3 ⌋ , rebuild the data structure of Lemma 13 that allows for efficient LCE queries after every k = ⌊n 1∕3 ⌋ edit operations and apply a standard timeslicing deamortization technique to obtain the stated complexities. 4 ◻

Fully Dynamic Longest Repeat
In the longest repeat problem we are given as input a string S of length n and we are to report a longest substring that occurs at least twice in S. This can be done in O(n) time and space [78]. In the fully dynamic longest repeat problem we are given as input a string S, which we are to maintain under subsequent edit operations, so that after each operation we can efficiently return a longest repeat in S. The application of our techniques for the LCS problem is quite straightforward, which is not surprising given the connection between the two problems. In what follows we briefly discuss the modifications required to the algorithms presented for the two subcases which we have decomposed the LCS problem into; we decompose the longest repeat problem in an analogous manner. Lemma 14 covers the case that the LCS is short. It provides an efficient way to maintain all substrings of S and T of a specified length sorted lexicographically and maintaining the maximum value among the lcps of all pairs of consecutive substrings in this sorted list, originating from different strings. Here we can simply maintain this information for one string.
Lemma 16 finds long enough substrings that are common in both strings and are hence guaranteed to be anchored in a pair of positions in the difference cover.  3, 5, 8, 9, 11, 14, 15, 17, 20} , with the elements marked as black circles. For example, we may have h(4, 11) = 4 since 4 + 4, 11 + 4 ∈ 20 (6) Let A be this difference cover. Recall that we construct a compact trie T( ) of a family of strings , defined in terms of the input strings, the difference cover and the updated positions. Then, given T( ) and sets , ⊆ 2 we efficiently compute max{ (P 1 , Q 1 ) + (P 2 , Q 2 ) ∶ (P 1 , P 2 ) ∈ and (Q 1 , Q 2 ) ∈ } by using an efficient solution to the twO String famiLieS LCP problem. Sets and essentially allow us to distinguish between substrings of S ′ and T ′ in . We adapt this for the longest repeat problem with an extra log n factor in the complexities. We build O(log n) copies of the respective tree T( ) , where in the j-th copy, 0 ≤ j ≤ log n , we set: where m are integer. Note that since and are disjoint, in none of the tries do we align a fragment of S with itself. It is also easy to see that any two positions in A will be in different sets at one of the copies of T( ).

Theorem 4 A longest repeat of a string of length up to n can be maintained
in Õ (n 2∕3 ) time w.h.p. per edit operation, using Õ (n) space, after Õ (n) -time preprocessing.

General Scheme for Dynamic Problems on Strings
We now present a general scheme for dynamic problems on strings. Let the input be a string S of length n. We construct a data structure that answers the following type of queries: given k edit operations on S, compute the answer to a particular problem on the resulting string S ′ . Assuming that the data structure occupies O(s n ) space, answers queries for k edits in time O(q n (k)) and can be constructed in time O(t n ) ( s n ≥ n and q n (k) ≥ k is non-decreasing with respect to k), this data structure can be used to design a dynamic algorithm that preprocesses the input string in time O(t n ) and answers queries dynamically under edit operations in amortized time O(q n ( )) , where is such that q n ( ) = (t n + n)∕ , using O(s n ) space. The query time can be made worst-case using time slicing: for s n , t n =Õ(n) and q n (k) =Õ(k) we obtain a fully dynamic algorithm with Õ ( √ n)-time queries, whereas for q n (k) =Õ(k 2 ) the query time is Õ (n 2∕3 ).
A k-substring of a string S is a concatenation of k strings, each of which is either a substring of S (possibly empty) or a single character. A k-substring of S can be represented in O(k) additional space using a doubly-linked list if the string S itself is stored. The string S after k subsequent edit operations can be represented as a (2k + 1)-substring due to the following lemma.

Lemma 17
Let S ′ be a k-substring of S and S ′′ be S ′ after a single edit operation. Then S ′′ is a (k + 2)-substring of S. Moreover, S ′′ can be computed from S ′ in O(k) time.
Proof Let S � = F 1 … F k where each F i is either a substring of S or a single character. We traverse the list of substrings until we find the substring F i such that the edit operation takes place at the j-th character of F i . As a result, F i is decomposed into a prefix and a suffix, potentially with a single character inserted in between in case of insertion or substitution. The resulting string S ′′ is a (k + 2)-substring of S. ◻ Thus the fully dynamic version reduces to designing a data structure over a string S of length n that computes the result of a specific problem on a k-substring F 1 … F k of S. For problems in which we aim at computing the longest substring of S that satisfies a certain property there are two cases.
• Case 1: the sought substring occurs inside one of the substrings F i . This requires us to compute the solution to a certain internal pattern matching problem. • Case 2: it contains the boundary between some two substrings F i and F i+1 . We call Case 2 cross-substring queries. Note that certain internal queries may arise in crosssubstring queries as well.
Finally, let us formalize the time slicing deamortization technique.

Lemma 18
Assume that there is a data structure D over an input string of length n that occupies O(s n ) space, answers queries for k-substrings in time O(q n (k)) and can be constructed in time O(t n ). Assume that s n ≥ n and q n (k) ≥ k is non-decreasing with respect to k. We can then design an algorithm that preprocesses the input string in time O(t n ) and answers queries dynamically under edit operations in worst-case time O(q n ( )) , where is such that q n ( ) = (t n + n)∕ , using O(s n ) space.
Proof We first build D for the input string. The k-substring of S after the subsequent edit operations is stored. We keep a counter C of the number of queries answered since the point in time to which our data structure refers; if C ≤ 2 and a new edit operation occurs, we create the (2C + 1)-substring representing the string from the (2C − 1)-substring we have using Lemma 17 in time O(C) = O( ) and answer the query in time O(q n (C)) = O(q n ( )).
For convenience let us assume that is an integer. As soon as C = , we start recomputing the data structure D for the string after all the edit operations so far, but we allocate this computation so that it happens while answering the next queries. First we create a working copy of the string in O(n) time and then construct the data structure in O(t n ) time.
When C = 2 , we set C to , dispose of the original data structure and string and start using the new ones for the next queries while computing the next one.
The following invariant is kept throughout the execution of the algorithm: the data structure being used refers to the string(s) at most 2 edit operations before.

3
Hence, the query time is O(q n ( )) . The extra time spent during each query for computing the data structure is also O((t n + n)∕ ) = O(q n ( )) since the O(t n + n) time is split equally among queries. At every point in the algorithm we store at most two copies of the data structure. ◻

Fully Dynamic Longest Palindrome Substring
Palindromes (also known as symmetric strings) are one of the fundamental concepts on strings with applications in computational biology (see, e.g., [54]). A recent progress in this area was the design of an O(n log n)-time algorithm for partitioning a string into the minimum number of palindromes [44,57] (that was improved to O(n) time [24] afterwards). The main combinatorial insight of these results is that the set of lengths of suffix palindromes of a string can be represented as a logarithmic number of arithmetic progressions, each of which consists of palindromes with the same shortest period. Funakoshi et al. [48] use this fact to present a data structure for computing a longest palindrome substring of a string after a single edit operation. This problem is called LOngeSt PaLindrOme SubString after One edit. They obtain O(log log n)-time queries with a data structure of O(n) size that can be constructed in O(n) time. We present a fully dynamic algorithm with Õ ( √ n)-time queries for this problem.
A palindrome is a string U such that U R = U . For a string S, by LSPal(S) let us denote a longest substring of S that is a palindrome. We first show a data structure with Õ (n) preprocessing time and Õ (1) time for internal longest palindrome substring queries; it is based on orthogonal range maximum queries. We then show a solution to the following auxiliary problem.
The center of a palindrome substring is not a palindrome. For short, we call maximal palindrome substrings MPSs. By (S) we denote the set of all MPSs of S. For each integer or half-integer center between 1 and n there is exactly one MPS with this center, so | (S)| = 2n − 1 . The set (S) can be computed in O(n) time using Manacher's algorithm [66] or queries [54].

Internal Queries
In an internal LSPal query we are to compute the longest palindrome substring of a given substring of S. In the following lemma we show that such queries can be reduced to 2d range maximum queries. In either case, we use Lemma 1 to answer a 2d RMQ; the complexity follows. ◻

Lemma 19 Let S be a string of length n. After
If the answer to k -SubString LSPal contains none of the changed positions, it can be found by asking k + 1 internal LSPal queries.

Cross-Substring Queries
Let us assume that the boundary between F i−1 and F i is the closest one to the center of LSPal. In what follows, we consider the case that it lies to the left of the center. Then, the palindrome cut to the positions in F i is a prefix palindrome of F i . The opposite case is symmetric. In total, this gives rise to 2k cases that need to be checked.
The structure of palindromes being prefixes of a string has been well studied. It is known that a palindrome being a prefix of another palindrome is also its border, and a border of a palindrome is always a palindrome; see [44]. Hence, the palindromes being prefixes of F i are the borders of the longest such palindrome, further denoted by U 0 . We are interested in the borders of U 0 .
The palindrome U 0 can be computed by Lemma 19. By Lemma 11, the set of border lengths of U 0 can be divided into O(log n) arithmetic sequences. Each such sequence will be treated separately. Assume first that it contains at most two elements. Let f i denote the starting position of F i in S ′ , for i = 1, … , k . Then for each element u representing a palindrome U, the longest palindrome having the same center as U in S ′ has the length This LCE query can be answered in O(log n) time using Lemma 13 for S#S R .

3
Now assume that the arithmetic sequence has more than two elements. Let p be the difference of the arithmetic sequence, be its length, u be its maximum element, and Then the longest palindrome having the same center as an element of this sequence has length By Observation 2, this formula can be evaluated in O(log n) time.
Over all arithmetic sequences, we obtain O(log 2 n) query time.

Round-Up
The results of the two subsections can be combined into an algorithm for k -Sub-String LSPaL. Using the general scheme (Lemma 18), we obtain a solution to fully dynamic longest palindrome substring problem.

Fully Dynamic Longest Lyndon Substring
A Lyndon string is a string that is lexicographically smaller than all its suffixes [64]. (Let us recall that string S is smaller in the lexicographic order than string T, written as S < T , if S is a proper prefix of T or S (i) = T (i) and S[i + 1] < T[i + 1] .) For example, aabab and a are Lyndon strings, whereas abaab and abab are not. Lyndon strings are an object of interest in combinatorics on words, especially due to the Lyndon factorization theorem [34] that asserts that every string can be uniquely decomposed into a non-decreasing sequence of Lyndon strings. More formally, the Lyndon factorization of a string S, denoted as LF S , is the unique way of writing S as L 1 … L p where L 1 , … , L p are Lyndon strings (called factors) that satisfy L 1 ≥ L 2 ≥ ⋯ ≥ L p (if S is a Lyndon string, then its Lyndon factorization is composed of S itself). Recently, Lyndon strings have found important applications in algorithm design [68] and were used to settle a known conjecture on the number of maximal repetitions in a string [19,20]. Urabe et al. [77] presented a data structure for computing a longest substring of a string being a Lyndon string in the restricted dynamic setting of a single edit that is reverted afterwards. This problem is called LOngeSt LyndOn SubString after One edit. Their data structure can be constructed in O(n) time and space and answers queries in OO(log n) time. A simple observation of [77] is the following.
Lemma 21 (Lemma 3 in [77]) The longest Lyndon substring of a string is the longest factor in its Lyndon factorization.
Thus, this work indirectly poses the question of maintaining the Lyndon factorization of a dynamic string. We first show how to answer queries for the Lyndon factorization of a given substring. We then present an algorithm that maintains a representation of the Lyndon factorization of a string with Õ ( √ n)-time queries in the fully dynamic setting.

Internal Queries
We consider the following internal Lyndon factorization queries in a static string: In the rest of this subsection, we show the following lemma. Main Idea Urabe et al. [77] show how to efficiently compute a representation of a Lyndon factorization of a prefix of a string and of a suffix of a string. For the prefixes, their solution is based on the Lyndon representations of prefixes of a Lyndon string, whereas for the suffixes, they rely on the structure of the Lyndon tree (due to [21]). We combine the two approaches to obtain Lemma 22. Let us start with the definition of a Lyndon tree of a Lyndon string W [21]. If W is not a Lyndon string, the tree is constructed for $W , where $ is a special character that is smaller than all the characters in W. An example of a Lyndon tree can be found in Fig. 5 below.

Definition 1
The standard factorization of a Lyndon string S is (U, V) if S = UV and V is the longest proper suffix of S that is a Lyndon string. In this case, both U and V are Lyndon strings. The Lyndon tree of a Lyndon string S is the full binary tree defined by recursive standard factorization of S. More precisely, S is the root; if |S| > 1 , its left child is the root of the Lyndon tree of U, and its right child is the root of the Lyndon tree of V.
If v is a node of a binary tree, u is a strict ancestor of v (i.e. u ≠ v ), and w is the right child of u that is not an ancestor of v, then we call w a right uncle of v. By U(v) we denote the list of all right uncles of v in bottom-up order. The following lemma was shown in [77].

Lemma 23 (Lemma 12 in [77]) LF S
We say that S is a pre-Lyndon string if it is a prefix of a Lyndon string. The following lemma is a well-known property of Lyndon strings; see Lemma 10 in [77] or the book [59].
Lemma 24 ( [59, 77]) If S is a pre-Lyndon string, then there exists a unique Lyndon string X such that S = X k X � where X ′ is a proper prefix of X and k ≥ 1 . Moreover, In this case LF S can be represented in a compact form by simply writing the first part of the representation as (X) k . Note that X ′ is a prefix of S and hence is also a pre-Lyndon string. Finally, the string X from Lemma 24 also satisfies the following property.

Observation 4 |X| is the shortest period of S.
Proof Clearly |X| is a period of S. If it was not the shortest period, then X would have a non-trivial period, hence a proper non-empty border B. Then, since B < X , X would not be a Lyndon string, a contradiction. ◻ The above properties are sufficient to determine Lyndon factorizations of prefixes and suffixes of a string S according to [77] as follows: • To compute LF S (j) , take the minimal number of prefix factors in LF S that cover S (j) , trim the last of these factors accordingly, compute the Lyndon factorization of the trimmed factor by repeatedly using Lemma 24, and append it to the previous factors.

Example 3
We consider the Lyndon string S = $ , whose Lyndon tree is presented in Fig. 5. Let us suppose that we want to compute the Lyndon factorization of the substring S[4 . . 13] = . We first find the LCA of the leaves representing S [3] and S [13]. The path from this LCA to the leaf representing S [3] is shown in blue. The Lyndon factorization of S [4 . . 14] can be obtained by the right uncles of S [3] (red and yellow nodes). In order to have LF S [4. .13] , the last right uncle (in yellow) needs to be trimmed. By Observation 4, we take the shortest period of the trimmed string S[9 . . 13] = and obtain the Lyndon factorization , . Thus we obtain the Lyndon factorization of S [4 . . 13] , also depicted in Fig. 5, which is , , , , .
In order to make this computation efficient we make use of the heavy path decomposition of LTree(S) . Each non-leaf node v stores, as rc(v) , the length of its right child provided that it is not present on its heavy path, or −1 otherwise. A balanced BST with all the nodes in the heavy path with positive values of rc given in bottomup order is stored for every heavy path. It can be augmented in a standard manner to support the following types of queries on any subpath of the heavy path in O(log n) time: • longest-subpath: the maximal value rc on the subpath; • count-subpath: the number of nodes with a positive value of rc on the subpath; • select-subpath(i): the i-th node with a positive value of rc on the subpath.
These precomputations take O(n) time and space. Finally, a lowest common ancestor data structure can be constructed over the Lyndon tree in O(n) time and O(n) space [22] supporting LCA queries in O(1) time per query.
We represent LF S[i. .j] as follows. First, the pair of nodes (v 1 , w � ) where w ′ is the left child of w is stored. Second, the Lyndon factorization of a prefix u ′ of the right child u of w, which we compute by recursively applying Lemma 24. We can store the LF u ′ in a compact form in O(log n) space. This representation can be computed in O(log 2 n) time as follows: Finally let us check that we can support the desired types of queries in O(log 2 n) time: • longest(i, j): We divide the path from v 1 to w ′ into O(log n) subpaths of heavy paths and for each of them ask a longest-subpath query. This takes O(log 2 n) time. We compare the maximum of the results with the maximum length of a factor in the second part of the representation, in O(log n) time. • count(i, j) is implemented analogously using count-subpath. • select(i, j, t): We use count-subpath queries to locate the subpath that contains the t-th factor in the whole factorization or check that none of the subpaths contains it. In the first case, we use a select-subpath query. In the second case, we locate the correct factor in the second part of the representation in O(log n) time.
We have thus proved Lemma 22.

Cross-Substring Queries
In this section we show how to compute a representation of the Lyndon factorization of a k-substring S ′ from the Lyndon factorizations of the k involved substrings. We want to be able to answer longest, count and select(t) queries for S ′ -not for substrings of S ′ . We rely on the following characterization of Lyndon factorizations of concatenations of strings.
Lemma 25 (see Lemmas 6 and 7 in [77]; originally due to [15,39,56]) Assume that LF U = (L 1 ) p 1 , … , (L m ) p m and LF V = (L � 1 ) p � 1 , … , (L � m � ) p � m � . Then: We will represent LF S ′ for a k-substring S ′ of S as a sequence of elements of two types: powers of Lyndon substrings of S ′ and pairs of nodes (v, w) in LTree(S) that denote the bottom-up list of right uncles of nodes on the path from v to w in the tree. To compute a representation of LF S ′ for S � = F 1 … F k , we compute the representation of LF F i for all i = 1, … , k defined in the above subsection and then repetitively apply Lemma 25. (Note that the representation of LF F i is of the desired form for all i.) Let S �� = F 1 … F i−1 and assume that the desired representation of LF S ′′ has been already computed. Then, by Lemma 25, a representation of LF S ′′ F i can be obtained by removing a number of trailing elements in LF S ′′ , a number of leading elements of LF F i , and merging them with at most one power of a Lyndon substring Z of S ′ in between. The removal of an element may be captured by (a) the removal of a power of a Lyndon substring, (b) the removal of a pair of nodes, (c) the alteration of some pair of nodes, corresponding to trimming the underlying path.
The length of our representation of LF S ′ is O(k log n) and all its elements are stored in a left-to-right order in a balanced BST. For each element, the maximum length of a factor and the number of factors are also stored in the BST. The BST is augmented with the counts of Lyndon factors so that one can identify in logarithmic time the element of the representation that contains the t-th We have just proved the following lemma.

Lemma 26
After an O(n)-time and space preprocessing of a string S of length n, given a k-substring of S, for k ≤ n, we can process it in O(k log 3 n) time, answering longest and count queries. We can then answer select(t) queries in O(log 2 n) time each.
Remark 2 One could define the Lyndon factorization of a string S as a unique way of expressing S as (L 1 ) a 1 … (L p ) a p where a 1 , … , a p ≥ 1 are integers and L 1 > L 2 > ⋯ > L p . With this definition, the same complexities of operations as in Lemma 26 can be achieved for count queries, returning the length p of the representation, and select(t) queries for 1 ≤ t ≤ p. Let us recall that LF S ′ is stored using powers of Lyndon substrings of S and lists of right uncles on heavy paths of Lyndon trees as building blocks. Then the main modification to our approach is to store, for each right uncle, a bit stating if is is different from the previous right uncle on its heavy path. This additional data can be computed in constant time per right uncle using LCE queries. Then the countsubpath and select-subpath queries consider only the uncles for which this bit is set (treating the first right uncle on each heavy path separately).

Round-Up
We apply the time slicing technique (Lemma 18) to Lemma 26 in order to obtain a solution in the fully dynamic setting.

Final Remarks
We anticipate that the techniques presented in this paper to obtain fully dynamic algorithms for several classical problems on strings are applicable in a wider range of problems on strings.
We conclude by summarizing the main results of a very recent work [29] on the dynamic LCS problem, which improves over some of our results. It was published after the submission of this manuscript. This work shows that the fully dynamic LCS problem admits a solution with Õ (1) update time, that uses Õ (n) space. The authors complement this upper bound by an unconditional lower bound: the update time of any polynomial-sized data structure for the fully dynamic LCS is (log n∕ log log n) . They also consider the partially dynamic LCS problem, in which only one of the two strings is subject to updates. This problem has been considered in an earlier version of our work. For this problem, the authors show an Õ (n)-space, O(log 2 n)-update time solution and an Õ (n 1+ )-space, O(log log n)-update time solution, for any constant > 0 . On the lower bounds' side, they show that the update time of any Õ (n) -sized data structure for this problem is (log n∕ log log n).