In computer science, the longest common prefix array (LCP array) is an auxiliary data structure to the suffix array. It stores the lengths of the longest common prefixes (LCPs) between all pairs of consecutive suffixes in a sorted suffix array. For example, if A := [aab, ab, abaab, b, baab] is a suffix array, the longest common prefix between A[1] = aab and A[2] = ab is a which has length 1, so H[2] = 1 in the LCP array H. Likewise, the LCP of A[2] = ab and A[3] = abaab is ab, so H[3] = 2. Augmenting the suffix array with the LCP array allows one to efficiently simulate top-down and bottom-up traversals of the suffix tree, speeds up pattern matching on the suffix array and is a prerequisite for compressed suffix trees.
History The LCP array was introduced in 1993, by Udi Manber and Gene Myers alongside the suffix array in order to improve the running time of their string search algorithm.
Definition Let A be the suffix array of the string S=s1,s2,…sn−1$ of length n, where $ is a sentinel letter that is unique and lexicographically smaller than any other character. Let S[i,j] denote the substring of S ranging from i to j. Thus, S[A[i],n] is the ith smallest suffix of S. Let lcp(v,w) denote the length of the longest common prefix between two strings v and w. Then the LCP array H[1,n] is an integer array of size n such that H[1] is undefined and H[i]=lcp(S[A[i−1],n],S[A[i],n]) for every 1<i≤n. Thus H[i] stores the length of longest common prefix of the lexicographically ith smallest suffix and its predecessor in the suffix array. Difference between LCP array and suffix array:
Suffix array: Represents the lexicographic rank of each suffix of an array. LCP array: Contains the maximum length prefix match between two consecutive suffixes, after they are sorted lexicographically.
Example Consider the string S=banana$:
and its corresponding sorted suffix array A :
Suffix array with suffixes written out underneath vertically:
Then the LCP array H is constructed by comparing lexicographically consecutive suffixes to determine their longest common prefix:
So, for example, H[4]=3 is the length of the longest common prefix ana shared by the suffixes A[3]=S[4,7]=ana$ and A[4]=S[2,7]=anana$. Note that H[1] is undefined, since there is no lexicographically smaller suffix.
Efficient construction algorithms LCP array construction algorithms can be divided into two different categories: algorithms that compute the LCP array as a byproduct to the suffix array and algorithms that use an already constructed suffix array in order to compute the LCP values. Manber & Myers (1993) provide an algorithm to compute the LCP array alongside the suffix array in O(nlogn) time. Kärkkäinen & Sanders (2003) show that it is also possible to modify their O(n) time algorithm such that it computes the LCP array as well. Kasai et al. (2001) present the first O(n) time algorithm (FLAAP) that computes the LCP array given the text and the suffix array. Assuming that each text symbol takes one byte and each entry of the suffix or LCP array takes 4 bytes, the major drawback of their algorithm is a large space occupancy of 13n bytes, while the original output (text, suffix array, LCP array) only occupies 9n bytes. Therefore, Manzini (2004) created a refined version of the algorithm of Kasai et al. (2001) (lcp9) and reduced the space occupancy to 9n bytes. Kärkkäinen, Manzini & Puglisi (2009) provide another refinement of Kasai's algorithm (Φ-algorithm) that improves the running time. Rather than the actual LCP array, this algorithm builds the permuted LCP (PLCP) array, in which the values appear in text order rather than lexicographical order. Gog & Ohlebusch (2011) provide two algorithms that although being theoretically slow (O(n2)) were faster than the above-mentioned algorithms in practice. As of 2012, the currently fastest linear-time LCP array construction algorithm is due to Fischer (2011), which in turn is based on one of the fastest suffix array construction algorithms (SA-IS) by Nong, Zhang & Chan (2009). Fischer & Kurpicz (2017) based on Yuta Mori's DivSufSort is even faster.
Applications As noted by Abouelhoda, Kurtz & Ohlebusch (2004) several string processing problems can be solved by the following kinds of tree traversals:
bottom-up traversal of the complete suffix tree top-down traversal of a subtree of the suffix tree suffix tree traversal using the suffix links. Kasai et al. (2001) show how to simulate a bottom-up traversal of the suffix tree using only the suffix array and LCP array. Abouelhoda, Kurtz & Ohlebusch (2004) enhance the suffix array with the LCP array and additional data structures and describe how this enhanced suffix array can be used to simulate all three kinds of suffix tree traversals. Fischer & Heun (2007) reduce the space requirements of the enhanced suffix array by preprocessing the LCP array for range minimum queries. Thus, every problem that can be solved by suffix tree algorithms can also be solved using the enhanced suffix array. Deciding if a pattern P of length m is a substring of a string S of length n takes O(mlogn) time if only the suffix array is used. By additionally using the LCP information, this bound can be improved to O(m+logn) time. Abouelhoda, Kurtz & Ohlebusch (2004) show how to improve this running time even further to achieve optimal O(m) time. Thus, using suffix array and LCP array information, the decision query can be answered as fast as using the suffix tree. The LCP array is also an essential part of compressed suffix trees which provide full suffix tree functionality like suffix links and lowest common ancestor queries. Furthermore, it can be used together with the suffix array to compute the Lempel-Ziv LZ77 factorization in O(n) time. The longest repeated substring problem for a string S of length n can be solved in Θ(n) time using both the suffix array A and the LCP array. It is sufficient to perform a linear scan through the LCP array in order to find its maximum value vmax and the corresponding index i where vmax is stored. The longest substring that occurs at least twice is then given by S[A[i],A[i]+vmax−1]. The remainder of this section explains two applications of the LCP array in more detail: How the suffix array and the LCP array of a string can be used to construct the corresponding suffix tree and how it is possible to answer LCP queries for arbitrary suffixes using range minimum queries on the LCP array.
Find the number of occurrences of a pattern
… excerpt ends here. Continue reading the full article.

![LCP array: Case 2 (d(v)<H[i+1]): In order to add suffix nana$, the edge to the previously inserted suffix na$ has to be split up. The new edge to the new internal node is labeled with the longest common prefix of the suffixes na$ and nana$. The edges connecting the two leaves are labeled with the remaining suffix characters that are not part of the prefix.](https://thumb.wikimedia.org/wikipedia/commons/thumb/3/3a/Constructing_the_suffix_tree_of_banana_based_on_the_suffix_array_and_the_LCP_array_-_Case_2.pdf/page1-1280px-Constructing_the_suffix_tree_of_banana_based_on_the_suffix_array_and_the_LCP_array_-_Case_2.pdf.jpg?utm_source=en.wikipedia.org&utm_campaign=parser&utm_content=thumbnail)