#C2386. Most Frequent Words
Most Frequent Words
Most Frequent Words
Given an integer n and several paragraphs of text, your task is to identify the n most frequent words across all paragraphs. The words are compared in a case-insensitive manner and any punctuation should be ignored. In the case of ties, the words should be sorted in alphabetical order.
The input consists of an integer n that indicates how many words to output, followed by an integer m which indicates the number of paragraphs, and then m lines each containing a paragraph of text. The output should contain the n most frequent words separated by a single space.
Note: All punctuation characters are to be removed and all letters converted to lowercase before processing.
For example, if n = 3 and the paragraphs are:
Hello world! This is a test. Hello again. Test the function. Make sure it works, as this is important. Hello from the other side. Test case number three, hello.
Then the output should be: hello test is
inputFormat
The first line of input contains an integer n
, which specifies the number of most frequent words to output. The second line contains an integer m
, representing the number of paragraphs. This is followed by m
lines, each containing a paragraph of text.
Input format:
n m paragraph_1 paragraph_2 ... paragraph_m
outputFormat
Output the n
most frequent words separated by a single space in one line. The words should be in descending order of frequency. In case of ties, they are sorted in alphabetical order.
3
3
Hello world! This is a test. Hello again.
Test the function. Make sure it works, as this is important.
Hello from the other side. Test case number three, hello.
hello test is