Advertisement
Guest User

Untitled

a guest
Jul 10th, 2014
264
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Bash 1.98 KB | None | 0 0
  1. algorithm PrintingNeatly ( M; l1 , . . . , ln )
  2.  pre-cond : l1 , . . . , ln are the lengths of the words, and M is the length of each line.
  3.  post-cond : opt Sol splits the text into lines in an optimal way, and cost is its cost.
  4. begin
  5.     % Table: optSol[i] would store an optimal way to print the first i words of the input,
  6.     but actually we store only the bird’s advice for the subinstance and the cost of its
  7.     solution.
  8.     table[0..n] birdAdvice, cost
  9.     % Base case: The only base case is for the best printing of the first zero words.
  10.     Its solution is the empty printing with cost zero.
  11.     % optSol[0] = ∅
  12.     cost[0] = 0
  13.     birdAdvice[0] = ∅
  14.     % General cases: Loop over subinstances in the table.
  15.     for i = 1 to n
  16.           % Solve instance M; l1 , . . . , li and fill in table entry i .
  17.           K = maximum number k such that the words of length li−k+1 , . . . , li fit
  18.           on a single line.
  19.           % Try each possible bird answers.
  20.           for k = 1 to K
  21.                % The bird-and-friend algorithm: The bird tells us to put k words on the
  22.                last line. We ask the friend for an optimal printing of the first i − k words.
  23.                He gives us optSol[i − k], which he had stored in the table. To this we add
  24.                the bird’s k words on a new last line. This gives us optSolk , which is a best
  25.                printing of the first i words from amongst those printings consistent with
  26.                the bird’s answer.
  27.                % optSolk = optSub[i − k], k
  28.                costk = cost[i − k] + (M − k + 1 − ij =i−k+1 l j )3
  29.           end for
  30.           % Having the best, optSolk , for each bird’s answer k, we keep the best of these
  31.           best.
  32.           k min = a k that minimizes costk
  33.                birdAdvice[i] = k min
  34.          % optSol[i] = optSolkmin
  35.          % cost[i] = costkmin
  36.     end for
  37.     optSol = PrintingNeatlyWithAdvice
  38.     return optSol, cost[n]
  39. end algorithm
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement