1answer.
Ask question
Login Signup
Ask question
All categories
  • English
  • Mathematics
  • Social Studies
  • Business
  • History
  • Health
  • Geography
  • Biology
  • Physics
  • Chemistry
  • Computers and Technology
  • Arts
  • World Languages
  • Spanish
  • French
  • German
  • Advanced Placement (AP)
  • SAT
  • Medicine
  • Law
  • Engineering
Sedaia [141]
3 years ago
10

A certain string-processing language offers a primitive operation which splits a string into two pieces. Since this operation in

volves 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.
Required:
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.
Computers and Technology
1 answer:
Nostrana [21]3 years ago
8 0

Answer:

Explanation:

First we define 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}

void  s_cut()    

 {

   int l,p;

   int temp=0;

   //ArrayList<Integer> al = new ArrayList<Integer>();

   int al[];

   Scanner s=new Scanner(System.in);

   int table[][];

   ArrayList<Integer> values[][];

   int low=0,high=0;

   int min=0;

   l=s.nextInt();

   p=s.nextInt();

   System.out.println("The values are "+l+"  "+p);

   table= new int[l+1][l+1];

   values= new ArrayList[l+1][l+1];

   al= new int[p];

   for(int i=0;i<p;i++)

   {

       al[i]=s.nextInt();

   }

   for(int i=0;i<=l;i++)

   for(int j=0;j<=l;j++)

       values[i][j]=new ArrayList<Integer>();

   System.out.println();

   for(int i=1;i<=l;i++)

       table[i][i]=0;

   //Arrays.s

   Arrays.sort(al);

   for(int i=0;i<p;i++)

   {

       System.out.print(al[i]+ "  ");

   }

   for(int len=2;len<=l;len++)

   {

       //System.out.println("The length is  "+len);

       for(int i=1,j=i+len-1;j<=l;i++,j++)

       {

           high= min_index(al,j-1);

           low= max_index(al,i);

           System.out.println("Indices are "+low+"  "+high);

           if(low<=high && low!=-1 && high!=-1)

           {

           int cost=Integer.MAX_VALUE;;

           for(int k=low;k<=high;k++)

           {

               //if(al[k]!=j)

               temp=cost;

               cost=Math.min(cost, table[i][al[k]]+table[al[k]+1][j]);

               if(temp!=cost)

               {

                   min=k;  

                   //values[i][j].add(al[k]);

                   //values[i][j].addAll(values[i][al[k]]);

                   //values[i][j].addAll(values[al[k]+1][j]);

                   //values[i][j].addAll(values[i][al[k]]);

               }

               //else

               //cost=0;

           }

           table[i][j]= len+cost;

           values[i][j].add(al[min]);

           //values[i][j].addAll(values[i][al[min]]);

           values[i][j].addAll(values[al[min]+1][j]);

           values[i][j].addAll(values[i][al[min]]);

           }

           else

               table[i][j]=0;

           System.out.println(" values are "+i+"  "+j+"  "+table[i][j]);

       }

   }

   System.out.println(" The minimum cost is "+table[1][l]);

   //temp=values[1][l];

   for(int e: values[1][l])

   {

       System.out.print(e+"-->");

   }

You might be interested in
Select the statements below that correctly match the meaning of the letter in the concept of developing SMART goals?
fenix001 [56]

Specific, Measurable, Attainable, Relevant and Timely.

6 0
3 years ago
Help to how to write pseudo code to insert a new node to Binary Search Tree. Using C++.
dimulka [17.4K]

Answer:

Let the function be Node* ins(Node *root,int k)

if root node is NULL then return new node with data equal to k.

If the k <root->data

root->left=ins(root->left,k);

else if k >root->data

root->right =ins(root->right,k);

At last return root.

Explanation:

Node is always inserted at the at the leaf node.We will search k in the tree if we hit a the leaf node the new node is inserted as the child of the leaf node.

4 0
3 years ago
How do you get access To The advance server in Mobile legends
kiruha [24]

Answer: To enter the Advanced Server, please follow the below instructions: Tap into the Profile section and click Account Settings. Tap on the Switch Server button on the bottom right of the screen. This will allow the player to switch between the Original Server and Advanced Server.

Explanation: I play Mobile legends a lot so I know this

4 0
3 years ago
% Do not modify CalculateSum function maxSum = CalculateSum(userNum1, userNum2, userNum3) maxSum = MaxValue(userNum1, userNum2)
sergeinik [125]

Answer:

The function in Python is as follows:

def MaxValue(userNum1, userNum2):

   if userNum2>userNum1:

       return userNum2

   else:

       return userNum1

Explanation:

This defines the function

def MaxValue(userNum1, userNum2):

This returns userNum2 if userNum2 is greater than userNum1

<em>    if userNum2>userNum1:</em>

<em>        return userNum2</em>

If otherwise, this returns userNum1

<em>    else:</em>

<em>        return userNum1</em>

<em />

6 0
3 years ago
Is space between lines of program code that makes the code easier to read and that the compiler ignores?
MAXImum [283]
<span>The space between the lines of program codes make the code easier to read.  This is used by programmers to make their programs more readable and clear.  The space between the lines is not recognized by the compiler, thus it is ignored by the compiler.</span>
3 0
3 years ago
Other questions:
  • Trevor got home from work and suddenly realized that he needed to edit a certain file stored in the company network's server. ho
    11·1 answer
  • A job posting is the best way to find out what _____ are required for a position.A.aptitudes B.hard skills C.soft skills D.dress
    10·2 answers
  • A windows host sends a tcp segment with source port number 1200 and destination port number 25. the sending host is a(n) _______
    10·1 answer
  • When a file is transferred between two computers, two acknowledgment strategies are possible. In the first one, the file is chop
    5·1 answer
  • A(n) __________ item is a hardware or software item that is to be modified and revised throughout its life cycle
    10·1 answer
  • A 5-stage MIPS pipeline has a register file without forwarding mechanism. How many NOPs (or bubbles) will you need to add to mak
    9·1 answer
  • Write a program that takes in a positive integer as input, and outputs a string of 1's and 0's representing the integer in binar
    12·1 answer
  • Definition of laptop
    12·2 answers
  • Attribute variables have the same meaning as participants variables. is this true or false?​
    14·1 answer
  • In Fantasy Football, participants compete against one another by choosing certain players from different NFL teams that they thi
    14·1 answer
Add answer
Login
Not registered? Fast signup
Signup
Login Signup
Ask question!