Warning: foreach() argument must be of type array|object, bool given in /var/www/html/web/app/themes/studypress-core-theme/template-parts/header/mobile-offcanvas.php on line 20

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 first has a better cost of 20+17=37.

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

Short Answer

Expert verified

It is given in the question, that the primitive operation will take 0n time for copying the original string of length ‘n’. Hence, the cost of complete algorithm will be greater then 0n.

To get minimum cost of breaking the string, we will use dynamic programming algorithm and define our recursion relation which will be our subproblem.

Step by step solution

01

Defining the Subproblem

Let us suppose that we have input string ‘S’ of length ‘n’. Then, also let ‘i’ denote the position of cut. Thusi=1,2.....m.

So, if m cuts are made, that means we havesubstrings.

So, let atmade the cut on our original string

So, we got two substrings: S1....iandSi+1...n . But stillm-1cut is to be made. Now, two cases will arise:

  • Ifm-1remaining cut is made in substringS1....i
  • Ifm-1remaining cut is made in substringSi+1...n

In any one of the above two case, there must be minimum cost.

Let x0,x2,....xm+1be the position(index) where we have to make cut. Here, x1=0and xm=n.

Let Li,jdenotes the minimum cost to cut the string, and i=0...mand j=1....m+1.

These strings will start from xiand ends to xj.

So our recursive equation will be:

L(i,j)={0;forj=i+1xj-xi+mini<k<jLi,k,Lk+1,j;forj>i+1

02

Analysing the recursive equation

The above recursive relation will run for 0n2And it is already mention in the question that we use primitive operation to store each string. And taking our string of length ‘n’, our effective time complexity will be0n3.

Unlock Step-by-Step Solutions & Ace Your Exams!

  • Full Textbook Solutions

    Get detailed explanations and key concepts

  • Unlimited Al creation

    Al flashcards, explanations, exams and more...

  • Ads-free access

    To over 500 millions flashcards

  • Money-back guarantee

    We refund you if you fail your exam.

Over 30 million students worldwide already upgrade their learning with Vaia!

One App. One Place for Learning.

All the tools & learning materials you need for study success - in one app.

Get started for free

Most popular questions from this chapter

Let us define a multiplication operation on three symbols a,b,caccording to the following table; thus ab=b,ba=c, and so on. Notice that the multiplication operation defined by the table is neither associative nor commutative.

Find an efficient algorithm that examines a string of these symbols, say bbbbac, and decides whether or not it is possible to parenthesize the string in such a way that the value of the resulting expression is . For example, on input bbbbacyour algorithm should return yes because((b(bb))(ba))c=a.

Given two strings x=x1x2···xnand y=y1y2···ym, we wish to find the length of their longest common subsequence, that is, the largest k for which there are indices i1<i2<···<ikand j1<j2<···<jkwith xi1xi2···xik=yj1yj2···yjk. Show how to do this in time 0(mn).

Yuckdonald’s is considering opening a series of restaurant along Quaint Valley Highway(QVH). The n possible locations are along a straight line, and the distances of these locations from the start of QVH are, in miles and in increasing order,m1,m22,....,mn.. The constraints are as follows:

At each location, Yuckdonald may open at most one restaurant. The expected profit from opening a restaurant at location i is given aspi, wherepi>0andi=1,2,,n.

Any two restaurants should be at least k miles apart, where k is a positive integer.

Give an efficient algorithm to compute the maximum expected total profit subject to the given constraints.

You are going on a long trip. You start on the road at mile post 0. Along the way there aren hotels, at mile posts a1<a2<...<an , where eachai is measured from the starting point. The only places you are allowed to stop are at these hotels, but you can choose which of the hotels you stop at. You must stop at the final hotel (at distance an), which is your destination. You’d ideally like to travel miles a day, but this may not be possible (depending on the spacing of the hotels). If you travel x miles during a day, the penalty for that day is (200x)2. You want to plan your trip so as to minimize the total penalty- that is, the sum, over all travel days, of the daily penalties.Give an efficient algorithm that determines the optimal sequence of hotels at which to stop

Given two strings x=x1x2···xnand y=y1y2···ym, we wish to find the length of their longest common substring, that is, the largest k for which there are indices i and j with xixi+1···xi+k-1=yjyj+1···yj+k-1. Show how to do this in time0(mn)

See all solutions

Recommended explanations on Computer Science Textbooks

View all explanations

What do you think about this solution?

We value your feedback to improve our textbook solutions.

Study anywhere. Anytime. Across all devices.

Sign-up for free