# Just a Little More: Solution ## Comparing solutions Full materialization and on-demand output are peer methods. Materialization avoids recursive expansion and searches; on-demand output avoids storing the largest string and is used in the reference. The problem setter’s solution first reads all queries, finds the largest requested version, and constructs it by repeatedly copying the specified prefix to the end. Every version is a prefix of this largest string, so a query simply outputs a prefix of the appropriate length. For maximum version $V$ , maximum length $L_{max}$ , $Q$ queries and $C$ output characters, this takes $O(V+Q+L_{max}+C)$ time and $O(L_{max}+V+Q)$ space. The implementation used by Codex (GPT-6) stores only the initial string and version lengths, recursively expanding a requested prefix according to the same concatenations. This is equivalent to materializing the copies. Extra space falls to $O(V+|s_1|)$ , fitting the memory limit; the time bound is $O(V+(C+Q)\log(V+1))$ . Recursion and version searches add overhead, and all $C$ output characters still have to be written. No overall speed improvement is claimed. ## Compute lengths first Let $L_k=|s_k|$ . Then $$L_1=|s_1|,\qquad L_k=L_{k-1}+\left\lceil L_{k-1}/5\right\rceil$$ Use integer arithmetic `(L + 4) / 5` for the ceiling. Lengths strictly increase, and every version is a prefix of every later version. Store only the 70 lengths and the initial string. ## Print a prefix recursively Define `PrintPrefix(t)` to print the first $t$ characters of any version of length at least $t$ . Every shorter version is a prefix of every longer one, so the choice of a sufficiently long version does not affect the result. - If $t\le L_1$ , print the first $t$ characters of $s_1$ directly. - Otherwise, binary-search for the smallest $k$ with $L_k\ge t$ . Since $L_{k-1}L_1$ , the smallest suitable version $k$ satisfies $L_{k-1}