Editorial for DMPG '19 G6 - Pairs
Submitting an official solution before solving the problem yourself is a bannable offence.
Author:
For the first subtask, consider all pairs of substrings. It takes
to compute the length of the longest common prefix (LCP) of two of these substrings, which would be too slow. Instead, by fixing the starting position of each of the substrings and computing the running LCP, each length can be computed in
.
Time Complexity:
For the next subtask, we think of substrings as prefixes of suffixes. For each pair of suffixes, their LCP can be computed in . Say that the suffixes with length
and
have an LCP of length
. It is not hard to see that there are
pairs of prefixes whose LCP is equal to
and there are
pairs of prefixes whose LCP is equal to
. In particular, the strings themselves are no longer important, only
,
, and
matter. These values can be computed and added to the answer for each length.
Time Complexity:
For the third subtask, the solution to the second subtask is sped up. A suffix tree can be constructed to compute all the LCPs of the pairs of suffixes in . The answer values found above must also be added to the answers more quickly. This can be done by noting that each term can be split into
,
, and
terms. Then these coefficients can be added to the answer values by updating a difference array instead and computing the final answer at the end.
Time Complexity:
For the final subtask, the solution can be sped up even further. A suffix array and LCP array construction can be done in . The LCP array only computes the LCP between two consecutive suffixes in the suffix array rather than every pair. However, the LCP between two arbitrary pairs of suffixes can easily be computed using the LCP array: If the suffixes are at index
and
, their LCP is the minimum of
.
For each , consider indices
and
,
, so that the LCP of the suffixes indexed at
and
is equal to
. These indices can all be computed in
using a stack. Observe that
describe a range of pairs of suffixes whose LCP is exactly equal to
. Then this essentially computes the LCP of any pair of suffixes. It is not hard to handle multiple pairs of suffixes at once rather than a single pair of suffixes as in the third subtask. The solution is essentially the same and the final answer can be done by updating a difference array of the answers instead and processing at the end.
Time Complexity:
Comments