Hi, I want to implement a program on finding the k most frequently occurring words from a text file. The input will be like:
$facebook 5
$amazon 3
$twitter 8
3
.
.
^ Like that
Basically, the input words and their frequencies are given, and if the word starts with $, I need to input the frequency given after the space to a hash map
If the line doesn't start with $, then the digit is k, and I need to output the k most frequent words.
My question is, how can I input the line, extract the first character and check if it's a $ or a number, and then do the rest of the operations on the line I have input?
It's strange that you're throwing around words like 'hash map', when you seem to be struggling with the basics of just "reading a line from a file into a string".
Did you know - you don't NEED to project your negativity onto someone! If you can't appreciate the efforts of someone who's trying to learn, then the least you could do is to KEEP QUIET and LET THEM. I don't think you were born with coding skills. Must have learnt, right? Let others do the same!
#include <iostream>
#include <string>
#include <cctype>
usingnamespace std;
int main()
{
string line;
getline(cin, line);
if (line[0] == '$')
{
// store the word and its frequency
}
else
{
if (isdigit(line[0]))
{
// convert line into an int k and print the k frequent words
}
}
}
The "hash maps" in C++ is called map, unordered_map and multi_map.
I don't think they are the best option for this task since you need to sort the words by frequency, not the word themselves.
I would prefer a vector with structs.
Hey, thanks a lot for providing that!
The thing is, I can't choose how to implement it. Its the problem statement that's given to me that specifies the use of hash tables.
I'll share a part of the problem statement:
Keywords will be given from an input file together with their frequencies. You need to use a max priority structure to find the most popular keywords.
You must use the following data structures for the implementation.
-Max Fibonacci heap: to keep track of the frequencies of keywords
-Hash table: keywords should be used as keys for the hash table and value is the pointer to the corresponding node in the Fibonacci heap.
You will need to perform the increase key operation many times as keywords appear in the input keywords stream. You are only required to implement the Fibonacci heap operations that are required to accomplish this task.