Page 122, Exercise 4.9: The allusion to Viterbi in this exercise implies that there is a polynomial time solution to this problem when in fact it is NP-hard. The following re-wording should help.
(Advanced) Suppose someone took all the words in a sentence and reordered
them randomly. Write a program that takes as input such a bag of words and
produces as output a guess at the original order. You will need to use an N-gram
grammar produced by your N-gram program (on some corpus). This problem is NP-hard
so you will want to think of an approximate solution rather than an exact solution.
Applying beam-search and other advanced search techniques used for machine translation
search, introduced in Chapter 24, will be handy. This task is sometimes called