Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7763255
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T14:36:50+00:00 2026-06-01T14:36:50+00:00

I have been working on the following problem from this book . A certain

  • 0

I have been working on the following problem from this book.

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.

I need a dynamic programming algorithm that given m cuts, finds the minimum cost of cutting a string into m+1 pieces.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T14:36:51+00:00Added an answer on June 1, 2026 at 2:36 pm

    The divide and conquer approach seems to me the best one for this kind of problem. Here is a Java implementation of the algorithm:

    Note: the array m should be sorted in ascending order (use Arrays.sort(m);)

    public int findMinCutCost(int[] m, int n) {
       int cost = n * m.length;
       for (int i=0; i<m.length; i++) {
          cost = Math.min(findMinCutCostImpl(m, n, i), cost);
       }
       return cost;
    }
    
    private int findMinCutCostImpl(int[] m, int n, int i) {
       if (m.length == 1) return n;
       int cl = 0, cr = 0;
       if (i > 0) {
          cl = Integer.MAX_VALUE;
          int[] ml = Arrays.copyOfRange(m, 0, i);
          int nl = m[i];
          for (int j=0; j<ml.length; j++) {
             cl = Math.min(findMinCutCostImpl(ml, nl, j), cl);
          }
       }
       if (i < m.length - 1) {
          cr = Integer.MAX_VALUE;
          int[] mr = Arrays.copyOfRange(m, i + 1, m.length);
          int nr = n - m[i];
          for (int j=0; j<mr.length; j++) {
             mr[j] = mr[j] - m[i];
          }
          for (int j=0; j<mr.length; j++) {
             cr = Math.min(findMinCutCostImpl(mr, nr, j), cr);
          }
       }
       return n + cl + cr;
    }
    

    For example :

     int n = 20;
     int[] m = new int[] { 10, 3 };
    
     System.out.println(findMinCutCost(m, n));
    

    Will print 30

    ** Edit **

    I have implemented two other methods to answer the problem in the question.

    1. Median cut approximation

    This method cut recursively always the biggest chunks. The results are not always the best solution, but offers a not negligible gain (in the order of +100000% gain from my tests) for a negligible minimal cut loss difference from the best cost.

    public int findMinCutCost2(int[] m, int n) {
       if (m.length == 0) return 0;
       if (m.length == 1) return n;
          float half = n/2f;
          int bestIndex = 0;
          for (int i=1; i<m.length; i++) {
             if (Math.abs(half - m[bestIndex]) > Math.abs(half - m[i])) {
                bestIndex = i;
             }
          }
          int cl = 0, cr = 0;
          if (bestIndex > 0) {
             int[] ml = Arrays.copyOfRange(m, 0, bestIndex);
             int nl = m[bestIndex];
             cl = findMinCutCost2(ml, nl);
          }
          if (bestIndex < m.length - 1) {
             int[] mr = Arrays.copyOfRange(m, bestIndex + 1, m.length);
             int nr = n - m[bestIndex];
             for (int j=0; j<mr.length; j++) {
             mr[j] = mr[j] - m[bestIndex];
          }
          cr = findMinCutCost2(mr, nr);
       }
       return n + cl + cr;
    }
    

    2. A constant time multi-cut

    Instead of calculating the minimal cost, just use different indices and buffers. Since this method executes in a constant time, it always returns n. Plus, the method actually split the string in substrings.

    public int findMinCutCost3(int[] m, int n) {
       char[][] charArr = new char[m.length+1][];
       charArr[0] = new char[m[0]];
       for (int i=0, j=0, k=0; j<n; j++) {
          //charArr[i][k++] = string[j];   // string is the actual string to split
          if (i < m.length && j == m[i]) {
             if (++i >= m.length) {
                charArr[i] = new char[n - m[i-1]];
             } else {
                charArr[i] = new char[m[i] - m[i-1]];
             }
             k=0;
          }
       }
       return n;
    }
    

    Note: that this last method could easily be modified to accept a String str argument instead of n and set n = str.length(), and return a String[] array from charArr[][].

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have been working on this problem for 2 days now and it's an
I have been trying to get a basic reorderlist working following this guide ->
I have been working with Xcode 4.2.1, but following some tutorials and videos from
Hi For many days I have been working on this problem in MySQL, however
I've been working on this problem for 7 hours now, and I still have
Following on from my previous question I have been working on getting my object
Have been working on this question for a couple hours and have come close
I have been having this problem in IE. I have two divs which I
I've been using the following memoizing decorator (from the great book Python Algorithms: Mastering
I have been reading this question and doesn't get the problem it encounters. why

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.