Dynamic Programming21 sections · 916 units
Open in Course

Breaking a String - Problem Statement

Classic Knuth problem

A string of length nn with mm break points. Each break costs the current string length. Find minimum total cost. dp[i][j]dp[i][j] = min cost to break segment between break points ii and jj.

Cost of one break = segment length. Transition: dp[i][j]=mink(dp[i][k]+dp[k][j])+(pos[j]pos[i])dp[i][j] = \min_{k} (dp[i][k] + dp[k][j]) + (pos[j] - pos[i]). The length adds to all splits. QI holds. Apply Knuth: O(m2)O(m^2) total. Without improvement, O(m3)O(m^3). This is one of the cleanest examples of Knuth improvement. The cost function satisfies the quadrangle inequality.