A specific string processing language offers a primitive operation that splits a string into two parts. Since this operation involves copying the original string, it takes n time units for a string of length n, regardless of the location of the cut. Suppose now that you want to split a string into many parts. The order in which breaks are performed may affect the overall runtime. For example, if you want to cut a 20-digit string at positions 3 and 10, then at the first cut in position 3, the total cost is 20 + 17 = 37, while position 10 first has the best cost 20+ 10 = 30.
Give a dynamic programming algorithm that, given the location of m cuts in a string of length n, will find the minimum cost of dividing the string into m + 1 pieces.
This problem is taken from Chapter 6, Algorithms 6.9.
Since there is no answer to this problem, this is what I thought.
Define OPT(i,j,n) as the minimum cost of breaking the string, i for the starting index, j for the ending index String and n for the remaining amount of cut I can use.
Here is what I get:
OPT(i,j,n) = min {OPT(i,k,w) + OPT(k+1,j,nw) + ji} for i<=k<j and 0<=w<=n
Is this right or wrong? Please help, thanks!
string algorithm dynamic-programming
Csnerd
source share