Question

Develop a C++ PROGRAM which will find a hidden sentence in a list of random words...

Develop a C++ PROGRAM which will find a hidden sentence in a list of random words using map function Open this text file named shuffled_words.txt in your program If it matters, you may presume there are an even number of words in the file Place the contents of the file into an appropriate data structure in the following manner: Grab a pair of strings from the file (unless end of file is reached) Each string is separated by a space The first string is the key The second string is the value So both the key and value are strings Place the key/value pair into your data structure Words are chained together such that a key will give its value like normal, but that value may also be a key in the map, which can find another value and so forth For example... If... map["why"] = "hello" map["hello"] = "there" map["there"] = "everyone" map["everyone"] does not exist This means if I start with "why" and concatenate the map values together I get the sentence "why hello there everyone" Here is what a graphical representation of what the data structure might look like: There is only one sentence chained together in the entire text file. All other map entries are immediate dead ends. Take the giant word list, find the longest word chain as described above, and print out the sentence that chain makes.

Homework Answers

Answer #1

Solution -

Below is the required code in C++. The data structure used in this code is Vector along with pairing.

#include<bits/stdc++.h>

#include <iterator>

using namespace std;

int main(){

fstream file;

// declaring a vector of pairs

vector<pair<string,string>>v;

// opening the file named "input.txt"

file.open("input.txt",ios::in);

if(!file)

cout<<"No such file";

else {

string s,prev;

file >> s;                      // reading the first word of sentence

prev = s;

int i=0;

while (file >> s) {      // reading the rest of the words

v.push_back(make_pair(prev,s)); // making pair like ("why","hello") and storing in vector

prev = s;                        // storing the current word as prev for next coming word to map

}

}

// displaying the chain

for(int i=0;i<v.size();i++)

cout << "(" << v[i].first << "->" << v[i].second << ")"<<endl;

file.close();  

return 0;

}

Explanation -

In this vector of pairs if used to store the mapping of prev word with next word.

An object ifstream is created to open the file in read mode.

First we read the very first word of the sentence and store it as prev for the next word.

Now we read all the other words using while loop and keep pushing the pair in the vector. The first pair will be (prev, current word). Now the current word will become prev for next coming word in the file. This keep repeating for other words until the end of the file.

At last we display the longest chain from the vector and close the file.

Note - We could have used map but since map keeps the key-value pair in sorted order then our original order of the sentence would be lost and we would then need another data structure like vector to store the original order of words in the sentence. For this reason, vector of pairs is used which fulfills the requirement with less space.

Refer to the screenshot below for code and output -

Thanks

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
** Language Used : Python ** PART 2 : Create a list of unique words This...
** Language Used : Python ** PART 2 : Create a list of unique words This part of the project involves creating a function that will manage a List of unique strings. The function is passed a string and a list as arguments. It passes a list back. The function to add a word to a List if word does not exist in the List. If the word does exist in the List, the function does nothing. Create a test...
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings....
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings. You must base your code on the doubly linked list implementation given in my Week 8 slides. Change the code so that instead of an ‘int’ each node stores a string (choose a suitable size). Each node should also have a next node pointer, and previous node pointer. Then write functions to implement the following linked list operations: • A printList function that prints...
JAVA PROGRAMMING: Write a program called TimeSymbolTables that creates three symbol tables, each of a different...
JAVA PROGRAMMING: Write a program called TimeSymbolTables that creates three symbol tables, each of a different implementation. Each symbol table will contain as a key a word read from a text file and as a value the number of times that word occurs in the text file. Have the program fill the first symbol table with these counts, keeping track of how long that takes using a Stopwatch object. It then does the same thing with the second symbol table....
This assignment involves using a binary search tree (BST) to keep track of all words in...
This assignment involves using a binary search tree (BST) to keep track of all words in a text document. It produces a cross-reference, or a concordance. This is very much like assignment 4, except that you must use a different data structure. You may use some of the code you wrote for that assignment, such as input parsing, for this one. Remember that in a binary search tree, the value to the left of the root is less than the...
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question....
#Linked Lists and Classes #C++ Hi, please use singly linked list method to do this question. Thank you! Here’s the contents of a file called example.cpp: // example.cpp #include "LinkedList.h" #include <iostream> #include <string> using namespace std; int main() { cout << "Please enter some words (ctrl-d to stop):\n"; LinkedList lst; int count = 0; string s; while (cin >> s) { count++; lst.add(remove_non_letters(s)); } // while cout << "\n" << count << " total words read in\n"; cout <<...
Language: C++ You're given a 1000-line text file, phoneno.txt, where each line consists of a 5-digit...
Language: C++ You're given a 1000-line text file, phoneno.txt, where each line consists of a 5-digit ID# and a phone# in the format of ###-###-####. The data were generated randomly so there might be duplicates in the IDs. You're asked to do the following by using standard library algorithms as much as possible: 1. read the file into a map which has an integer for key (ID#) and a string for value (phone#), this allows the duplicates to be removed....
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g,...
For a C program hangman game: Create the function int setup_game [int setup_game ( Game *g, char wordlist[][MAX_WORD_LENGTH], int numwords)] for a C program hangman game. (The existing code for other functions and the program is below, along with what the function needs to do) What int setup_game needs to do setup_game() does exactly what the name suggests. It sets up a new game of hangman. This means that it picks a random word from the supplied wordlist array and...
Strings The example program below, with a few notes following, shows how strings work in C++....
Strings The example program below, with a few notes following, shows how strings work in C++. Example 1: #include <iostream> using namespace std; int main() { string s="eggplant"; string t="okra"; cout<<s[2]<<endl; cout<< s.length()<<endl; ​//prints 8 cout<<s.substr(1,4)<<endl; ​//prints ggpl...kind of like a slice, but the second num is the length of the piece cout<<s+t<<endl; //concatenates: prints eggplantokra cout<<s+"a"<<endl; cout<<s.append("a")<<endl; ​//prints eggplanta: see Note 1 below //cout<<s.append(t[1])<<endl; ​//an error; see Note 1 cout<<s.append(t.substr(1,1))<<endl; ​//prints eggplantak; see Note 1 cout<<s.find("gg")<<endl; if (s.find("gg")!=-1) cout<<"found...
Use Python to Complete the following on a single text file and submit your code and...
Use Python to Complete the following on a single text file and submit your code and your output as separate documents. For each problem create the necessary list objects and write code to perform the following examples: Sum all the items in a list. Multiply all the items in a list. Get the largest number from a list. Get the smallest number from a list. Remove duplicates from a list. Check a list is empty or not. Clone or copy...
Finish the CustomerAccountTransactions program that reads customer accounts receivable data from a file and then applies...
Finish the CustomerAccountTransactions program that reads customer accounts receivable data from a file and then applies a series of transactions to the accounts. Its main() method uses the CustomerAccounts class to manage a collection of CustomerAccount objects: reading customer accounts data & transactions, and obtaining a String showing the customer accounts data after the operations are complete. You will need to complete the readCustomerAccounts () and applyTransactions() methods in the CustomerAccounts class. First, please fill in your name in the...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT