Official

G - Substring Game Editorial by en_translator


This problem can be solved with a data structure called Suffix Automaton.

Suffix Automaton is an automaton that accepts, for a string \(S\), the suffixes of \(S\).

For more details, please refer to the following articles:

The game in the original problem can be interpreted using the Suffix Automaton of \(S\) as follows:

  • Initially, there is a vertex representing an empty string of the Suffix Automaton of \(A\).
  • Alice and Bob take turns to choose a vertex that can be advanced by traversing an edge going from the current vertex and move the piece to there.
  • The first player unable to make a move loses. Who wins?

This can be answered with a simple backtracking.

The Suffix Automaton has at most \((2|S|-1)\) vertices, with at most \((3|S|-4)\) edges. Thus, the problem can be solved in \(O(|S|)\) time.

By implementing this approach appropriately, this problem can be solved.

posted:
last update: