Explanation
We go through all possible substring lengths starting from . For each length, we use a map to track how many times each substring of length appears.
If all substrings of that length appear only once, we know we've found our answer and print . 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:
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 answerans = n# We can iterate through lengths of sequences to find the smallest lengthfor 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!