# Reconstructing Corrupted Text Document Using Dynamic Programming Algorithm

## Dynamic Programming Algorithm for Reconstructing Corrupted Text

You are given a string of n characters s[1 : : : n], which you believe to be a corrupted text document in which all punctuation has vanished (so that it looks something like itwasthebestoftimes...). You wish to reconstruct the document using a dictionary, which is available in the form of a Boolean function dict(): for any string w, dict(w) = true if w is a valid word false otherwise.

(a) Give a dynamic programming algorithm that determines whether the string s[] can be reconstituted as a sequence of valid words. The running time should be at most O(n^2), assuming calls to dict take unit time.

## Explanation:

Given that we need to determine if the string s[] can be reconstructed as a sequence of valid words, we can define a recurrence relation D(k) where D(k) = true if [1 : : : k] is a valid sequence of words or false otherwise.

To determine D(n), we observe that the subproblem s[1 : : : k] is a valid sequence of words if s[1 : : : 1] is a valid sequence of words and s[1 + 1 : : : k] is a valid word. Therefore, the recurrence relation for D(k) can be defined as follows:

D(k) = false maximum (D[l]∧DICT(s[1 + 1 : : : k]) otherwise

## Algorithm:

Valid sentence (s, k)

D[1 : : : k] ∦ array of boolean variables.

for a ← 1 to m do; d(0) ← false

for b ← 0 to a - j do;

if D[b] ∧ DICT s([b + 1 : : : a]) d (a) ← True

## (b) Output:

If D[k] = True, the algorithm should output the corresponding sequence of words by using a stack to print the strings in order.

Algorithm Output:

stack = temp stack // stack is used to print the strings in order

c = k while C > 0

stack push (s[w(c)] : : : C) // w(p) is the position in s[1 : : : k] of the valid word at position c, P = W (p) - 1

Output stack = 0 = cheers. I hope this helps!!!

Can you explain how the dynamic programming algorithm determines whether a given string can be reconstructed as a sequence of valid words?

The dynamic programming algorithm determines the validity of a string by breaking it down into subproblems and recursively checking if each subproblem can be reconstructed as a valid word sequence. By defining a recurrence relation and utilizing a boolean function dict() to check the validity of each word in the string, the algorithm can efficiently determine if the entire string can be reconstructed using valid words.