Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

dynamic programming for minimum cost of breaking the string

A certain string-processing language offers a primitive operation which splits a string into two pieces. Since this operation involves copying the original string, it takes n units of time for a string of length n, regardless of the location of the cut. Suppose, now, that you want to break a string into many pieces. The order in which the breaks are made can affect the total running time. For example, if you want to cut a 20-character string at positions 3 and 10, then making the first cut at position 3 incurs a total cost of 20+17=37, while doing position 10 first has a better cost of 20+10=30.

Give a dynamic programming algorithm that, given the locations of m cuts in a string of length n, finds the minimum cost of breaking the string into m + 1 pieces.

This problem is from "Algorithms" chapter6 6.9.

Since there is no answer for this problem, This is what I thought.

Define OPT(i,j,n) as the minimum cost of breaking the string, i for start index, j for end index of String and n for the remaining number of cut I can use.

Here is what I get:

OPT(i,j,n) = min {OPT(i,k,w) + OPT(k+1,j,n-w) + j-i} for i<=k<j and 0<=w<=n

Is it right or not? Please help, thx!

like image 492
CSnerd Avatar asked Oct 29 '13 18:10

CSnerd


1 Answers

I think your recurrence relation can become more better. Here's what I came up with, define cost(i,j) to be the cost of cutting the string from index i to j. Then,

cost(i,j) = min {length of substring + cost(i,k) + cost(k,j) where i < k < j}
like image 143
KanwarG Avatar answered Nov 08 '22 03:11

KanwarG