#C171. Top K Hashtags
Top K Hashtags
Top K Hashtags
You are given a list of hashtags along with their usage frequencies. Your task is to calculate and output the top ( k ) hashtags based on the following criteria:
- Sum the frequencies for hashtags that appear multiple times.
- Sort the hashtags by their total frequency in descending order.
- In case two hashtags have the same frequency, sort them in lexicographical (alphabetical) order.
If there are fewer than ( k ) unique hashtags, simply output all of them. Each hashtag should be printed on a new line.
For example, given the input:
3 2 #python 10 #java 15 #cpp 8
The output should be:
#java #python
This is because after summing the frequencies, the hashtag #java
has 15 and #python
has 10, which are the top two frequencies.
inputFormat
Input is read from standard input (stdin). The first line contains two integers ( n ) and ( k ), where ( n ) is the number of hashtag entries and ( k ) is the number of top hashtags to output. This is followed by ( n ) lines, each containing a hashtag (a string starting with '#') and an integer representing its frequency, separated by space.
outputFormat
Output the top ( k ) hashtags to standard output (stdout), one per line. The hashtags must be ordered by descending frequency. If multiple hashtags have the same frequency, they should be ordered lexicographically in ascending order.## sample
3 2
#python 10
#java 15
#cpp 8
#java
#python
</p>