You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/string/suffix-array.md
+5-5Lines changed: 5 additions & 5 deletions
Original file line number
Diff line number
Diff line change
@@ -299,25 +299,25 @@ We have compute the longest common prefix (**LCP**) for two suffixes of a string
299
299
300
300
Unlike the previous method this one will only use $O(|s|)$ memory.
301
301
The result of the preprocessing will be an array (which itself is an important source of information about the string, and therefore also used to solve other tasks).
302
-
Answering LCP queries can be answered by performing RMQ queries (range minimum queries) in this array, so for different implementations it is possible to achieve logarithmic and even constant query time.
302
+
LCP queries can be answered by performing RMQ queries (range minimum queries) in this array, so for different implementations it is possible to achieve logarithmic and even constant query time.
303
303
304
304
The basis for this algorithm is the following idea:
305
305
we will compute the longest common prefix for each **pair of adjacent suffixes in the sorted order**.
306
306
In other words we construct an array $\text{lcp}[0 \dots n-2]$, where $\text{lcp}[i]$ is equal to the length of the longest common prefix of the suffixes starting at $p[i]$ and $p[i+1]$.
307
307
This array will give us an answer for any two adjacent suffixes of the string.
308
308
Then the answer for arbitrary two suffixes, not necessarily neighboring ones, can be obtained from this array.
309
309
In fact, let the request be to compute the LCP of the suffixes $p[i]$ and $p[j]$.
310
-
Then the answer to this query will be $\min\\{p[i],~p[i+1],~ \dots,~p[j-1]\\}$.
310
+
Then the answer to this query will be $\min(lcp[i],~lcp[i+1],~ \dots,~lcp[j-1])$.
311
311
312
312
Thus if we have such an array $\text{lcp}$, then the problem is reduced to the [RMQ](./sequences/rmq.html), which has many wide number of different solutions with different complexities.
313
313
314
314
So the main task is to **build** this array $\text{lcp}$.
315
-
We will uses**Kasai's algorithm**, which can compute this array in $O(n)$ time.
315
+
We will use**Kasai's algorithm**, which can compute this array in $O(n)$ time.
316
316
317
317
Let's look at two adjacent suffixes in the sorted order (order of the suffix array).
318
-
Let their staring positions be $i$ and $j$ and their $\text{lcp}$ equal to $k > 0$.
318
+
Let their starting positions be $i$ and $j$ and their $\text{lcp}$ equal to $k > 0$.
319
319
If we remove the first letter of both suffixes - i.e. we take the suffixes $i+1$ and $j+1$ - then it should be obvious that the $\text{lcp}$ of these two is $k - 1$.
320
-
However we cannot use this value and write it in the $\text{lcp}$ array, because these two two suffixes might not be next to each other in the sorted order.
320
+
However we cannot use this value and write it in the $\text{lcp}$ array, because these two suffixes might not be next to each other in the sorted order.
321
321
The suffix $i+1$ will of course be smaller than the suffix $j+1$, but there might be some suffixes between them.
322
322
However, since we know that the LCP between two suffixes is the minimum value of all transitions, we also know that the LCP between any two pairs in that interval has to be at least $k-1$, especially also between $i+1$ and the next suffix.
0 commit comments