Skip to content

P9623 Baby's First Suffix Array Problem

Solution by Gemini

TODO

If this is for a competitive programming problem where you are given a large string \(s\) and \(Q\) queries \((l, r, k)\), building a SAM for every query will be too slow (\(O(Q \cdot N)\)).

In this case, you use a global SAM combined with a 2D-counting data structure to answer queries in \(O(\log N)\) time.

  1. Build the Suffix Tree: Build the SAM for the reversed string \(s^R\). The link tree of this SAM is exactly the Suffix Tree of the original string \(s\).

  2. Suffix Array / DFS Order: Do a lexicographical DFS on this link tree. The order in which you visit the leaves gives you the global Suffix Array of \(s\).

  3. Map the Query:

    To compare \(s[i \dots r]\) and \(s[k \dots r]\), you use their Longest Common Prefix (LCP), which is found using the Lowest Common Ancestor (LCA) in the Suffix Tree.

    Because both strings end at \(r\), \(s[i \dots r]\) is lexicographically smaller than \(s[k \dots r]\) exactly when:

    • \(s[i \dots N] < s[k \dots N]\) (its global SA rank is strictly smaller), AND they don't have \(s[k \dots r]\) as a prefix.

    • Or, \(s[i \dots r]\) is a proper prefix of \(s[k \dots r]\).

  4. 2D Range Counting: The query boils down to counting how many indices \(i \in [l, r]\) fall into a specific range of global Suffix Array ranks. This is a classic 2D Range Counting problem, which can be solved efficiently using a Persistent Segment Tree built over the Suffix Array, or offline using a Fenwick tree and line sweep (Mo's algorithm on trees could also work depending on constraints).