UVa 11512 - GATTACA

11512 - GATTACA

 * https://onlinejudge.org/index.php?option=com_onlinejudge&Itemid=8&category=24&page=show_problem&problem=2507

Summary
Given a string (length <= 1000). Find the longest least lexicographical substring of the string that occurs at least twice.

Explanation
Any algorithms that have complexity O(length^2) or lower can get accepted. One way is to use trie to enumerate all N suffixes of the string and keep the number of visitation for each node in the trie. To find the answer, a tree traversal will do. Find the deepest node in the trie that have at least 2 visitation count.

Another way is to use Suffix Array and Longest Common Prefix to find an adjacent strings in the Suffix Array that have the longest Longest Common Prefix. The Suffix Array and Longest Common Prefix can be constructed in O(N). Thus the overall algorithm complexity is O(N). The number of occurrence can be found while scanning through the Longest Common Prefix list.

Gotchas

 * The trie traversal should be done in lexicographical order: A,C,G,T

Implementations
Trie O(length^2) implementations is as follows