#K78872. Top K Most Frequent IP Pairs
Top K Most Frequent IP Pairs
Top K Most Frequent IP Pairs
Given a list of log entries, each containing a source IP address, a destination IP address, and a timestamp, your task is to identify the top K most frequent IP pairs. An IP pair is defined in the format source IP -> destination IP.
If two pairs have the same frequency, they should be sorted lexicographically by the source IP and then by the destination IP. The frequency of a pair \(f(ip_1, ip_2)\) is simply the number of times the pair appears in the log data.
You should read from standard input and write the results to standard output.
inputFormat
The input is read from standard input and has the following format:
- The first line contains two integers N and K separated by a space, where N is the number of log entries and K is the number of top IP pairs to output.
- The following N lines each contain a log entry with three fields: a source IP, a destination IP, and a timestamp. The fields are separated by spaces. The timestamp is given as a single token without spaces (for example, 2023-01-01_12:00:00).
outputFormat
Output the top K IP pairs, one per line, in the format:
source IP -> destination IP
If there are fewer than K unique pairs, output all available pairs.## sample
5 2
192.168.1.1 192.168.1.2 2023-01-01_12:00:00
192.168.1.1 192.168.1.2 2023-01-01_12:05:00
192.168.1.2 192.168.1.3 2023-01-01_12:10:00
192.168.1.1 192.168.1.2 2023-01-01_12:15:00
192.168.1.2 192.168.1.3 2023-01-01_12:20:00
192.168.1.1 -> 192.168.1.2
192.168.1.2 -> 192.168.1.3
</p>