Common Words
Time limit1sMemory limit128 MB
For each data set, count word frequencies, find the k-th most common words, and print them alphabetically after a title line.
- Level
Medium4 of 10
- Topics
- Hash map, Sorting, String, Implementation
- Solved
- No attempts yet
Problem
You are given a sequence of words taken from a newspaper article together with an integer . Find the -th most common word (or words).
Input
The first line contains an integer , the number of data sets. Each data set begins with a line holding two integers and , followed by lines. Each of those lines contains one word made of at most lowercase letters. A single data set contains at most words.
Output
For each data set, determine the -th most common word(s). A word is the -th most common when exactly distinct words occur more frequently than . Such a word may not be unique (several words can tie for the -th place) or may not exist at all.
First print a title line <ordinal of k> most common word(s):, where the ordinal uses English notation (1st, 2nd, 3rd, 4th, 5th, …). Then print every word that is the -th most common, one per line, in ascending lexicographic (alphabetical) order. If no such word exists, print only the title line. Separate consecutive data sets with a single blank line.