I'm looking to implement a way to perform text justification (flush left and right). Each output line can have a maximum width of M characters. Breaking of words is not allowed.
For example, please see "Justified (flush left and right)" in this wikipedia page: http://en.wikipedia.org/wiki/Justification_(typesetting).
I'm aware that there is a dynamic programming solution for optimal flush left, ragged right justification i.e. distribute the extra spaces at the end of the lines evenly such that the cost of extra spaces is optimal (this is also known as the 'word wrap' problem or 'printing neatly' problem). However, I'm not able to arrive at a dynamic programming or greedy approach for the full text justification problem.
Googling lead me to text justification based on Markov Chain Programming: http://www.rose-hulman.edu/Users/faculty/young/OldFiles/CS-Classes/csse220/200820/web/Programs/Markov/justification.html. But this seems complicated to me. If this is the best (and simplest) solution to the full text justification problem, then it will be great if somebody could explain the same in easy words.