Se não estou lhe entendendo mal, acho que a fatoração de custo mínimo pode ser calculada no tempo de O(n2) seguinte maneira.
Para cada índice i, iremos calcular um grupo de valores (pℓi,rℓi) para ℓ=1,2,… , como se segue. Seja p1i≥1 o número inteiro menor, de modo que exista um número inteiro r≥2 satisfazendo S[i−rp1i+1,i−p1i]=S[i−(r−1)p1i+1,i].
For this particular p1i, let r1i be the largest r with this property.
If no such pi exists, set Li=0 so we know there are zero (pℓi,rℓi) values for this index.
Let p2i be the smallest integer strictly bigger than (r1i−1)p1i satisfying, likewise,
S[i−r2ip2i+1,i−p2i]=S[i−(r2i−1)p2i+1,i]
for some r2i≥2. Like before, take r2i to be the maximal one having fixed p2i.
In general pℓi is the smallest such number strictly bigger than (rℓ−1i−1)pℓ−1i. If no such pℓi exists, then Li=ℓ−1.
Note that for each index i, we have Li=O(log(i+1)) due to pℓi values increasing geometrically with ℓ. (if pℓ+1i exists, it's not just strictly bigger than (rℓi−1)pℓi but bigger than that by at least pℓi/2. This establishes the geometric increase.)
Suppose now all (pℓi,rℓi) values are given to us. The minimum cost is given by the recurrence
dp(i,j)=min{dp(i,j−1)+1,minℓ(dp(i,j−rℓjpℓj)+dp(j−rℓjpℓj+1,j−pℓj))}
with the understanding that for i>j we set dp(i,j)=+∞.
The table can be filled in O(n2+n∑jLj) time.
We already observed above that ∑jLj=O(∑jlog(j+1))=Θ(nlogn) by bounding the sum term by term. But actually if we look at the whole sum, we can prove something sharper.
Consider the suffix tree T(S←) of the reverse of S (i.e., the prefix tree of S). We will charge each contribution to the sum ∑iLi to an edge of T(S←) so that each edge will be charged at most once. Charge each pji to the edge emanating from nca(v(i),v(i−pji)) and going towards v(i−pji). Here v(i) is the leaf of the prefix tree corresponding to S[1..i] and nca denotes the nearest common ancestor.
This shows that O(∑iLi)=O(n). The values (pji,rji) can be calculated in time O(n+∑iLi) by a traversal of the suffix tree but I will leave the details to a later edit if anyone is interested.
Let me know if this makes sense.