UVa 1223

It's a string matching problem called Longest Common Substring (LCS). We need to find the length of the longest substring that appears more than twice. It can be solved using KMP or Suffix Array.

Explanation
Using the reset table of KMP we can find the longest common prefix that is a suffix too. The longest border is the answer. But we have the case that the answer is not at the begin of the string. So, if we have a string S of length N, and a function called kmpPreprocess that builds the reset table, our answer will be MAX({kmpPreprocess(S[0...n-1), kmpPreprocess(S[1...n-1]), kmpPreprocess(S[2...n-1)).

But this solution is O(n³) we'll get a TLE. We can improve that. Instead of blindly test the S[i+1...n-1] substring, we use the last reset table to find the maximum safe value of characters we can skip. When you run the algorithm, just keep track of the border found, that's the value of characters that need to skip. Works like the KMP Search, but, inside reset table.

Gotchas

 * The statement says that overlapping ocurrences should count. It's a case that KMP treats well.
 * The reset table gives the answer, but also, we need to use itself to speed up the solution. If in S we find a border of length 6, the next substring S[i+1...n-1] will give us a border of 5, the next 4, etc. We don't need to recompute these values.

Input
4 abcdefghikjlmn abcabcabc abcdabcabb dabcabcabc

Output
0 6 3 6

Solutions
CPP: https://github.com/goodeath/algorithmist-problems/blob/master/uva/strings/1223/1223.cpp