Dynamic Programming - Restoring white space between words in a file

In summary: If you get to the end of the file and still haven't found a solution, then you have failed. In summary, the program will look for all possible first words, skip over them, and look for punctuation marks. If it can't find a solution, it will back up to the previous word and try again.
  • #1
MattS134
2
0
Poster warned about not using the homework template
All the white space among words in a text file was lost. Write a C++ program which using dynamic programming to get all of the possible original text files (i.e. with white spaces between words) and rank them in order of likelihood with the best possible runtime.

You have a text file of dictionary words and the popularity class of the word (words are listed from popularity 1-100 (being most popular words), 101-200, etc)

- Input is a text file

- Output should be a series of files, each containing one the possible original documents and one document containing the ranking of likelihood of the outputs

- Use the right data structure for storing the data structure in memory, as this will impact the runtime

- Only dictionary words will be in the input text file

- Punctuation such as: . ? ! ; should be handled by program

- Remove words with non-English letters from dictionary

- Remove all the single letter words except for 'I' , 'A', and 'a' from dictionary or handle them in another way. (Important so there aren't infinite possible solutions)

To Test:

- Create text files using words in the dictionary. The ranking of outputs is in part based on the popularity class of the words used

I know how to use filestreams to store information from the file, but I'm not sure what data structure to use to store the dictionary for max efficiency
Also not sure of the logic of generating multiple output text files and how to create all possible original documents
 
Physics news on Phys.org
  • #2
You have a tree-walk. In each case, you will look at the start of the text and look for all possible first words. The skip past that word and repeat the process. Whenever you get to the end of the file or punctuation, you have success. So back up to the previous word and look for another. Whenever you get to a character string that cannot form a word, that string has failed to generate a solution. So, once again, back up to the previous word and look for another.
 

1. What is dynamic programming?

Dynamic programming is a method for solving complex problems by breaking them down into smaller subproblems and storing the solutions to these subproblems in a table. This allows for more efficient computation and can often result in faster algorithms.

2. How does dynamic programming help with restoring white space between words in a file?

In the context of restoring white space between words in a file, dynamic programming can be used to find the optimal solution for inserting spaces between words. By breaking the problem down into smaller subproblems and storing the solutions, dynamic programming can efficiently determine the best placement of spaces to restore the original text.

3. Can dynamic programming be used for any type of file?

Yes, dynamic programming can be used for any type of file as long as the problem can be broken down into smaller subproblems and the solutions can be stored in a table. This method is commonly used for text files, but it can also be applied to other types of files such as images or videos.

4. What are the advantages of using dynamic programming for restoring white space in a file?

One advantage of using dynamic programming for restoring white space in a file is that it can significantly improve the efficiency of the algorithm. By storing solutions to subproblems, the algorithm does not need to repeatedly compute the same subproblems, resulting in faster computation. Additionally, dynamic programming can handle files of any size, making it a versatile approach.

5. Are there any limitations to using dynamic programming for restoring white space in a file?

While dynamic programming can be a powerful method for solving complex problems, it may not always be the most efficient approach. In some cases, a simpler algorithm may be more appropriate and yield similar results. Additionally, dynamic programming may require more space to store solutions, which can be a limitation for larger files.

Similar threads

  • Engineering and Comp Sci Homework Help
Replies
7
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
14
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
1
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
6
Views
3K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
4K
  • Engineering and Comp Sci Homework Help
Replies
3
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
4
Views
12K
  • Engineering and Comp Sci Homework Help
Replies
15
Views
2K
  • Engineering and Comp Sci Homework Help
Replies
5
Views
2K
Back
Top