Given a string a I need to find for every position i in a the length of the longest substring b such that it starts in position i and was already present in a, which means that there exists i'<i such that a[i...i+len(b)] = b = a[i'...i'+len(b)]. For example, for a="ababaab" the answer is [0,0,3,2,1,2,1]. I need an algorithm that will solve this problem in O(|a|) or O(|a|log|a|).
I think this problem could be solved with suffix automaton. We can calculate for every state s in automaton the first position in string where the substring that corresponds to the state ends. With this information we will solve the problem in O(n^2) with just walking on the automaton from the starting position i while first_pos - size < i. But I can't find a way to optimise this solution.