One-Variable Word Equations in Linear Time

In this paper we consider word equations with one-variable (and arbitrarily many occurrences of it). A recent technique of recompression, which is applicable to general word equations, is shown to be suitable also in this case. While in general case the recompression is nondeterministic in case of one-variable it becomes deterministic and its running time is O(n+#Xlogn)\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 + \#_X \log n)$$\end{document}, where #X\documentclass[12pt]{minimal} \usepackage{amsmath} \usepackage{wasysym} \usepackage{amsfonts} \usepackage{amssymb} \usepackage{amsbsy} \usepackage{mathrsfs} \usepackage{upgreek} \setlength{\oddsidemargin}{-69pt} \begin{document}$$\#_X$$\end{document} is the number of occurrences of the variable in the equation. This matches the previously best algorithm due to Dąbrowski and Plandowski. Then, using a couple of heuristics as well as more detailed time analysis, the running time is lowered to 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} in the RAM model. Unfortunately, no new properties of solutions are shown.

1. Introduction 1.1.Word equations.The problem of satisfiability of word equations was considered as one of the most intriguing in computer science and its study was initiated by Markow already in the '50.The first algorithm for it was given by Makanin [13], despite earlier conjectures that the problem is undecidable.The proposed solution was very complicated in terms of proof-length, algorithm and computational complexity.It was improved several times, however, no essentially different approach was proposed for over two decades.
An alternative algorithm was proposed by Plandowski and Rytter [19], who shoved that each minimal solution of a word equation is exponentially compressible, in the sense that for a word equation of size n and minimal solution of size N the LZ77 (a popular practical standard of compression) representation of the minimal solution is polynomial in n and log N .Hence a simple non-deterministic algorithm that guesses a compressed representation of a solution and verifies the guess has running time polynomial in n and log N .However, at that time the only bound on N followed from Makanin's work (with further improvements) and it was triply exponential in n.
Soon after Plandowski showed, using novel factorisations, that N is at most doubly exponential [16], showing that satisfiability of word equations is in NEXPTIME.Exploiting the interplay between factorisations and compression he improved the algorithm so that it worked in PSPACE [17].
Producing a description of all solutions of a word equation, even when a procedure for verification of its satisfiability is known, proved to be also a non-trivial task.Still, it is also possible to do this in PSPACE [18], though insight and non-trivial modifications to the earlier procedure are needed.
On the other hand, it is only known that the satisfiability of word equations is NP-hard.
1.1.1.Two variables.Since in general the problem is outside P, it was investigated, whether some subclass is feasible, with a restriction on the number of variables being a natural candidate.It was shown by Charatonik and Pacholski [2] that indeed, when only two variables are allowed (though with arbitrarily many occurrences), the satisfiability can be verified in deterministic polynomial time.The degree of the polynomial was very high, though.This was improved over the years and the best known algorithm is by Dąbrowski and Plandowski [3] and it runs in O(n 5 ) and returns a description of all solutions.
1.1.2.One variable.Clearly, the case of equations with only one variable is in P. Constructing a cubic algorithm is almost trivial, small improvements are needed to guarantee a quadratic running time.First non-trivial bound was given by Obono, Goralcik and Maksimenko, who devised an O(n log n) algorithm [15].This was improved by Dąbrowksi and Plandowski [4] to O(n + # X log n), where # X is the number of occurrences of the variable in the equation.Furthermore they showed that there are at most O(log n) distinct solutions and at most one infinite family of solutions.Intuitively, the O(# X log n) summand in the running time comes from the time needed to find and test these O(log n) solutions.This work was not completely model-independent, as it assumed that the alphabet Γ is finite or that it can be identified with numbers.A more general solution was presented by Laine and Plandowski [11], who improved the bound on the number of solutions to O(log # X ) (plus the infinite family) and gave an O(n log # X ) algorithm that runs in a pointer machine model (i.e.letters can be only compared and no arithmetical operations on them are allowed); roughly one candidate for the solution is found and tested in linear time.
1.2.Recompression.Recently, the author proposed a new technique of recompression based on previous techniques of Mehlhorn et.al [14] (for dynamic text equality testing), Lohrey and Mathissen [12] (for fully compressed membership problem for NFAs) and Sakamoto [20] (for construction of the smallest grammar for the input text).This method was successfully applied to various problems related to grammar-compressed strings [7,5,6].Unexpectedly, this approach was also applicable to word equations, in which case alternative proofs of many known algorithmic results were obtained using a unified approach [8].
The technique is based on iterative application of two replacement schemes performed on the text t: pair compression of ab: For two different letters a, b such that substring ab occurs in t replace each of ab in t by a fresh letter c. a's block compression: For each maximal block a , where a is a letter and > 1, that occurs in t, replace all a s in t by a fresh letter a .In one phase, pair compression (block compression) is applied to all pairs (blocks, respectively) that occurred at the beginning of this phase.Ideally, each letter is then compressed and so the length of t halves, in a worst-case scenario during one phase t is still shortened by a constant factor.
The surprising property is that such a schema can be efficiently applied to grammar-compressed data [7,5] or to text given in an implicit way, i.e. as a solution of a word equation [8].In order to do so, local changes of the variables (or nonterminals) are needed: X is replaced with a X (or Xa ), where a is prefix (suffix, respectively) of the substitution for X.In this way the solution that substitutes a w (or wa , respectively) for X is implicitly replaced with one that substitutes w.

Recompression and one-variable equations.
Clearly, as the recompression approach works for general word equations, it can be applied also to restricted subclasses.However, while in case of word equations it heavily relies on the nondeterminism, when restricted to instances with one variable it can be easily determinised; Section 2 recalls the main notions of word equations and recompression.Furthermore, a fairly natural implementation has O(n + # X log n) running time, so the same as the Dąbrowski and Plandowski algorithm [4]; this is presented in Section 3. Furthermore adding a few heuristics, data structures as well as applying a more sophisticated analysis yields a linear running time, this is described in Section 4.

Outline of the algorithm.
In this paper we present an algorithm for one-variable equation based on the recompression.It also provides a compact description of all solutions of such an equation.Intuitively: when pair compression is applied, say ab is replaced by c (assuming it can be applied) then there is a one-to-one correspondence of the solutions before and after the compression, this correspondence is simply an exchange of all abs by cs and vice-versa.The same applies to the block compression.On the other hand, the modification of X can lead to loss of solutions (note that for technical reasons we do note consider the solution S(X) = ): when X is to be replaced with a X the new equation has corresponding solutions for S other than S(X) = a .So before the replacement, it is tested whether S(X) = a is a solution and if so, it is reported.The test itself is simple: both sides of the equation are read and their values under substitution S(X) = a are created on the fly and compared symbol by symbol, until a mismatch is found or both strings end.
It is easy to implement the recompression so that one phase takes linear time.Then the cost can be distributed to explicit words between the variables, each of them is charged proportionally to its length.Consider such a string w, if it is long enough, its length decreases by a constant factor in one phase, see Lemma 10.Thus, the cost of compressing this fragment and testing a solution can be charged to the lost length.However, this is not true when w is short and the # X log n summand in the running time comes from bounding the running time for such 'short' strings.
In Section 4 it is shown that using a couple of heuristics as well as more involved analysis the running time can be lowered to O(n).The mentioned heuristics are as follows: • The problematic 'short' words between the variables need to be substrings of the 'long' words, this allows smaller storage size and consequently faster compression.• When we compare Xw 1 Xw 2 . . .w m X from one side of the equation with its copy (i.e. another occurrence Xw 1 Xw 2 . . .w m X) on the other side, we make such a comparison in O(1) time (using suffix arrays).• (S(X)u) m and (S(X)u ) m (perhaps offsetted) are compared in O(|u| + |u |) time instead of naive O(m • |u| + m • |u |), using simple facts from combinatorics on words.Furthermore a more insightful analysis shows that problematic 'short' words in the equation can be used to invalidate several candidate solutions fast, even before a mismatch in the equation is found during the testing.This allows a tighter estimation of the time spent on testing the solutions.
A note on the computational model.In order to perform the recompression efficiently, some algorithm for grouping pairs is needed.When we can identify the symbols in Γ with consecutive numbers, the grouping can be done using RadixSort in linear time.Thus, all (efficient) applications of recompression technique make such an assumption.On the other hand, the second of the mentioned heuristics craves checking string equality in constant time, to this end a suffix array [9] plus a structure for answering longest common prefix query (lcp) is employed [10] on which we use range minimum queries [1].The last structure needs the flexibility of the RAM model to run in O(1) time per query.

One-variable equations.
A word equation with one variable over the alphabet Γ and variable X is 'A = B', where A, B ∈ (Γ ∪ {X}) * .During the run of algorithm WordEqSat we introduce new letters into Γ, but no new variable is introduced.In this paper we shall consider only equations with one variable.
Without loss of generality in a word equation A = B one of A and B begin with a variable and the other with a letter: • if they both begins with the same symbol (be it letter or nonterminal), we can remove this symbol from them, without affecting the set of solutions; • if they begin with different letters, this equation clearly has no solution.The same applies to the last symbols of U and V .Thus, in the following we assume that the equation is of the form (1) where A i , B i ∈ Γ * (we call them words or explicit words) and n A (n B ) denote the number of X occurrences in A (B, respectively).Note that exactly one of A n A , B n B is empty and A 0 is nonempty.If this condition is violated for any reason, we greedily repair it by cutting identical letters (or variables) from both sides of the equation.We say that A 0 is the first word of the equation and the non-empty of A n A and B n B is the last word.We additionally assume that none of words A i , B j is empty.We later (after Lemma 4) justify why this is indeed without loss of generality.
A substitution S assigns a string to X, we expand it to (X ∪ Γ) * with an obvious meaning.A solution is a substitution such that S(A) = S(B).For a given equation A = B we are looking for a description of all its solutions.We treat the empty solution S(X) = in a special way and always assume that S(X) = .
Note that if S(X) = , then using (1) we can always determine the first (a) and last (b) letter of S(X) in O(1) time.In fact, we can determine the length of the a-prefix and b-suffix of S(X).

Lemma 1. For every solution S of a word equation the first letter of S(X) is the first letter of A 0 and the last the last letter of A n A or B n B (whichever is non-empty).
If If the first letter of A 0 is a and A 0 / ∈ a + then there is at most one solution S(X) ∈ a + , existence of such a solution can be tested (and its length returned) in O(|A| + |B|) time.Furthermore, for S(X) / ∈ a + the lengths of the a-prefixes of S(X) and A 0 are the same.
It is later shown that finding all solutions from a + can be done in linear time, see Lemma 11.
Proof.Concerning the first claim, observe that the first letter of S(A) is the first letter of A 0 , while the first letter of S(B) is the first letter of S(X), hence those letters are equal.The same applies to the last letter of S(X) and the last letter of A n A or B n B , whichever of them is non-empty.Consider the case when A 0 ∈ a + and suppose that S(X) / ∈ a * , let ≥ 0 be the length of the a-prefix of S(X).The length of the a-prefix of S(A) is then |A 0 | + > , which is the length of the a-prefix of S(B), contradiction.Hence S(X) ∈ a + .
Consider now the case when A 0 begins with a but A 0 / ∈ a + , let its a-prefix has length A .Consider S(X) ∈ a + , say S(X) = a .Let the first letter other than a in B be the B + 1 letter in B and let it be in explicit word B i .If there is no such B i then there is no solution S(X) ∈ a + , as then S(B) consists only of as, which is not true for S(A).The length of the a-prefix of S(A) is A , while the length of the a-prefix of S(B) is B + i • .Those two need to be equal, so A = B + i • and consequently = A − B i , so this is the only candidate for the solution.It is easy to verify whether S(X) = a is a solution for a single in linear time.It is enough to compare S(A) and S(B) letter by letter, note that they can be created on the fly while reading A and B. Each such comparison consumes one symbol from A and B (note that if we compare a suffix of S(X), i.e. some a for < , with S(X) = a we simply remove a from both those strings).So the running time is linear.
Lastly, consider S(X) / ∈ a * .Then the a-prefix of S(A) has length A and as S(X) / ∈ a + , the a-prefix of S(B) is the same as the a-prefix of S(X), which consequently has length A .

Symmetric version of Lemma 1 holds for the suffix of S(X).
By TestSimpleSolution(a) we denote a procedure, described in Lemma 1, that for A 0 / ∈ a * establishes the unique possible solution S(X) = a , tests it and returns if this indeed is a solution.

Representation of solutions.
Consider any solution S of A = B. We claim that S(X) is uniquely determined by its length and so when describing solution of A = B it is enough to give their lengths.

Lemma 2. Each solution S of equation of the form
where A is a prefix of A 0 and k ≥ 0. In particular, it is uniquely defined by its length.
Proof.If |S(X)| ≤ |A 0 | then S(X) is a prefix of A 0 , so S(X) is uniquely determined by its length.When |S(X)| > |A 0 | then S(A) begins with A 0 S(X) while S(B) begins with S(X) and thus S(X) has a period A 0 .Consequently, it is of the form A k 0 A, where A is a prefix of A 0 .
Weight.Each letter in the current instance of our algorithm represents some string (in a compressed form) of the input equation, we store its weight which is the length of such a string.Furthermore, when we replace X with a X (or Xa ) we keep track of sum of weights of all letters removed so far from X.In this way, for each solution of the current equation we know what is the length of the corresponding solution of the original equation (it is the sum of weights of letters removed so far from X and the weight of the current solution).Therefore, in the following, we will not explain how we recreate the solutions of the original equation from the solution of the current one.

Recompression.
We recall here the technique of recompression [7,5,8], restating all important facts about it.Note that in case of one variable many notions simplify.

Preserving solutions.
All subprocedures of the presented algorithm should preserve solutions, i.e. there should be a one-to-one correspondence between solution before and after the application of the subprocedure.However, when we replace X with a X (or Xb r ), some solutions may be lost in the process and so they should be reported.We formalise these notions.We say that a subprocedure preserves solutions when given an equation A = B it returns A = B such that for some strings u and v (calculated by the subprocedure) • some solutions of A = B are reported by the subprocedure; • for each unreported solution S of A = B there is a solution S of A = B , where S(X) = uS (X)v and The intuitive meaning of these conditions is that during transformation of the equation, either we report a solution or the new equation has a corresponding solution (and no new 'extra' solutions).
By h c→ab (w) we denote the string obtained from w by replacing each c by ab, which corresponds to the inverse of pair compression.We say that a subprocedure implements pair compression for ab, if it satisfies the conditions for preserving solutions above, but with S(X) = u h c→ab (S (X))v and S(A) = u h c→ab (S (A ))v replacing S(X) = uS (X)v and S(A) = uS (A )v.Similarly, by h a →a (w) we denote a string with letters a replaced with blocks a (note that this requires that we know, which letters 'are' a and what is the value of , but this is always clear from the context) and we say that a subprocedure implements blocks compression for a letter a.The intuitive meaning is the same as in case of preserving solutions: we not loose, nor gain any solutions.
Given an equation A = B, its solution S and a pair ab ∈ Γ 2 occurring S(U ) (or S(V )) we say that this occurrence is explicit, if it comes from substring ab of A (or B, respectively); implicit, if it comes (wholly) from S(X); crossing otherwise.A pair is crossing if it has a crossing occurrence and non-crossing otherwise.Similar notion applies to maximal blocks of as, in which case we say that a has a crossing block or it has no crossing blocks.Alternatively, a pair ab is crossing if b is the first letter of S(X) and aX occurs in the equation or a is the last letter of S(X) and Xb occurs in the equation or a is the last and b the first letter of S(X) and XX occurs in the equation.
Unless explicitly stated, we consider crossing/non-crossing pairs ab for a = b.Note that as the first (last) letter of S(X) is the same for each S, see Lemma 1, the definition of the crossing pair does not depend on the solution; the same applies to crossing blocks.
When a pair ab is non-crossing, its compression is easy, as it is enough to replace each explicit ab with a fresh letter c let a ∈ Γ be an unused letter Since c is a free letter, the S(A) is obtained from S (A ) by replacing each c with ab, the same applies to S(X) and S (X) as well as S(B) and S (B ).Hence S(A) = h c→ab (S (A)) = h c→ab (S (B)) = S(B) and S(X) = h c→ab (S (X)), as required by the definition of implementing the pair compression.
Lastly, for a solution S of A = B take the corresponding S defined as S(X) = h c→ab (S (X)) (i.e.replacing each c with ab in S (X)).It can be easily shown that S(A) = h c→ab (S (A )) and The proof for the block compression follows in the same way.
The main idea of the recompression method is the way it deals with the crossing pairs: imagine ab is a crossing pair, this is because S(X) = bw and aX occurs in A = B or S(X) = wa and bX occurs in it (the remaining case, in which S(X) = awb and XX occurs in the equation is treated in the same way).The cases are symmetric, so we deal only with the first one.To 'uncross' ab in this case it is enough to 'left-pop' b from X: replace each X in the equation with bX and implicitly change the solution to S(X) = w.Note that before replacing X with aX we need to check, whether S(X) = a is a solution, as this solution cannot be represented in the new equation; similar remark applies to replacing X with Xb.

Algorithm 3 Pop(a, b)
1: if b is the first letter of S(X) then if TestSimpleSolution(a) returns 1 then S(X) = a is a solution Note that Lemma 4 justifies our earlier claim that without loss of generality we can assume that none of A i , B j is empty: at the beginning of the algorithm we can run Pop(a, b) once for a being the first and B the last letter of S(X).This ensures the claim and increases the size of the instance at most thrice.
Proof.It is easy to verify that a pair ab is crossing if and only if one of the following situations occurs: CP1 aX occurs in the equation and the first letter of S(X) is b; CP2 Xb occurs in the equation and the last letter of S(X) is a; CP3 XX occurs in the equation, the first letter of S(X) is b and the last a.Let A = B be the obtained equation, we show that ab in A = B is noncrossing.Consider whether X was replaced by bX is line 4.If not, then the first letter of S(X) and S (X) is not b, so ab cannot be crossing because of (CP1) nor (CP3).Suppose that X was replaced with bX.Then to the left of each X there is a letter which is not a, so none of situations (CP1), (CP3) occurs.
A similar analysis applied to the last letter of S(X) yields that (CP2) cannot happen and so ab cannot be a crossing pair.
Pop can be naturally divided into two parts, which correspond to the replacement of X by bX and the replacement of X by Xa.We show for the first one that it preserves solutions, the proof for the second one is identical.
If S(X) does not begin with b (recall that all solutions have the same first letter) then nothing changes and the set of solutions is preserved.Otherwise S(X) = bw.In this case the solutions of the new equation shall be obtained by prepending b to them.Consider what happens with a solution S(X) = bw if w = : then it is reported in line 3; if w = : then S (X) = w is a solution of the obtained equation.Note that the solution reported by Pop is verified, so it is indeed a solution.Furthermore, the only reported solutions is S(X) = b, none of which corresponds to a non-empty solution after popping.Lastly, when S (X) = w is a solution after popping b then clearly S(X) = bw is a solution of A = B. A symmetric analysis is done for the operation of right-popping a, which ends the proof.Now the presented procedures can be merged into one procedure that turns crossing pairs into noncrossing ones and then compresses them, effectively compressing crossing pairs.

The proof follows by combining Lemma 3 and 4.
There is one issue: the number of non-crossing pairs can be large, however, a simple preprocessing, which basically applies Pop, is enough to reduce the number of crossing pairs to 2.
Algorithm 5 PreProc Ensures that there are at most 2 crossing pairs 1: let a, b be the first and last letter of S(X) 2: run Pop(a, b) Lemma 6. PreProc preserves solution and after its application there are at most two crossing pairs.Proof.It is enough to show that there are at most 2 crossing pairs, as the rest follows form Lemma 4. Let a and b be the first and last letters of S(X), and a , b such letters after the application of PreProc.Then each X is proceeded with a and succeeded with b in A = B .So the only crossing pairs are aa and b b (note that this might be the same pair or part of a letter-block, i.e. a = a or b = b ).
The problems with crossing blocks can be solved in a similar fashion: a has a crossing block if and only if aa is a crossing pair.So we 'left-pop' a from X until the first letter of S(X) is different than a, we do the same with the ending letter b.This can be alternatively seen as removing the whole a-prefix (b-suffix, respectively) from X: suppose that S(X) = a wb r , where w does not begin with a nor end with b.Then we replace each X by a Xb r implicitly changing the solution to S(X) = w, see Algorithm 6.
Algorithm 6 CutPrefSuff Cutting prefixes and suffixes; assumes that A 0 is not a block of letters Require: A 0 is not a block of letters, the non-empty of A n A , B n B is not a block of letters 1: let a be the first letter of S(X) 2: report solution found by TestSimpleSolution(a) Excludes S(X) ∈ a + from further considerations.
3: let > 0 be the length of the a-prefix of A 0 By Lemma 1 S(X) has the same a-prefix 4: replace each X in A = B by a X a is stored in a compressed form, 5: implicitly change S(X) = a w to S(X) = w 6: let b be the last letter of S(X) It cannot be that S(X) ∈ b + 7: report solution found by TestSimpleSolution(b) Exclude S(X) ∈ b + from furhter considerations.implicitly change S(X) = wb r to S(X) = w Lemma 7. Let a be the first letter of the first word and b the last of the last word.If the first word is not a block of as and the last not a block of bs then CutPrefSuff preserves solutions and after its application there are no crossing blocks of letters.
Proof.Consider first only the changes done by the modification of the prefix.Suppose that S(X) = a w, where w does not begin with a.If w = then as A 0 / ∈ a + by Lemma 1 there is only one such solution and it is reported in line 2. Otherwise, by Lemma 1, each solution S of the equation is of the form S(X) = a w, where a is the a-prefix of A 0 and w = nor does it begin with a. Then the S (X) = w is the solution of the new equation.Similarly, for any solution S (X) = w the S(X) = a w is the solution of the original equation.
The same analysis can be applied to the modifications of the suffix: observe that if at the beginning the last word was not a block of bs it did not become one during the cutting of the a-prefix.
Lastly, suppose that some letter c has a crossing block, without loss of generality assume that c is the first letter of S(X) and cX occurs in the equation.But this is not possible: X was replaced by a X and so the only letter to the left of X is a and S(X) does not start with a, contradiction.
The CutPrefSuff allows defining a procedure BlockComp that compresses maximal blocks of all letters, regardless of whether they have crossing blocks or not.

BlockCompNCr(a)
Lemma 8. Let a be the first letter of the first word and b the last of the last word.If the first word is not a block of as and the last not a block of bs then BlockComp implements the block compression for letters present in A = B before its application.
The proof follows by combining Lemma 3 and 7.

Main algorithm
The following algorithm OneVarWordEq is basically a specification of the general algorithm for testing the satisfiability of word equations [8] and is built up from procedures presented in the previous section.Before showing the running time, let us first comment on how the equation is stored.Each of sides (A and B) is represented as two lists of pointers to strings, i.e. to A 0 , A 1 , . . ., A n A and to B 0 , B 1 , . . ., B n B .Each of those words is stored as a doubly-linked list.When we want to refer to a concrete word in a phase, we use names A i and B j , when we want to stress its evolution in phases, we use names A i-word and B j-word.
Shortening of the solutions.The most important property of OneVarWordEq is that the explicit strings between the variables shorten (assuming that they have a large enough length).To show this we use the following technical lemma, which is also used several times later on: We say that a word A i (B i ) is short if it consists of at most 100 letters and long otherwise.To avoid usage of strange constants and its multiplicities, we shall use N = 100 to denote this value.

Lemma 10. Consider the length of the A i-word (or B j-word).
If it is long then its length is reduced by 1/4 in this phase.If it is short then after the phase it still is.The length of each unreported solution is reduced by at least 1/4 in a phase.
Additionally, if the first (last) word is short and has at least 2 letters then its length is shortened by at least 1 in a phase.
Proof.We shall first deal with the words and then comment how this argument extends to the solutions.Consider two consecutive letters a, b in any word at the beginning of a phase.By Lemma 9 at least one of those letters is compressed in this phase.Hence each uncompressed letter in a word (the last letter) can be associated with the two letters to the right that are compressed.This means that in a word of length k during the phase at least 2(k−1) 3 letters are compressed i.e. its length is reduced by at least k−1 3 letters.On the other hand, letters are introduced into words by popping them from variables.Let symbol denote a single letter or block a that is popped into a word, we investigate, how many symbols are introduced in this way in one phase.At most one symbol is popped to the left and one to the right by BlockComp in line 3, the same holds for PreProc in line 4.Moreover, one symbol is popped to the left and one to the right in line 10; since this line is executed twice, this yields 8 symbols in total.Note that the symbols popped by BlockComp are replaced by single letters, so the claim in fact holds for letters as well.
So, consider any word A i ∈ Γ * (the proof for B j is the same), at the beginning of the phase and let A i be the corresponding word at the end of the phase.There were at most 8 symbols introduced into A i (some of them might be compressed later).On the other hand, by Lemma 9, at least |A i |−1 3 letters were removed A i due to compression.Hence It is easy to check that when A i is short, i.e. |A i | ≤ N = 100, then A i is short as well and when It is left to show that the first word shortens by at least one letter in each phase.Consider that if a letter a is left-popped from X then we created B 0 and in order to preserve (1) the first letters of B 0 and A 0 are removed.Thus, A 0 gained one letter on the right and lost one on the left, so its length stayed the same.Furthermore the right-popping does not affect the first word at all (as X is not to its left); the same analysis applies to cutting the prefixes and suffixes.Hence the length of the first word is never increased by popping letters.Moreover, if at least one compression (be it block compression or pair compression) is performed inside the first word, its length drops.So consider the first word at the end of the phase let it be A 0 .Note that there is no letter representing a compressed pair or block in A 0 : consider for the sake of contradiction the first such letter that occurred in the first word.It could not occur through a compression inside the first word (as we assumed that it did not happen), cutting prefixes does not introduce compressed letters, nor does popping letters.So in A 0 there are no compressed letters.But if |A 0 | > 1 then this contradicts Lemma 9. Now, consider a solution S(X).We know that S(X) is either a prefix of A 0 or of the form A 0 A, where A is a prefix of A 0 , see Lemma 2. In the former case, S(X) is compressed as a substring of A 0 .In the latter observe that argument follows in the same way, as long as we try to compress every pair of letters in S(X).So consider such a pair ab.If it is inside A 0 then we are done.Otherwise, a is the last letter of A 0 and b the first.Then this pair occurs also on the crossing between A 0 and X in A, i.e. ab is one of the crossing pairs.In particular, we try to compress it.So, the claim of the lemma holds for S(X) as well.
Lemma 11.For a ∈ Γ we can report all solutions in which S(X) = a for some natural in O(|A| + |B|) time.There is either exactly one for which S(X) = a is a solution or S(X) = a is a solution for each or there is no solution of this form.
Note that we do not assume that the first or last word is a block of as.
Proof.The algorithm and proof is similar as in Lemma 1.Consider a substitution S(X) = a .We calculate the length of the a-prefix of S(A) and S(B).Consider first letter other than a in A, let it be in the A k A and suppose that there were A letters a before it (if there is non such letter, imagine we attach an 'ending marker' to both A and B, which then becomes such letter).Then the length of the a-prefix of S(A) is k A • + A .Let additionally A be obtained from A by removing those letters a and variables in between them.Similarly, define k B , B and B .Then the length of the a-prefix of

and S(A ) = S(B ). Consider the number of natural solutions of the equation
no natural solution: clearly there is no solution of the word equation A = B; one solution x = : then S(X) = a is the only possible solution from a + of A = B. To verify whether S satisfies A = B we apply the same strategy as in TestSimpleSolution(a): we evaluate both sides of A = B under the substitution S(X) = a on the fly.The same argument as in Lemma 1 shows that the running time is linear in |A | + |B | satisfied by all natural numbers: then the a-prefixes of A and B are of the same length for each S(X) ∈ a * .We thus repeat the procedure for A = B , shortening them so that they obey the form (1), if needed.Clearly, solutions in a * of A = B are exactly the solutions of A = B in a * .The stopping condition for the recurrence above is obvious: if A and B are both empty then we are done (each S(X) = a is a solution of this equation), if exactly one of them is empty and the other is not then there is no solution at all.
Lastly, observe that the cost of the subprocedure above is proportional to the amount of read letters, which are then not read again, so the running time is O(|A| + |B|) Running time.Concerning the running time, we first show that one phase runs in linear time, which follows by standard approach, and then that in total the running time is O(n + # X log n).To this end we assign in a fixed phase to each A i word and B j word cost proportional to its length.For a fixed A i word the sum of costs assigned while it was long forms a geometric sequence, so sums up to at most constant more than the initial length of A i word; on the other hand the cost assigned when A i word is short is O(1) per phase ad there are O(log n) phases.

Lemma 12. One phase of OneVarWordEq can be performed in
Proof.For grouping of pairs and blocks we use RadixSort, to this end it is needed that the alphabet of (used) letters can be identified with consecutive numbers, i.e. with an interval of at most |A| + |B| integers.In the first phase of OneVarWordEq this follows from the assumption on the input. 1 At the end of this proof we describe how to bring back this property at the end of the phase.
To perform BlockComp we want for each letter a occurring in the equation to have lists of all maximal a-blocks occurring in A = B (note that after CutPrefSuff there are no crossing blocks, see Lemma 7).This is done by reading A = B and listing triples (a, k, p), where k is the length of a maximal block of as and p is a pointer to the beginning of this occurrence.Notice, that the maximal block of a's may consist also of prefixes/suffixes that were cut from X by CutPrefSuff.However, by Lemma 1 such a prefix is of length at most |A 0 | ≤ |A| + |B| (and similar analysis applies for the a suffix).Then each maximal block includes at most one such prefix and one such suffix thus the length of the a maximal block is at most 3(|A| + |B|).Hence, the triples (a, k, p) can be sorted by their first two coordinates using RadixSort in total time O(|A| + |B|).
After the sorting, we go through the list of maximal blocks.For a fixed letter a, we use the pointers to localise a's blocks in the rules and we replace each of its maximal block of length > 1 by a fresh letter.Since the blocks of a are sorted, all blocks of the same length are consecutive on the list, and replacing them by the same letter is easily done.
To compress all non-crossing pairs, i.e. to perform the loop in line 8, we do a similar thing as for blocks: we read both A and B, whenever we read a pair ab where a = b and both a and b are not letters that replaced blocks during the blocks compression, we add a triple (a, b, p) to the temporary list, where p is a pointer to this position.Then we sort all these pairs according to lexicographic order on first two coordinates, we use RadixSort for that.Since in each phase we number the letters occurring in A = B using consecutive numbers, this can be done in time O(|A| + |B|).The occurrences of the crossing pairs can be removed from the list: by Lemma 6 there are at most two crossing pairs and they can be easily established (by looking at A 0 XA 1 ).So we read the sorted list of pairs occurrences and we remove from it the ones that correspond to a crossing pair.Lastly, we go through this list and replaces pairs, as in the case of blocks.Note that when we try to replace ab it might be that this pair is no longer there as one of its letters was already replaced, in such a case we do nothing.This situation is easy to identify: before replacing the pair we check whether it is indeed ab that we expect there, as we know a and b, this is done in costant time.
We can compress each of the crossing pairs naively in O(|A| + |B|) time by simply first applying the popping and then reading the equation form the left to the right and replacing occurrences of this fixed pair.
It is left to describe, how to enumerate (with consecutive numbers) letters in Γ at the end of each phase.Firstly notice that we can easily enumerate all letters introduced in this phase and identify them (at the end of this phase) with {1, . . ., m}, where m is the number of introduced letters (note that none of them were removed during the WordEqSat).Next by the assumption the letters in Γ (from the beginning of this phase) are already identified with a subset of {1, . . ., |A| + |B|}, we want to renumber them, so that the subset of letters from Γ that are present at the end of the phase is identified with {m + 1, . . ., m + m } for an appropriate m .To this end we read the equation, whever we spot a letter a that was present at the beginning of the phase we add a pair (a, p) where p is a pointer to this occurrence.We sort the list in time O(|A| + |B|).From this list we can obtain a list of present letters together with list of pointers to their occurrences in the equation.Using those pointers the renumbering is easy to perform in O(|A| + |B|) time.
So the total running time is The amortisation, especially in the next section, is much easier to be shown when we know that both the first and last words are long.This assumption is not restrictive, as as soon as one of them becomes short, the remaining running time of WordEqSat is linear.

Lemma 13. As soon as first or last word becomes short, the rest of the running time of OneVarWordEq is O(n).
Proof.One phase takes O(|A| + |B|) time by Lemma 12 (this is at most O(n) by Lemma 10) and as Lemma 10 guarantees that both the first word and the last word are shortened by at least one letter in a phase, there will be at most N = O(1) many phases.Lastly, Lemma 11 shows that TestSolution also runs in O(n).
So it remains to estimate the running time until one of the last or first word becomes short.

Lemma 14. The running time of OneVarWordEq till one of first or last word becomes short is
Proof.By Lemma 12 the time of one iteration of OneVarWordEq is O(|A|+|B|).We distribute the cost among the A words and B words: we charge β|A i | to A i-word and β|B j | to B j-word, for appropriate positive β.Fix A i-word, we separately estimate how much was charged to it when it was a long and short word.long: Let n i be the initial length of A i-word.Then by Lemma 10 the length in the (k + 1)-th phase it at most ( 3 4 ) k n i and so these costs are at most βn i + 3 4 βn i + ( 3 4 ) 2 βn i + . . .≤ 4βn i .short: Since A i-word is short, its length is at most N , so we charge at most N β to it.Notice, that there are O(log n) iterations of the loop in total, as first word is of length at most n and it shortens by 3  4 in each iteration when it is long and we calculate only the cost when it is long.Hence we charge in this way O(log n) times, so in total O(log n).

Heuristics and Better Analysis
The intuition gained from the analysis in the previous section, especially in Lemma 14 is that the main obstacle in obtaining the linear running time is the necessity of dealing with short words, as the time spend on processing them is difficult to charge.This applies to both the compression performed within the short words, which does not guarantee any reduction in length, see Lemma 10, and to testing of the candidate solutions, which cannot be charged to the length decrease of the whole equation.
Observe that by Lemma 13 as soon as the first or last word becomes short, the remaining running time is linear.Hence, in our improvements of the running time we can restrict ourselves to the case, in which the first and last word are long.
The improvement to linear running time is done by four improvements in algorithm analysis and employed data structures, which are described in details in the following subsections: several equations: Instead of a single equation, we store a system of several equations and look for a solution of such a system.This allows removal of some words from the equations that always correspond to each other.and thus decreases the overall storing space and testing time.This is described in Section 4.2 and Section 4.4.small solutions: We identify a class of particularly simple solutions, called small, and show that a solution is reported within O(1) phases from the moment when it became small.In several problematic cases of the analysis we are able to show that the solutions involved are small and so it is easier to charge the time spent on testing them.Section 4.3 is devoted to this issue.storage: The storage is changed so that all words are represented by a structure of size proportional to the size of the long words.In this way the storage space decreases by a constant factor in each phase and so the running time (except for testing) is linear.This is explained in Section 4.4 testing: The testing procedure is modified, so that the time it spends on the short words is reduced.In particular, we improve the rough estimate that one TestSimpleSolution takes time proportional to the equation to an estimation that actually counts for each word whether it was included in the test or not.Section 4.5 is devoted to this. . .m] is the k-th suffix according to the lexicographical order.It is known that such an array can be constructed in O(m) time [9] assuming that RadixSort is applicable to letters, i.e. that they are integers from {1, 2, . . ., m c } for some constant c.We assume explicitly that this is the case in our problem.
Using a suffix array the equality testing for substrings of w reduces to the longest common prefix (lcp) query: observe that w[i . .i + k] = w[j . .j + k] if and only if the common prefix of w[i . .m] and w[j . .m] is at least k.The first step in constructing a data structure for answering such queries is the LCP array: for each i = 1, . . ., m − 1 the LCP [i] stores the length of the longest common prefix of SA [i] and SA[i + 1].Given a suffix array, the LCP array can be constructed in linear time [10], however, the linear-time construction of suffix arrays can be in fact extended to return also the LCP array [9].
When the LCP array is supplied, the general longest prefix queries reduce to the range minimum queries: the longest common prefix of SA[i] and SA[j] (for i < j) is the minimum among LCP [i], . . ., LCP [j − 1], and so it is enough to have a data structure that answers the queries about the minimum in the range in constant time.Such data structures in general case are known and in case of LCP arrays even simpler construction were given [1].The construction time is linear and query time is O(1) [1].Hence, after a linear preprocessing, we can calculate the length of the longest common prefix of two substrings of a given string in O(1) time.

Several equations.
The improved analysis assumes that we do not store a single equation, instead, we store several equations and look for substitutions that simultaneously satisfy all of them.Hence we have a collection A i = B i of equations, for i = 1, . . ., m, each of them is of the form described by (1); by A = B we denote the whole system of those equations.In particular, each of those equations specifies the first and last letter of the solution, length of the a-prefix and suffix etc., exactly in the same way as it does for a single equation.If there is a conflict, as two equations give different answers regarding the first/last letter or the length of the a-prefix or b-suffix, then there is no solution at all.Still, we do not check the consistency of all those answers, instead, we use an arbitrary equation, say A 1 = B 1 , to establish the first, last letter, etc., and as soon as we find out that there is a conflict, we stop the computation and terminate immediately.
The system of equations stored by OneVarWordEq is obtained by replacing one equation that in general the latter two equation are not equivalent to the former one, however, we perform the replacement only when they are; moreover, we need to trim them so that they satisfy the form (1)).
The described way of splitting the equations implies a natural order on the equations in the system: when moreover, they are both before/after each equation before/after which A i A i = B i B i was).This order is followed whenever we perform any operations on all words of the equations.We store a list of all equations, in this order.
We store each of those equations in the same way as described for a single equation in the previous phase, i.e. for an equation A i = B i we store a list of pointers to words on one side and list of pointers to words on the other side.Additionally, the first word of A i has a link to the last word of A i−1 and the last word of A i similarly, the last word of A i has a link to the first word of A i and the first word of A i+1 .We also say that A i (B j ) is first or last if it is in any of the stored equations.
All operations on a single equation introduced in the previous sections (popping letters, cutting prefixes and suffixes, pair compression, blocks compression) generalise to a system of equations.The running times are addressed in detail later on.Concerning the properties, they are the same, we list those for which the generalisation or the proof are non-obvious: PreProc should ensure that there are only two crossing pairs.This is the case, as each X in every equation is replaced by the same aXb and S(X) is the same for all equations, which is the main fact used in the proof of Lemma 6. Lemma 10 ensured that in each phase the length of the first and last word is decreased.Currently the first words in each equation may be different, however, the analysis in Lemma 10 applies to each of them.4.3.Small solutions.We say that a word w is almost periodic with period size p and side size s if it can be represented as w = w 1 w 2 w 3 (where is an arbitrary number), where |w 2 | ≤ p and |w 1 w 3 | ≤ s; we often call w 2 the periodic part of this factorisation.(Note that several such representation may exist, we use this notion for a particular representation that is clear from the context).A substitution S is small, if S(X) = (w) k v, where w, v are almost periodic with period and side sizes N .
The following theorem shows the main result of this section: if a solution is small, then it is reported by OneVarWordEq within O(1) phases.

Theorem 2. If S(X) is a small solution then OneVarWordEq reports it within O(1) phases.
We would like to note that the rest of the paper is independent from the proof of Theorem 2, so it might be skipped in reading.
Intuition is as follows: observe first that in each phase we make Pop and test whether S(X) = a, where a is a single letter, is a solution.Thus it is enough to show that a small solution is reduced to one letter within O(1) phases.To see this, consider first an almost periodic word, represented as w 1 w 2 w 3 .Ideally, all compressions performed in one phase of OneVarWordEq are done separately on w 1 , w 3 and each w 2 .In this way we obtain a string w 1 w 2 w 3 and from Lemma 9 it follows that w i is shorter than w i by a constant fraction.After O(log |w 2 |) steps we obtain a word w 1 w 2 w 3 in which w 2 is a single letter, and so in this phase w 2 is replaced with a single letter.Then, since the length of w 1 w 3 is at most N , after O(1) phases this is also reduced to a single letter.Concerning the small solution, w k v we first make such an analysis for w, when it is reduced to a single letter (after O(1) phases) after one additional phase w k = a k is also reduced to one letter (by BlockComp) and so the obtained string a k v is a concatenation of two almost periodic strings.Using the same analysis as above for each of them we obtain that it takes O(1) time to reduce them all to single letters.Thus we have a 2-letter string, which is reduced to a single letter within 2 phases.
In reality we need to take into the account that some compression are made on the crossings of the considered strings, however, we can alter the factorisation (into almost periodic words and almost periodic words into periodic part and rest) of the string so that the result is almost as in the idealised case.
We say that for a substring w of S(X) during one phase of OneVarWordEq the letters in w are compressed independently, if every compressed pair or block were either wholly within this w or wholly outside this w (in some sense this corresponds to the non-crossing compression).
The following lemma shows that given an almost periodic substring of S(X) with period size p and side size s we can find an alternative representation in which the period size is the same, side size increases (a bit) but each w in w k in this new representation is compressed independently.This shows that the intuition about shortening of almost periodic strings is almost precise -we can think that periodic part in almost periodic strings are compressed independently, but we need to pay for that by an increase in the side size.
Lemma 15.Consider almost periodic substring of S(X) with period size p and side size s represented as w 1 w 2 w 3 , where w is not a block of single letter.Then there is a representation of this string as • the form of w 2 depends solely on w 2 and does not depend on w 1 , w 3 (it does depend on the equation and on the order of blocks and pairs compressed by OneVarWordEq) • the compression in one phase of OneVarWordEq compresses each w from w independently.In particular, this other representation has period size p and side size s + 2p.
Proof.First of all, if ≤ 2 then we take w 2 = , k = 0 and concatenate w k 2 to w 1 to obtain w 1 (and take w 3 = w 3 ).So in the following we consider the case in which > 2 and set = − 2.
Let w 2 = a m zb r , where a, b ∈ Γ, m, r ≥ 1 and z ∈ Γ * does not start with a nor it ends with b, such a representation is possible, as w 2 is not a block of letters.Then w 1 w 2 w 3 = w 1 (a m zb r ) w 3 .Since w 1 can end with a and w 2 can begin with b, we are interested in compressions within the middle zb r (a m zb r ) −2 a m z.We first show that indeed there is a compression of a substring that is fully within the zb r (a m zb r ) −2 a m z: • If m > 1 or r > 1 then we compress the block a m or b r .
• If m = r = 1, a = b then ab = aa is a block and it is compressed.
• If m = r = 1, a = b and z = then this substring is b(ab) −2 a.As > 2 the pair ab is listed by OneVarWordEq and we try to compress it.If we fail then it means that one of the letters was already compressed with a letter inside the considered string.• If m = r = 1 and a = b and z = then ba is listed among the pairs and we try to compress the occurrence right after the first z.If we fail then it means that one of the letters was compressed with its neighbouring letter, which is also in the string.Consider the first substring that is compressed and it is wholly within zb r (a m zb r ) −2 a m z.There are two cases: the compressed substring is a block of letters or it is a pair.We give a detailed analysis in the latter case, the analysis in the former case is similar.
So, let the first pair compressed wholly within this fragment zb r (a m zb r ) −2 a m z be cd, see Fig. 1 for an illustration.We claim that all pairs cd that occurred within this fragment at the beginning of the phase are compressed at this moment.Assume for the sake of contradiction that this is not the case.So this means that one of the letters, say c was already compressed in some other compression performed earlier.By the choice of the compressed pair (i.e.cd), this c is compressed with a letter from outside of the fragment zb(a m zb r ) −2 az, there are two possibilities: c is the last letter of zb r (a m zb r ) −2 a m z: Observe that the letter succeeding c is either b or a letter representing a compressed pair/string.In the latter case we do not make a further compression, so it has to be b.This is a contradiction: each c that is a last letter of z was initially followed by b, and so in fact some compression of cb (note that by our choice the last letter of z was not b, and so b = c) was performed wholly within zb r (a m zb r ) −2 a m z and it was done earlier than the compression of cd, contradiction with the choice of cd.c is the first letter of zb r (a m zb r ) −2 a m z: The argument is symmetric, with a preceding c in this case.There are at least − 1 occurrences of cd that are separated by |w| − 2 letters, i.e. the (cdz ) −2 cd is a substring of zb r (a m zb r ) −2 a m z, for some z of length |w| − 2, see Fig. 1.We take w 2 = cdz and let w 1 be the w 1 concatenated with string proceeding the (cdz ) −2 cd and w 3 the w 3 concatenated with the string following this (cdz ) −2 (note that the latter includes the ending cd, see Fig. 1).Clearly Note that each w 2 begins with cd, which is the first substring even partially within w 2 that is compressed, furthermore, each of those w 2 is also followed by cd.So the compression inside each w 2 is done independently (because by the choice of cd there was no prior compression applied in w 2 ).
Concerning the analysis when the first compressed substring is some c m it can be routinely verified that there are no essential differences in the analysis.
The immediate consequence of Lemma 15 is that when an almost periodic string is a substring of S, then we can give bounds on the period size and side size on the corresponding word after one phase of OneVarWordEq.
Lemma 16.Consider an almost periodic substring of S(X) with period size p and side size s.Then the corresponding substring after the phase of OneVarWordEq has a factorisation with period size at most 3  4 p and side size at most 2 3 s + 7 3 p.There are two remarks: firstly, if period size of the original word was 1 then the given bound is 3 4 < 1, which holds, i.e. the corresponding word has no periodic part in the factorisation.Secondly, the first (last) letter of the substring may be compressed with the letter to the left (tight, respectively), so outside of the considered substring.In such a case we still include the letter representing the replaced pair or block in the corresponding substring.
Proof.Let us fix the factorisation w 1 w 2 w 3 of w, where p = |w 2 | is the period size and s = |w 1 w 3 | is the side size.First of all, consider the special case, in which w 2 is a block of letters, say a, without loss of generality we may assume that it is a single letter (note that this simple borderline case is not covered by Lemma 15).Without loss of generality we also may assume that w 1 does not end and w 3 does not begin with a, as otherwise we can move those letters to w 2 , decreasing the side size and not increasing the period size.Then during the block compression the w 2 = a is going to be replaced by a single letter (this block may also include some letters from outside of w, when w 1 or w 3 is empty, this does not affect the analysis).Now consider w 1 : its first letter can be compressed with letters outside it, otherwise each letter not compressed in the phase, except perhaps the last one, is followed by two letters that are, see Lemma 9. Hence at most 2 + |w 1 |−2 3 letters are uncompressed and so the length of the corresponding compressed w 1 is at most  Imagine now we want to make a similar refactoring also for the small word (in order to draw conclusions about shortening of S(X), which is small).So take w k v where both w and v are almost periodic words (with some period sizes and side sizes) and k is some number.When we look at w k , each single w can be refactored so that its periodic part is compressed independently.Note that this is the same word, i.e. we are still given w k v, though we have in mind a different factorisation of w.However, the compression of the w is influenced by the neighbouring letters, so while each of the middle w in w k−2 is compressed in the same way, both the first and the last w can be compressed differently.Hence, after the compression we obtain something of the form w 1 w k−2 w 2 v , where w 1 , w , w 2 , v are almost periodic.In the next phase the process continues and we accumulate almost periodic words on both sides of w k .So in general we deal with a word of the form uw k v, where w is almost periodic and u, v are concatenations of almost periodic words.The good news is that we can bound the sum of side sizes and period sizes of almost periodic words occurring in u, v.Moreover, the period size of w drops by a constant factor in each phase, so after O(1) phases it is reduced to 0, i.e. w k is almost periodic.
As a first technical step we show that Lemma 15 can be used to analyse what happens with a concatenation of almost periodic words in one phase of OneVarWordEq: as in the case of a single word, see Lemma 16, the sum of period sizes drops by a constant factor, while the sum of side sizes drops by a constant factor but it increases by magnitude of sum of period sizes.Lemma 17.Let u, a substring of S(X), be a concatenation of almost periodic words with a factorisation for which the sum of period sizes if p and side sizes is s.Then after one phase of OneVarWordEq the corresponding string u is a concatenation of almost periodic words with a factorisation for which the sum of period sizes is at most 3  4 p and sum of side sizes is at most 2 3 s + 7 3 p.Note that as in the case of Lemma 16 when sum of the period sizes is 1, then after one phase we are guaranteed that all almost periodic words in the factorisation have empty periodic parts.Moreover, as in the case of Lemma 16 the first and last letter of u may be compressed with the letters outside u, in which case we include in the corresponding word the letters that are obtained in this way.
So let us go back to the main case, in which w 1 w 3 = and k ≥ 2. Then uw k v = u(w 1 a w 3 ) k v = uw 1 a w 3 w 1 (a w 3 w 1 ) k−2 a w 3 v.As w 3 w 1 is non-empty and does not end, nor begin with a, each a in (a w 3 w 1 ) k−2 is compressed independently.We set u = uw 1 a w 3 w 1 , v = a w 3 v and w = a w 3 w 1 .Applying Lemma 17 to u and w yields that after one phase the sum of period sizes is 3  4 (p uv + 2p w ) while side size 2  3 (s uv + 2s w ) + 7 3 (p uv + 2p w ).On the other hand, the period size of w is 3 4 p w while its side size at most 2  3 s w + 7 3 p w With Lemma 18 established, we can prove Theorem 2.
proof of Theorem 2. Consider the string S(X).We show that within O(log N ) = O(1) this string is reduced to a single letter.This means that S(X) is reported in the same time.Note that in the following phases the corresponding solution (if unreported) is not the corresponding string, as we also pop letters from X.However, the corresponding solution is the substring of this string.So fix a small solution and its occurrence within S(A).It can be represented as w k v, where w and v are almost periodic with period and side size N We claim that in each following phase the corresponding string can be represented as u w k v , where u and v are concatenations of almost periodic words, the sum of their period sizes is at most 6N while side sizes 78N .Also, w is almost periodic with side size at most 11N and period size dropping by 3  4 in each phase (and at most N at the beginning).This claim can be easily verified by induction on the estimations given by Lemma 18.As the period size of w drops by 3  4 in each phase and initially it is N , after O(log N ) phases w has period size 0. Then inside u w k v we treat w k as a periodic word with period size |w | ≤ 11N and side size 0. Thus u w k v is a concatenation of almost periodic words with sum of period sizes at most 17N and sum of side size at most 78N .Then, by easy induction on bounds given by Lemma 17, in the following phases the corresponding string will be a concatenation of almost periodic strings, with sum of period sizes decreasing by 3  4 in each phase (and initial value 17N )and sum of side sizes at most 78N .Thus after O(log N ) phase its sum of period sizes is reduced to 0 and so it is a string of length at most 78N , which will be reduced to a single letter within O(log N ) rounds, as claimed.Since N = O(1).

Storing of an equation.
To reduce the running time we store duplicates of short word only once.Recall that for each equation we store lists of pointers pointing to strings that are the explicit words in this equation.We store the long words in a natural way, i.e. each long word is represented by a separate string.The short words are stored more efficiently: if two short words in equations are equal we store only one string, to which both pointers point.In this way all identical short words are stored only once (though each of them has a separate pointer pointing to it); we call such a representation succinct.
We show that the compression can be performed on the succinct representation, without the need of reading the actual equation.This allows bounding the running time using the size of the succinct representation and not the equation.
We distinguish two types of short words: those that are substrings of long words (normal) and those that are not (overdue).We can charge the cost of processing the normal short words to the time of processing the long words.The overdue words can be removed from the equation after O(1) phases after becoming overdue, so their processing time is constant per A-i word (or B-j word).
The rest of this subsection is organised as follows: • We first give precise details, how we store short and long words, see Section 4.4.1 and prove that we can perform compression using only succinct representation, see Lemma 19.• We then define precisely the normal and overdue words, see Section 4.4.2 as well as show that we can identify new short and overdue words, see Lemma 21.Then we show that overdue words can be removed O(1) phases after becoming overdue, see Lemma 22 and 23.• Lastly, in Section 4.4.3,we show that the whole compression time, summed over all phases is O(n).The analysis is done separately for long words normal short words and overdue short words.
As observed at the beginning of Section 4, as soon as the first or last word becomes short, the remaining running time is linear.Thus, when such a word becomes short, we drop our succinct representation and recreate out of it the simple representation used in Sections2-3.Such a recreation takes linear time.
4.4.1.Storing details.We give some more details about the storing: All long words are stored on two doubly-linked lists, one representing the long words on the left-hand sides and the other the long words on the right-hand sides.Those words are stored on the lists according to the initial order of the words in the input equation.Furthermore, for each long word we store additionally, whether it is a first or last word of some equation (note that a short word cannot be first or last).The short words are also organised as a list, the order on the list is irrelevant.Each short word has a list of its occurrences in the equations, the list points to the occurrences in the natural order (occurrences on the left-hand sides and on the right-hand sides are stored separately).
We say that such a representation is succinct and its size is the sum of lengths of words stored in it (so the sum of sizes of long words, perhaps with multiplicities, plus the sum of sizes of different short words).Note that we do not include the number of pointers from occurrences of short words.We later show that in this way we do not need to actually read the whole equation in order to compress it; it is enough to read the words in the succinct representation, see Lemma 20.We now show that such a storage makes sense, i.e. that if two short words become equal, they remain equal in the following phases (note again that none of them are first, nor last).

Lemma 19. Consider any explicit words A and B in the input equation. Suppose that during OneVar-WordEq they were transformed to A = B , none of which is a first or last word in one of the equations. Then A = B if and only if
Proof.By induction on operation performed by OneVarWordEq.Since none of the A , B is the first or last word in the equation, it means that during the whole OneVarWordEq they had X to the left and to the right.So whenever a letter was left-popped or right-popped from X, it was prepended or appended to both A and B; the same applies to cutting prefixes and suffixes.Compression is never applied to a crossing pair or a crossing block, so after it two strings are equal if and only if they were before the operation.The removal of letters (in order to preserve (1)) is applied only to first and last words, so it does not apply to words considered here.Partitioning the equation into subequations does not affect the equality of explicit words.
We now show the main property of succinct representation: the compression (both pair and block) can be performed on succinct representation in linear time.

Lemma 20. The compression in one phase of OneVarWordEq can be performed in time linear in size of the succinct representation.
Proof.The long words are stored in a list and we can compress them without the need of reading the word table.We know which one of them is first or last, so when letters are popped from X we know what letters are appended/prepended to each of those words.Since they are stored explicitly, the claim for them follows from the analysis of the original version of OneVarWordEq, see Lemma 12.This analysis in particular requires that we can identify the letters used in the equation with numbers from an interval of linear size.Here the size is the size of the succinct representation.Note though that this part of the proof follows in the same way: when listing letters (to replace them with new ones) we do not need to list letters in different occurrences of the same short word, it is enough to do this once, which can be done using the succinct representation.
For the short words stored in the list of short words, from Lemma 19 it follows that if an explicit word A occurs twice in the equations (both times not as a first, nor last word of the equation) it is changed during OneVarWordEq in the same way at both those instances.So it is enough to perform the operations on the words stored in the list, doing so as in the original version of OneVarWordEq takes time linear in the size of the tables of short words, as in Lemma 12. 4.4.2.Normal and overdue short words.The short words stored in the tables are of two types: normal and overdue.The normal words are substrings of the long words or A 2 0 and consequently the sum of their sizes is proportional to the size of the long words.A word becomes overdue if at the beginning of the phase it is not a substring of a long word nor A 2 0 .It might be that it becomes a substring of such a word later, it does not stop to be an overdue word in such a case.
Since the normal words are of size O(N ) = O(1), the sum of lengths of normal words stored in short word list is at most O(1) larger than the sum of sizes of the long words.Hence the processing time of normal short words can be charged to the long words.For the overdue words the analysis is different: we show that after O(1) phases we can remove them from the equation (splitting the equations).Thus their processing time is O(1) per A-i word (or B-j word) and thus O(n) in total.The new overdue words can be identified in linear time: this is done by constructing a suffix array for a concatenation of long and short words occurring in the equations.
Lemma 21.In time proportional to the size of succinct representation size we can identify the new overdue words.
Proof.Consider all long words A 0 , . . ., A m (with or without multiplicities, it does not matter) and all short (not already overdue) words A 1 , . . .A m , without multiplicities; in both cases this is just a listing of words stored in the representation (except for old overdue words).We construct a suffix array for the string As it was already observed that the size of the alphabet is linear in the size of the succinct representation, the construction of the suffix array can be done in linear time [9].Now A i is a factor in some A j (the case of A 2 0 is similar, it is omitted to streamline the presentation) if and only if for some suffix A j of A j the strings In terms of a suffix array, the entries for Recall that the length of the longest common prefix of two suffixes stored at positions p < p in the suffix array is the minimum of where A j is a suffix of some long word A j ) with which it has the longest common prefix.As the length of the common prefix of pth and p th entry in a suffix array is min(LCP [p], LCP [p + 1], . . ., LCP [p − 1]), this is is either the first previous or first next suffix of this form in the suffix array.Thus the appropriate computation can be done in linear time: we first go down in the suffix array, storing the last spotted entry corresponding to a suffix of some long A j , calculating the LCP with consecutive suffixes and storing them for the suffixes of the form A i $ . . .$A m #.We then do the same going from the bottom of the suffix array.Lastly, we choose the larger from two stored values; for A i $ . . .$A m # it is smaller than |A i | if and only if A i just became an overdue word.
Concerning the running time, it linearly depends on the size of the succinct representation and alphabet size, which is also linear in size of succinct representation, as claimed.
The main property of the overdue words is that they can be removed from the equations in O(1) phases after becoming overdue.This is shown by a serious of lemmata.
First we need to define what does it mean that for solution word A in one side of the equation is at the same position as its copy on the other side of the equation: we say that for a substitution S the explicit word A i (or its subword) is arranged against the explicit word B j (S(X) for some fixed occurrence of X) if the position within S(A k ) occupied by this explicit word A i (or its subword) are within the positions occupied by explicit word B j (S(X), respectively) in B k .
Lemma 22. Consider a short word A in a phase in which it becomes overdue.Then for each solution S(X) either S is small or in every S(A k ) = S(B k ) each explicit word A i equal to A is arranged against another explicit word B j equal to A.
Proof.Consider an equation and a solution S such that in some S(A i ) = S(B i ) an explicit word A i (equal to an overdue word A) is not arranged against another explicit word equal to A. There are three cases: A is arranged against S(X).Note that in this case A is a substring of S(X).Either S(X) is a substring of A 0 or S(X) = A k 0 A 0 , where A 0 is a prefix of A 0 .In the former case A is a factor of A 0 , which is a contradiction, in the latter it is a factor of A k+1 0 .As A 0 is long and A short, it follows that |A| < |A 0 | and so A is a factor of A 2 0 , contradiction with the assumption that A is overdue.
Subword of A i is arranged against S(X).The overlapping S(X) are in in grey, the S(X) has a period shorter than A i .
A is arranged against some word.Since A is an overdue word, this means that A i is arranged against a short word B j .Note that both A i and B j are preceded and succeeded by S(X), since A i = B j we conclude that S(X) has a period at most |B j | − |A i |, see Fig. 2; in particular S is small.Other case.Since A i is not arranged against any word, nor arranged against S(X), it means that some substring of A i is arranged against S(X) and as A i is preceded and succeeded by S(X), this means that either S(X) is shorter than A i or it has a period at most |A|, see Figure 3 and 4, respectively.In both cases S is small.
Observe that due to Theorem 2 and Lemma 22 the A-i-words and B-j-words that are overdue can be removed in O(1) phases after becoming overdue: suppose that A becomes an overdue word in phase .Any solution, in which an overdue word A is not arranged against another occurrence of A is small and so it is reported after O(1) phases.Consider an equation A i = B i in which A occurs.Then the first occurrence of A in A i and the first occurrence of A in B j are arranged against each other for each solution S. In particular, we can write , where A i and B i do not have A as an explicit word (recall that A is not the first, nor the last word in A i = B i ).This equation is equivalent to two equations A i = B i and A i = B i .This procedure can be applied recursively to A i = B i .In this way, all occurrences of A are removed and no solutions are lost in the process.There may be many overdue strings so the process is a little more complicated, however, as each word can be removed once during the whole algorithm, in total it takes O(n) time.
Lemma 23.Consider the set of overdue words introduced in phase .Then in phase + O(1) we can remove all occurrences of overdue words from the equations.The obtained set of equations has the same set of solutions.The amortised time spend on removal of overdue words, over the whole run of OneVarWordEq, is O(# X ).
Proof.Consider any word A that become overdue in phase and any solution S of this equation, such that in some S(A i ) = S(B i ) the explicit word A is not arranged against another instance of the same explicit word.Then due to Lemma 22 the S(X) is small.Consequently, from Theorem 2 this solution is reported before phase + c, for some constant c.So any solution S in phase + c corresponds to a solution S from phase that had each explicit word A arranged in each S(A i ) = S(B i ) against another explicit word A. Since all operations in a phase either transform solution, implement the pair compression of implement the blocks compression for a solution S(X), it follows that in phase + c the corresponding overdue words A are arranged against each other in S (A i ) = S (B i ).Moreover, by Lemma 19 each explicit word A in this phase corresponds to an explicit word A in phase .This observation allows removing all overdue words introduced in phase .Let C 1 , C 2 , . . ., C m (in phase + c) correspond to all overdue words introduced in phase .By Lemma 21 we have already identified the overdue words.Using the list of short words, for each overdue word C, we have the list of pointers to occurrences of C in left-hand sides of the equations and right-hand sides of the equations, those lists are sorted according to the order of occurrences.In phase + c we go through those lists, if the first occurrences of A in the left-hand sides and right-hand sides are in different equations then the equations are not satisfiable, as this would contradict that in each solution both A is arranged against its copy.Otherwise, they are in the same equation A i = B i , which is of the form A i XAXA i = B i XAXB i , where A i and B i do not have any occurrence of A within them.We X X B j B j Figure 5.Let B j and B j both have their letters arranged against letters from fixed occurrence of X.Then the X separating them is a proper substring of another X, contradiction.split A i = B i into two equations A i = B i and A i = B i and we trim them so that they are in the form described in (1).The new equations have exactly the same set of solutions as the original one.
Note that as new equations are created, we need to reorganise the pointers from the first/last words in the equations, however, this is easily done in O(1) time.The overall cost can be charge to the removed X, which makes in total at most O(# X ) cost.Proof.By Lemma 20 the cost of compression is linear in terms of the size of the succinct representation by Lemma 21 in the same time bounds we can also identify the overdue words.Lastly, by Lemma 22 the total cost of removing the overdue words is O(n).So it is enough to show that the sum of sizes of the succinct representations summed over all phases is O(n).
When the overdue words are excluded, the size of the succinct representation is proportional to the total length of long words.Since by Lemma 10 this sum of lengths decreases by a constant in each phase, the sum of those costs is linear in n.
Concerning the costs related to the overdue words: Note that an A i-word or B j-word is overdue for only O(1) phases, after which it is deleted from the equation see Lemma 23.So in O(1) phases it is charged O(N ) = O(1) cost, during the whole run of OneVarWordEq.Summing over all A i-words and B j-words yields O(n) time.4.5.Testing.We already know that thanks to appropriate storing the compression of the equations can be performed in linear time.It remains to explain how to test the solutions fast, i.e. how to perform TestSimpleSolution when all first and last words are still long.
Recall that TestSimpleSolution checks whether S,which is of the form S(X) = a for some , is a solution by comparing S(A i ) and S(B i ) letter by letter, replacing X with a on the fly.We say that in such a case a letter b in S(A i ) is tested against the corresponding letter in S(B i ).Note that during the testing we do not take advantage of the smaller size of the succinct representation, so we need to make a separate analysis.Consider two letters, from A i and B j , that are tested against each other.If one of A i and B j is long, this can be amortised against the length of the long word.The same applies when one of the words A i+1 or B j+1 is long.So the only problematic case is when all of those words are short.To deal with this case efficiently we distinguish between different test types, in which we exploit different properties of the solutions to speed up the tests.In the end, we show that the total time spent on testing is linear.
For a substitution S by a mismatch we denote the first position on which S is shown not be a solution, i.e. sides of the equation have different letters (we use a natural order on the equations); clearly, a solution has no mismatch.Furthermore, OneVarWordEq stops the testing as soon as it finds a mismatch, so in the rest of this section, if we use a name test for a comparison of letters, this means that the compared letters are before the mismatch (or that there is no mismatch at all).
There are two preliminary technical remarks: First we note that for when testing a substitution S, for a fixed occurrence of X there is at most explicit word whose letters are tested against letters from this occurrence of X.
Lemma 25.Fix a tested substitution S and an occurrence of X in the equation.Then there is at most one explicit word whose letters are arranged against letters from this fixed occurrence of S(X).
Proof.Without loss of generality assume that X occurs within A in an equation A = B .Suppose that B j and B j (for j > j) have their letters arranged against a letter from this fixed occurrence of S(X), see Fig 5 .But B j and B j are separated by at least one X in the equation, and whole this X is also arranged against this fixed occurrence of X, contradiction.
As a second remark, observe that tests include not only explicit letters from S(A ) and S(B ) but also letters from S(X).In the following we will focus on tests in which at least one letter comes from an explicit word.It is easy to show that the time spent on other tests is at most as large as time spent on those tests.This follows from the fact that such other tests boil down to comparison of long blocks of a and the previous test is of a different type, so we can account the comparison between two long blocks of a to the previous test.However, our fast testing procedures in some times makes a series of tests in O(1) time, so this argument can be made precise only after the explanation of the details of various testing optimisations.For this reason the proof of Lemma 26 is delayed till the end of this section.
Lemma 26.Suppose that we can perform all tests in which at least one letter comes from an explicit word in O(n) time.Then we can perform all test in O(n) time.
Thus, in the following section we consider only the tests in which at least one letter comes from an explicit word.4.5.1.Test types.Suppose that for a substitution S a letter from A i is tested against a letter from S(XB j ) or a letter from B j is tested against a letter from S(XA i ) (the special case, when there is no explicit word after X is explained later).We say that this test is: protected: if at least one of A i , A i+1 , B j , B j+1 is long; failed: if A i , A i+1 , B j and B j+1 are short and a mismatch for S is found till the end of A i+1 or B j+1 ; aligned: if A i = B j and A i+1 = B j+1 , all of them are short and the first letter of A i is tested against the first letter of B j ; misaligned: if all of A i , A i+1 , B j , B j+1 are short, A i+1 = A i or B j+1 = B j and this is not an aligned nor failed test; periodical: if A i+1 = A i , B j+1 = B j , all of them are short and this is not an aligned nor failed test.This classification does not apply to the case, when a letter from A i is tested against letter from X that is not followed by an explicit word.There are two cases: • If A i is not followed by X in the equation then A i is a last word, in particular it is long.Therefore this test is protected.• If A i is followed by X then there is a mismatch till the end of A i X, so this test is failed.Observe that 'failed test' does not mean a mismatch, just a fact that soon there will be a mismatch.The protected, misaligned and failed tests are done in a letter-by-letter way, while the aligned and periodical tests are made in larger groups (in O(1) time per group, this of course means that we use some additional data structures).
It is easy to show that there are no other tests, see Lemma 27.We separately calculate the cost of each type of tests.As some tests are done in groups, we distinguish between number of tests of a particular type (which is the number of letter-to-letter comparisons) and the time spent on test of a particular type (which may be smaller, as group of tests are performed in O(1) time); the latter includes also the time needed to create and sustain the appropriate data structures.
For failed tests note that they take constant time per phase and we know that there are O(log n) phases.For protected tests, we charge the cost of the protected test to the long word and only O(|C|) such tests can be charged to one long word C in a phase.On the other hand, each long word is shortened by a constant factor in a phase, see Lemma 10, and so this cost can be charged to those removed letters and thus the total cost of those tests (over the whole run of OneVarWordEq) is O(n).
In case of the misaligned tests, it can be shown that S in this case is small and that it is tested at the latest O(1) phases after the last of A i+1 , A i , B i+1 , B i becomes short, so this cost can be charged to, say, B i becoming short and only O(1) such tests are charged to this B i (over the whole run of the algorithm).Hence the total time of such tests is O(n).
For the aligned tests, consider the consecutive aligned tests, they correspond to comparison of A i XA i+1 . . .A i+k X and B j XB j+1 . . .B j+k X, where A i+ = B j+ for = 1, . . ., k.So to perform them efficiently, it is enough to identify the maximal (syntactically) equal substrings of the equation and from Lemma 19 it follows that this corresponds to the (syntactical) equality of substrings in the original equation.Such an equality can be tested in O(1) using a suffix array constructed for the input equation (and general lcp queries on it).To bound the total running time it is enough to notice that the previous test is either misaligned or protected.There are O(n) such tests in total, so the time spent on aligned tests is also linear.
For the periodical test suppose that we are to test the equality of (suffix of) S((A i X) ) and (prefix of) S(X(B j X) k ).If |A i | = |B j | then the test for A i+1 and B j+1 is the same as for A i and B j and so can be skipped.If |A i | > |B j | then the common part of S((A i X) ) and S(X(B j X)  1) time.It might be that this test is failed (again, some of the words A i+1 , B j , B j+1 may not exist), too see this we need to make some look-ahead tests, but this can be done in O(N ) time (we do not treat those look-aheads as tests, so there is not recursion here).
Otherwise, if the first letter of A i and B j are tested against each other and A i = B j and A i+1 = B j+1 then the test is aligned (clearly this can be established in O(1) time using look-aheads).Otherwise, if A i+1 = A i or B j+1 = B j then it is misaligned (again, O(1) time for look-aheads).In the remaining case A i+1 = A i and B j+1 = B j , so this is a periodical test.Proof.As noticed, there are O(1) substitutions tested per phase.Suppose that the mismatch is for the letter from A i and a letter from XB j (the case of XA i and B j is symmetrical).Then the failed tests include at least one letter from XA i−1 XA i or XB j−1 XB j X, assuming they come from a short word.There are at most 4N failed tests that include a letter from A i−1 , A i , B j−1 , B j (as the test is failed then in particular this explicit word is short).Concerning the tests including the short occurrences of X in-between them, observe that by Lemma 25 each such X can have tests with at most one short word, so this gives additional 5N tests.Since N = O(1), we conclude that there are O(1) failed tests per phase and so O(log n) failed tests in total, as there are O(log n) phases, see Lemma 10.

Protected tests.
As already claimed, the total number of protected tests is linear in terms of length of long words: to show this it is enough to charge the cost of the protected test to the appropriate long word and see that a long word A can be charged only |A| such tests for test including letters from A and O(1) letters from neighbouring short words, which yields O(|A|) tests.As the length of the long words drops by a constant factor, summing this up over all phases in which this explicit word is long yields O(n) tests in total.
Lemma 29.In one phase the total number of protected tests is proportional to the length of the long words.In particular, there are O(n) such test during the whole run of OneVarWordEq.
Proof.As observed in Lemma 26 we can consider only tests in which at least one letter comes from an explicit word.Suppose that a letter from A i takes part in the protected test (the argument for a letter from B j is similar, it is given later on) and it is tested against a letter from XB j , then one of A i , A i+1 , B j , B j+1 is long, we charge the cost according to this order, i.e. we charge it to A i if it is long, if A i is not but A i+1 is long, we charge it to A i+1 , if not then to B j if it is long and otherwise to B j+1 .The analysis and charging for a test of a letter from B j is done in a symmetrical way (note that when the test includes two explicit letters, we charge it twice, but this is not a problem).form w k u, where |w| = a − b and |u| < a − b.Now, the prefix of S(X) of length x − p + a is of the form w k u , for some u of length less than a (as this is a prefix of length x − p + a − b extended by the following b letters).When we compare the positions of S(X) preceding B j and the one succeeding A i we obtain that S(X) has a period x − p + a so the whole S(X) is of the form (w k u ) w , where w is a prefix of w k u , hence S is small: w and u are of length at most N , as w is a prefix of w k u, either it is a prefix of w k , so it is of the form w k w where w is a prefix of w, or it includes the whole w k , so it is of the form w k u , where u is a prefix of u.
Consider the symmetric case, in which b > a and again use Fig. 7.The same argument as before shows that p and p − a + b are periods of S(X) and the corresponding borders are of length x − p and x − p + a − b.Now, the shorter of them (of length x − p + a − b) is a border of longer of them (of length x − p), so the prefix of length x − p of S(X) has a period b − a, so it is of the form w k u, where |w| = b − a and |u| < b − a. Hence the prefix of length x − p + a is of the form w k u for some u of length less than b.As in the previous case, S(X) has a period x − p + a and so the whole S(X) is of the form (w k u ) w , where w is a prefix of w k u , hence S is small.
Consider now the last case, in which | and we can repeat the same argument as above, with B j and A i+1 taking the roles of A i and B j , which shows that S is small.So consider the case in which then again, repeating the argument as above for A i+1 and B j+1 yields that S is small.So we are left with the case in which Then A i+1 is arranged against the same letters in S(X) as A i and B j+1 is arranged against the same letters in S(X) as B j .As there is no mismatch till the end of A i+1 and B j+1 , we conclude that A i+1 = A i and B j+1 = B j contradicting the assumption that A i and B j are misaligned, so this case is non-existing.
We now show that if A i and B j are misaligned for S then they were (for a corresponding solution) in the previous phase (assuming that all involved words were short).This is an easy consequence of the way explicit words are modified (we prepend and append the same letters and compress all explicit words in the same way).
Lemma 32.Suppose that A i and B j are misaligned for a solution S. If at the previous phase all A i+1 , A i , B j+1 and B j were short then A i and B j were misaligned for the corresponding solution S .
Proof.We verify the conditions on misaligned words point by point: • Since S is a solution, there is no mismatch.
• By the assumption, all A i+1 , A i , B j+1 and B j are short.
• We know that either A i = A i+1 or B j = B j+1 and so by Lemma 19 either A i = A i+1 or B j = B j+1 (observe that none of them is the last nor first, as they are all short).• Suppose that A i = B j , A i+1 = B j+1 and under S the first letters of A i and B j are arranged against each other.By Lemma 19 it follows that A i = B j , A i+1 = B j+1 .Observe that leftpopping and right popping preserves the fact that the first letters of A i-word and B j-word are arranged against each other for S (as S(A) and S (A ) are the same words).(asS(A) and S (A ) are the same words) As S is a solution, the same applies to pair compression and block compression.Hence, the first letters of A i and B j are arranged against each other, contradiction with the assumption that A i and B j are misaligned.• Suppose that the first letter of A i is arranged against a letter from S(XB j ).Consider, how A i and XB j under S are transformed to A i and XB j under S.As in the above item, popping letters does not influence whether the first letter of A i-word is arranged against letter from S(X) and B j-word (as S(A) and S (A ) are the same words).Since S is a solution, the same applies also to pair and block compression.So the position of the first letter of A i is among the position of S(XB j ) if and only if the first letter of A i is arranged against a letter from S (XB j ).
The case in which the position of the first letter of B j is among the position of S(XA i ) is shown in a symmetrical way.Now we are ready to give the improved procedure for testing and estimate the number of the misaligned tests in it.
Lemma 33.There are O(n) misaligned tests during the whole run of OneVarWordEq.
Proof.Consider a tested solution S and a misaligned test for a letter from A i against a letter from XB j (the case of test of letters from B j tested against XA i the argument is the same).Let be the number of the first phase, in which all A i-word, A i + 1-word, B j-word and B j + 1-word are short.We claim that this misaligned test happens between -th and + c phase, where c is the O(1) constant from Theorem 2.
Let A i and B j be the corresponding words in the phase .Using induction on Lemma 32 it follows that A i and B j are misaligned for S .Thus by Lemma 31 the S is small and thus by Theorem 2 it is reported till phase + c.So it can be tested only between phases and + c, as claimed.
This allows an improvement to the testing algorithm: whenever (say in phase ) a letter from A i has a misaligned test against a letter from S(XB j ) we can check (in O(1) time), in which turn the last among A i-word, A i + 1 − word, B j-word and B j + 1 word became small (it is enough to store for each explicit word the number of phase in which it became small).If + c < then we can terminate the test, as we know already that S is not a solution.Otherwise, we continue.
Concerning the estimation of the cost of the misaligned tests (in the setting as above), there are two cases: The misaligned tests that lead to the rejection of S: This can happen once per tested solution and there are O(log n) tested solution in total (O(1) per phase and there are O(log n) phases).Other misaligned tests: The cost of the test (of a letter from A i tested against S(XB j )) is charged to the last one among A i-word, A i + 1-word, B j-word and B j + 1-word that became short.By the argument above, this means that this word became short within the last c phases.Let us calculate, for a fixed A i word (the argument for B j-word is symmetrical) how many aligned tests of this kind can be charged to this word.They can be charged only within c phases after this word become short.In a fixed phase we test only a constant (i.e. 5) substitutions.For a fixed substitution, A i can be charged the cost of tests in which letters from A i or A i−1 are involved (providing that A i /A i−1 is short), which is at most 2N .They can be charged also the tests from letters from B j that is aligned against X proceeding A i−1 or X proceeding A i (providing that B j as well as A i−1 are short).Note that there is only one B j whose letter are aligned against X proceeding A i−1 and one for X proceeding A i , see Lemma 25, so when they are short this gives additional 2N tests.This yields that one A i word is charged O(N ) = O(1) tests in total.Summing over all words in the instance yields the claim of the lemma.4.5.5.Aligned tests.Suppose that we make an aligned test, without loss of generality consider the first such test in a sequence of aligned tests.Let it be between the first letter of A i and the first letter in B j (both of those words are short).For this A i and B j we want to perform the whole sequence of successive aligned tests at once, which corresponds of jumping to A i+k and B j+k within the same equation such that • A i+ = B i+ for 0 ≤ < k; • A i+k = B j+k or one of them is long or A i+k X or B j+k X ends one side of the equation.Note that this corresponds to a syntactical equality of fragments of the equation, which, by Lemma 19 is equivalent to a syntactical equality of fragments of the original equation.We preprocess (in O(n) time) the input equation (building a suffix array equipped with a structure answering general lcp queries) so that in O(1) we can return such k as well as the links to A i+k and B j+k .In this way we perform all equality tests for A i XA i+1 X . . .A i+k−1 X = B j XB j+1 X . . .XB j+k−1 X in O(1) time.
To simplify the considerations, when A i X (B j X) ends one side of the equation, we say that this A i (B j , respectively) is almost last word.Observe that in a given equation exactly one side has a last word and one an almost last word.
Lemma 34.In O(n) we can build a data structure which given equal A i and B j in O(1) time returns the smallest k ≥ 1 and links to A i+k and B j+k such that A i+k = B j+k or one of A i+k , B j+k is a last word or one of A i+k , B j+k is an almost last word.
Note that it might be that some of the equal words A i+ = B i+ are long, and so their tests should be protected (also, the tests for some neighbouring words).So in this way we also make some free protected tests, but this is not a problem.Furthermore, the returned A i+k and B j+k are guaranteed to be in the same equation.
Proof.First of all observe that for A i and B j it is easy to find the last word in their equation as well as the almost last word of the equation: when we begin to read a particular equation, we have the link to both the last word and the almost last word of this equation and we can keep them for the testing of this equation.We also know the numbers of those words so we can also calculate the respective candidate for k.So it is left to calculate the minimal k such that A i+k = A j+k .
Let A i , B j etc. denote the corresponding original words of the input equation.Observe that by Lemma 19 it holds that A i+ = B j+ if and only if A i+ = B j+ as long as none of them is last or first word.Hence, it is enough to be able to answer such queries for the input equation: if the returned word is in another equation then we should return the last or almost last word instead.
To this end we build a suffix array [9] for the input equation, i.e. for A 1 XA 2 X . . .A n A XB 1 XB 2 X . . .B n B $. Now, the lcp query for suffixes A i . . .$ and B j . . .$ returns the length of the longest common prefix.We want to know what is the number of explicit words in the common prefix, which corresponds to the number of Xs in this common prefix.This information can be easily preprocessed and stored in the suffix array: for each position in A 1 XA 2 X . . .A n A XB 1 XB 2 X . . .B n B $ we store, how many Xs are before it in the string and store this in the table prefX.Then when for a suffixes beginning at positions p and p we get that their common prefix is of length , the prefX[p + ] − prefX[p] is the number of Xs in the common prefix in such a case.If none of A i , A i+1 , . . ., A i+k nor B j , B j+1 , . . ., B j+k is the last word nor it ends the equation (i.e. they are all still in one equation) by Lemma 19 the k is the answer to our query (as A i = B j , A i+1 = B j+1 ,. . .and A i+k = B j+k and none of them is a last word, nor none of them ends the equation).To get the actual links to those words, at the beginning of the computation we make a table, which for each i return the pointer to A i-word and B i-word has the link to this word.As we know i, j and k we can obtain the appropriate links in O(1) time.So it is left to compare the value of k with the value calculated for the last word and almost last word and choose the one with smaller k and the corresponding pointers.
Using this data structure we perform the aligned tests is in the following way: whenever we make an aligned test (for the first letter of A i and the first letter of B j ), we use this structure, obtain k and jump to the test of the first letter of A i+k with the first letter of B j+k and we proceed with testing from this place on.Concerning the cost, by easy case analysis it can be shown that the test right before the first of sequence of aligned tests (so the test for the last letters of A i−1 and B j−1 ) is either protected or misaligned.There are only O(n) such tests (over the whole run of OneVarWordEq), so the time spend on aligned tests is O(n) as well.
Lemma 35.The total cost aligned test as well as the usage of the needed data structure is O(n).
Proof.We formalise the discussion above.In O(1) we get to know that this is an aligned test, see Lemma 27.Then in O(1), see Lemma 34, we get the smallest k such that A i+k = B j+k or one of them is an almost last word for this equation or the last word for this equation.We then jump straight to the test for the first letter of A i+k and B j+k .
Consider A i−1 and B j−1 we show that the test for their last letters (so the test immediately before the first aligned one) is protected or misaligned.By Lemma 27 it is enough to show that it is not aligned, nor periodic, nor failed.
• If it were failed then also the test for the first letters of A i and B j would be failed.
• It cannot be aligned, as we chose A i and B j as the first in a series of aligned tests.
• If it were periodic, then A i−1 = A i and B j−1 = B j while by assumption A i = B j , which implies that this test is in fact aligned, which was already excluded.Hence we can associate the O(1) cost of whole sequence of aligned test to the previous test, which is misaligned or protected.Clearly, one misaligned or protected test can be charged with only one sequence of aligned tests (as it is the immediate previous test).By Lemma 29 and 33 in total there are O(n) misaligned and protected tests.Thus in total all misaligned tests take O(n) time.4.5.6.Periodical tests.The general approach in case of periodical tests is similar as for the aligned tests: we would like to perform all consecutive periodical tests in O(N ) time and show that the test right before this sequence of periodic tests is either protected or misaligned.As in case of aligned tests, the crucial part is the identification of a sequence of consecutive periodical tests.To identify them quickly, we keep for each short A i the value k such that A i+k is the first word that is different from A i or is the last word or the almost last word (in the sense as in the previous section: A i+k is almost last if A i+k X ends the side of the equation), as well as the link to this A i+k .Those are Open problems.Is it possible to remove the usage of range minimum queries from the algorithm without increasing the running time?Can the recompression approach be used to speed up the algorithms for the two variable word equations?Can one use recompression approach also to better upper bound the number of solutions of an equation with a single variable?

Algorithm 1 Algorithm 2
PairCompNCr(a, b) Pair compression for a non-crossing pair 1: let c ∈ Γ be an unused letter 2: replace each explicit ab in A and B by c Similarly when none block of a has a crossing occurrence, the a's blocks compression consists simply of replacing explicit a blocks.BlockCompNCr(a) Block compression for a letter a with no crossing block 1: for each explicit a's -block occurring in U or V with > 1 do 2:

3 :Lemma 3 .
replace every explicit a's -block occurring in A or B by a Let ab be a non-crossing pair then PairCompNCr(a, b) implements the pair compression for ab.Let a has no crossing blocks, then BlockCompNCr(a) implements the block compression for a. Proof.Consider first the case of PairCompNCr.Suppose that A = B has a solution S. Define S : S (X) is equal to S(X) with each ab replaced with c (where c is a new letter).Consider S(A) and S (A ).Then S (A ) is obtained from S(A) by replacing each ab with c (as a = b this is well-defined): the explicit occurrences of ab are replaced by PairCompNCr(a, b), the implicit ones are replaced by the definition of S and by the assumption there are no crossing occurrences.The same applies to S(B) and S (B ), hence S is a solution of A = B .

2 : 3 : 4 : 5 :
if TestSimpleSolution(b) returns 1 then S(X) = b is a solution report solution S(X) = b replace each X in A = B by bX Implicitly change S(X) = bw to S(X) = w if a is the last letter of S(X) then 6:

7 : 8 :Lemma 4 .
report solution S(X) = a replace each X in A = B by XaImplicitly change S(X) = w a to S(X) = w Pop(a, b) preserves solutions and after its application the pair ab is noncrossing.

Algorithm 4
PairComp(a, b) Turning crossing pair ab into non-crossing ones and compressing it 1: run Pop(a, b) 2: run PairCompNCr(a, b) Lemma 5. PairComp(a, b) implements the pair compression of the pair ab.

8 :
let r > 0 be the length of the b-suffix of the non-empty of A n A , B n B By Lemma 1 S(X) has the same b-suffix 9: replace each X in A = B by Xb r b r is stored in a compressed form, 10:

1 :
Letters ← letters occurring in the equation 2: run CutPrefSuff Removes crossing blocks of a 3: for each letter a ∈ Letters do 4:

Algorithm 8 OneVarWordEq 2 : 4 : 5 : 6 : 8 : 9 :Theorem 1 .
Reports solutions of a given one-variable word equation 1: while the first block and the last block are not blocks of a letter do Pairs ← pairs occurring in S(A) = S(B) 3: BlockComp Compress blocks, in O(|A| + |B|) time.PreProc There are only two crossing pairs, see Lemma 6 Crossing ← list of crossing pairs from Pairs There are two such pairs Non-Crossing ← list of non-crossing pairs from Pairs 7: for each ab ∈ Non-Crossing do Compress non-crossing pairs, in time O(|A| + |B)| PairCompNCr(a, b) for ab ∈ Crossing do Compress the 2 crossing pairs, in time O(|A| + |B)| 10: PairComp(a, b) 11: TestSolution Test solutions from a * , see Lemma 11 We call one iteration of the main loop a phase.OneVarWordEq runs in time O(|A| + |B| + (n A + n B ) log(|A| + |B|)) and correctly reports all solution of a word equation A = B.

Lemma 9 .
Consider two consecutive letters a, b in S(A) for any solution S. At least one of those letters is compressed in this phase.Proof.Consider whether a = b or not: a = b: Then they are compressed using BlockComp.a = b: Then ab is a pair occurring in the equation at the beginning of the phase and so it was listed in Pairs in line 2 and as such we try to compress it, either in line 8 or in line 10.This occurrence cannot be compressed only when one of the letters a, b was already compressed, in some other pair or by BlockComp.In either case we are done.

4. 1 .
Suffix arrays and lcp arrays.We use a standard data structure for comparisons on strings: a suffix array SA[1 . .m] for a string w[1 . .m] stores the m non-trivial suffixes of w, that is w[m], w[m − 1 . .m], . . ., w[1 . .m] in (increasing) lexicographical order.In other words, SA[k] = p if and only if w[p .

Figure 1 .
Figure 1.The alternative factorisation.The first compressed letters are in grey.For simplicity m = r = 1.Each z between the cds is compressed independently.

2 3 s + 4 3 p + 4 3 ≤ 2 3 s 3 .
+ 2p, as p ≥ 2. For the period size, consider u 2 .By Lemma 9, each uncompressed letter (perhaps except the last one) is followed by a compressed one, and so |u 2 | ≤ 2|u 2 |+1 For |u 2 | ≥ 4 this yields the desired compression rate3  4 , for |u 2 | = 2 and |u 2 | = 3 observe that by Lemma 9 at least one letter inside u 2 is compressed and we know that the compressions inside u 2 are done independently, so |u 2 | ≤ |u 2 | − 1, which yields the desired bound for those two border cases.

Figure 2 .
Figure 2. A is arranged against B. The periods of length at most |B| − |A| are in ligther grey.Since A = B, at least one of them is non-empty.

4. 4 . 3 .
Compression running time.Lemma 24.The running time of OneVarWordEq, except for time used to test the solutions, is O(n).

4. 5 . 2 .
Failed tests.We show that in total there are O(log n) failed tests.This follows from the fact that there are O(1) substitutions tested per phase and there are O(log n) phases.Lemma 28.The number of all failed tests is O(log n) over the whole run of OneVarWordEq.
k ) have periods |S(A i X)| and |S(B j X)| and consequently has a period |A i | − |B j | ≤ N .So it is enough to test first common |A i | − |B j | letters and check whether |S(A i X)| and |S(B j X)| have period |A i | − |B j |, which can be checked in O(1) time.This yields that the total time of testing is linear.The details are given in the next subsections.We begin with showing that indeed each test is either failed, protected, misaligned or periodical.Without loss of generality, consider a test of a letter from A i and from S(XB j ).If any of A i+1 , B j+1 , A i or B j is long then it is protected (this includes the case in which some of A i+1 , B j , B j+1 does not exist).Concerning the running time, for each explicit word we keep a flag, whether it is short or long.Furthermore, as each explicit word has a link to its successor and predecessor, we can establish whether any of A i+1 , B j+1 , A i or B j is long in O(1) time.So consider the case in which all A i+1 , B j+1 , A i or B j (if they exist) are short, which also can be established in O(