Chapter 6: Q4E (page 192)
You are given a string of n characters , which you believe to be a corrupted text document in which all punctuation has vanished (so that it looks something like 鈥渋twasthebestoftimes...鈥). You wish to reconstruct the document using a dictionary, which is available in the form of a Boolean function dict(.): for any string w,
Give a dynamic programming algorithm that determines whether the string can be reconstituted as a sequence of valid words. The running time should be at most , assuming calls to dict take unit time.
In the event that the string is valid, make your algorithm output the corresponding sequence of words.
Short Answer
We are going to use dynamic programming to reconstruct the given document, by using dict() function.
Before we create our algorithm, we will define our sub-problem so:
The function means if the sub-string(s) we encounter from 鈥榠鈥 to 鈥榡鈥 is valid word according to dictionary.
And we are simultaneously we storing the previous word鈥檚 validity and checking using.