#C13347. Top IP Address Analysis
Top IP Address Analysis
Top IP Address Analysis
You are given a log file containing several lines. Each line in the log file has the following format:
YYYY-MM-DD HH:MM:SS IP METHOD PATH STATUS
Your task is to determine the top N IP addresses which made the most access attempts. In other words, count how many times each IP address appears in the log and output the top N IP addresses along with their counts.
Let \(L\) be the number of log entries, and let each log entry have the format shown above. You need to output the result in descending order by count. If there is a tie, the order of IP addresses can be determined lexicographically.
Note: The input is read from standard input (stdin
) and the output should be written to standard output (stdout
).
inputFormat
The first line contains an integer N
, indicating the number of top IP addresses to output.
The second line contains an integer L
, representing the number of log entries.
The following L
lines each contain a log entry in the format:
YYYY-MM-DD HH:MM:SS IP METHOD PATH STATUS
outputFormat
Output the top N
IP addresses along with their access counts, one per line. Each line should contain the IP address followed by its count, separated by a space.
2
6
2023-10-01 10:00:01 192.168.1.1 GET /index.html 200
2023-10-01 10:00:05 192.168.1.2 POST /submit 200
2023-10-01 10:00:12 192.168.1.1 GET /about.html 200
2023-10-01 10:00:15 192.168.1.3 GET /index.html 404
2023-10-01 10:00:18 192.168.1.2 GET /home 200
2023-10-01 10:00:20 192.168.1.1 POST /login 500
192.168.1.1 3
192.168.1.2 2
</p>