Question

In: Computer Science

This text is justified, meaning that the words are arranged so that both the left and...

This text is justified, meaning that the words are arranged so that both the left and right margins form straight lines. This is accomplished by varying the amount of space between words and characters in the text. Unfortunately, there are limits to this technique since uneven spacing eventually becomes noticeable to the human reader. Your job in this problem is going to be determining where to put line breaks in a sequence of words to make the most visually-pleasing justified paragraph.

You are given an array of word lengths (the lengths include space needed for whitespace and punctua- tion). We are assuming the every character occupies the same amount of space and that lines have room for 80 characters. You need to determine the number of words to put onto each line. Your selection must limit the number of characters per line to 80 or less. For example, consider the sequence 20, 20, 20, 10, 10, 10, 20, 15, 40. One possible solution would be 5, 3, 1 (i.e. 5 words on the first line, 3 on the second, and 1 on the third), which puts 80 characters on line 1, 45 on line 2, and 40 on line 3. A second solution would be 4, 4, 1, which puts 70 characters on line 1, 55 on line 2, and 40 on line 3.

The quality of your line breaks will be judged based on all lines except the last, which is ignored since the last line of a paragraph can be of any length. Every other line contributes the square of the number of unused characters on that line. You are asked to minimize the sum of these squares. For example, the first solution above has cost 02 + 252 = 1, 225 while the second has cost 102 + 252 = 725.

Give a fast dynamic programming algorithm to find the optimal set of line breaks. Be sure to explain how to get the actual locations of the line breaks (not just the optimal cost). Argue for your algorithm’s correctness and analyze its running time.

Solutions

Expert Solution

#include <bits/stdc++.h>
using namespace std;
#define INF INT_MAX

int printSolution (vector<int>print,int n)
{
int k;
if (print[n] == 1)
k = 1;
else
k = printSolution (print, print[n]-1) + 1;
cout<<"Line number "<<k<<": From word no. "<<print[n]<<" to "<<n<<endl;
return k;
}
void solve(vector<int>line, int m)
{
int n=line.size();
vector<vector<int> >extras(n+1,vector<int>(n+1));
vector<vector<int> >cost(n+1,vector<int>(n+1));
vector<int>cumulative_cost(n+1);
vector<int>print(n+1);
int i, j;
for (i = 1; i <= n; i++)
{
extras[i][i] = m - line[i-1]; //calculate extra words if i to j words place in a single line
for (j = i+1; j <= n; j++)
extras[i][j] = extras[i][j-1] - line[j-1] - 1;
}
for (i = 1; i <= n; i++)
{
for (j = i; j <= n; j++) //calculate extra words if i to j words place in a single line
{
if (extras[i][j] < 0)
cost[i][j] = INF;
else if (j == n && extras[i][j] >= 0)
cost[i][j] = 0;
else
cost[i][j] = extras[i][j]*extras[i][j];
}
}
cumulative_cost[0] = 0;
for (j = 1; j <= n; j++)
{
cumulative_cost[j] = INF;
for (i = 1; i <= j; i++) //calculate minimum cumulative cost from 1 to j
{
if (cumulative_cost[i-1] != INF && cost[i][j] != INF &&
(cumulative_cost[i-1] + cost[i][j] < cumulative_cost[j]))
{
cumulative_cost[j] = cumulative_cost[i-1] + cost[i][j];
print[j] = i;
}
}
}
printSolution(print,n);
}
int main()
{
int i,n,m=6;
cin>>n;
vector<int>a(n);
for(i=0;i<n;i++)
cin>>a[i];
solve(a,m);
return 0;
}
Time Complexity:O(n^2)

Space Complexity:O(n^2)

where n is length of array

Quick Explaination:

  • First we compute costs of all possible lines .
  • If a sequence of words from i to j cannot fit in a single line, then lc[i][j] is considered infinite
  • cumulative cost upto j is zero if j is zero
  • otherwise it is minimum of (for all i (1<=i<=j) min(cumulative cost[i]+cost[i+1][j]
  • we can keep a parallel p array that points to where each c value came from.

Related Solutions

14. Explain the meaning of the words "Nothing so aggravates an earnest person as a passive...
14. Explain the meaning of the words "Nothing so aggravates an earnest person as a passive resistance" (p. 304, Volume E) in "Bartleby the Scrivener". 15. What do you feel might happen to Melville's character Bartleby today, in real life? 16. In "Bartleby the Scrivener", how does Bartleby's past work in the "Dead Letter Office" (p. 321, Volume E) help explain his behavior in the story?
Etching and engraving are both intaglio processes. In 50 words or less, describe, in the text...
Etching and engraving are both intaglio processes. In 50 words or less, describe, in the text box below, the difference in line quality between an etching and an engraving.
Carefully Read the Case study and answer both questions in 250 words each. Shades of meaning...
Carefully Read the Case study and answer both questions in 250 words each. Shades of meaning If you have tried the activities in the previous parts, you are likely to be appreciating afresh just how much is going on around our words as we use them to communicate. As poet T.S. Eliot says, “Words... slip, slide, perish, Decay with imprecision, will not stay in place, Will not stay still.” Surely, our words have made it possible for us to construct...
Would the manager ever be justified in not hiring the female appli- cant? If so, what...
Would the manager ever be justified in not hiring the female appli- cant? If so, what would those circumstances be?
Given a sequence of closed intervals arranged so that each interval is a subinterval of the...
Given a sequence of closed intervals arranged so that each interval is a subinterval of the one preceding it and so that the lengths of the intervals shrink to zero, then there is exactly one point that belongs to every interval of the sequence. (This is known as the nested interval property)
Based on the article "Is Inheritance Justified?" by D.W.Haslett (300 words if possible) Discuss and explain...
Based on the article "Is Inheritance Justified?" by D.W.Haslett (300 words if possible) Discuss and explain Haslett’s argument that the commitment to equal opportunity and freedom in the broad sense requires abolishing inheritance and discuss how this argument makes use of a Rawls’ type theory of justice. (Rawls himself did not make the argument against inheritance, but it seems that we can interpret Haslett as making use of some main ideas in a Rawls’ type theory of justice)
Can war ever be justified? If so, under what conditions and with what restrictions? Compare the...
Can war ever be justified? If so, under what conditions and with what restrictions? Compare the views of Natural Law Theory, Utilitarianism, and Divine Command Theory. (400- 500 words)
Your text authors note that it is important to place consideration of the specific meaning of...
Your text authors note that it is important to place consideration of the specific meaning of a good life in the context of a. personality and genetic differences b. differences in values and religious orientation. c. different cultures and stages of lifespan development. d. life events and availability of resources and opportunities. According to your textbook authors, what is “new” and unique about positive psychology is that it has a. helped clarify the relative independence of the “good” and the...
The text is too large so I am not able to provide you with the text....
The text is too large so I am not able to provide you with the text. That is why I have provided a direct link for my assignment. Thank you https://www.researchgate.net/publication/271271253_Gut_microbiota_composition_correlates_with_changes_in_body_fat_content_due_to_weight_los I need to answer these questions on it. List TWO sources other than those listed in the periodicals, that you could refer to if you were to research the article’s topic further. (1 point) 8. Did you learn anything interesting (diet related) that may cause you to alter your...
Sociology Explain in your own words. 1.The meaning of science of sociology 2. The meaning of...
Sociology Explain in your own words. 1.The meaning of science of sociology 2. The meaning of C. Wright Mills theory and argument. EXPLAIN IN YOUR OWN WORDS how you relate to his theory, and produce examples from your daily life to explain what he says.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT