← 返回 apple 的题目列表Top N Frequent Logs
类型:online_judge
Problem: Top N Frequent Logs
Given a list of log lines logs and an integer n, output the top n distinct log lines with the highest frequencies.
Input Format
The first line contains two integers m and n:
m is the total number of log lines;
n is the number of top logs to output.
The next m lines each contain one log string. A log string may contain spaces.
Output Format
Output at most n log lines, one per line, using the following ordering:
Higher frequency comes first;
If frequencies are equal, sort by the log string in lexicographical ascending order;
If there are fewer than n distinct logs, output all distinct logs.
Constraints
1 <= m <= 100000
1 <= n <= 100000
Each log line has length at most 1000
The total length of all log lines is at most 1000000
Example
Input:
7 2
ERROR disk full
INFO started
ERROR disk full
WARN high memory
INFO started
ERROR disk full
WARN high memory
Output:
ERROR disk full
INFO started
Explanation:
ERROR disk full appears 3 times;
INFO started appears 2 times;
WARN high memory appears 2 times;
for equal frequencies, INFO started is lexicographically smaller than WARN high memory.
Example
Input
7 2
ERROR disk full
INFO started
ERROR disk full
WARN high memory
INFO started
ERROR disk full
WARN high memory
Output
ERROR disk full
INFO started