Problem 533
Longest Duplicate Substring
Hard · 30 pts · String, Binary Search, Rolling Hash
Problem
Find the longest substring of S that occurs at least twice in S. The two occurrences may overlap. If several substrings share that longest length, print the lexicographically smallest of them. If no substring repeats at all, print -1. If some length L repeats then every shorter length does too, so binary search on L, and test a length with a rolling hash over all windows of that length.
Input
A single string S of lowercase English letters.
Output
Print the longest repeated substring, or -1.
Constraints
1 <= |S| <= 30000
Example input
banana
Example output
ana
Explanation
In banana, the substring ana appears starting at positions 1 and 3 (the two copies overlap). No substring of length 4 appears twice, so the answer is ana.