Table of Contents

ExplanationImplementation

Official Analysis (C++)

Explanation

We go through all possible substring lengths kk starting from 11. For each length, we use a map to track how many times each substring of length kk appears.

If all substrings of that length appear only once, we know we've found our answer and print kk. This works because once we find a length where all substrings are unique, we don't need to check for longer lengths.

Implementation

Time Complexity: O(N3)\mathcal{O}(N^3)

file_in = open("whereami.in")
data = file_in.read().strip().split("\n")
n = int(data[0])
mailboxes = data[1]
# Set the answer initially to n, as we know n is always a possible answer
ans = n
# We can iterate through lengths of sequences to find the smallest length
for l in range(1, n + 1):

Join the USACO Forum!

Stuck on a problem, or don't understand a module? Join the USACO Forum and get help from other competitive programmers!