Given two strings $ S_1, S_2$ , we write $ S_1S_2$ for their concatenation. Given a string $ S$ and integer $ k\geq 1$ , we write $ (S)^k = SS\cdots S$ for the concatenation of $ k$ copies of $ S$ . Now given a string, we can use this notation to ‘compress’ it, i.e. $ AABAAB$ may be written as $ ((A)^2 B)^2$ . Let’s call the weight of a *compression* the number of characters appearing in it, so the weight of $ ((A)^2 B^2)$ is two, and the weight of $ (AB)^2 A$ (a *compression* of $ ABABA$ ) is three (separate $ A$ s are counted separately).

Now consider the problem of computing the ‘lightest’ compression of a given string $ S$ with $ |S|=n$ . After some thinking there is an obvious dynamic programming approach which runs in $ O(n^3 \log n)$ or $ O(n^3)$ depending on the exact approach.

However, I have been told this problem can be solved in $ O(n^2 \log n)$ time, though I cannot find any sources on how to do this. Specifically, this problem was given in a recent programming contest (problem K here, last two pages). During the analysis an $ O(n^3 \log n)$ algorithm was presented, and at the end the pseudo quadratic bound was mentioned (here at the four minute mark). Sadly the presenter only referred to ‘a complicated word combinatorics lemma’, so now I have come here to ask for the solution ðŸ™‚