← 返回 coinbase 的题目列表Parse and group log lines by thread id, then sort by timestamp
类型:online_judge
Problem
You are given N log lines (strings). Each line follows the format:
<timestamp> <thread_id> <level> <message>
timestamp: an ISO-8601 time string, e.g. 2024-01-01T10:00:00Z
thread_id: a no-space identifier, e.g. t1, thread-007
level: a no-space token such as INFO, WARN, ERROR
message: the rest of the line (may contain spaces)
Group logs by thread_id, and within each group sort the log lines by timestamp in ascending order.
Output
For each thread_id:
print the thread_id on its own line
then print all original log lines for that thread (unchanged), sorted by timestamp ascending
Thread groups must be printed in ascending lexicographic order of thread_id.
Input (stdin)
Line 1: integer N
Next N lines: one log line each
Constraints
1 <= N <= 2 * 10^5
each line length <= 500
timestamps are valid and can be compared lexicographically to reflect chronological order.
Example
Input:
5
2024-01-01T10:00:02Z t1 INFO start
2024-01-01T10:00:01Z t2 WARN w1
2024-01-01T10:00:03Z t1 INFO end
2024-01-01T10:00:00Z t1 ERROR boom
2024-01-01T10:00:02Z t2 INFO ok
Output:
t1
2024-01-01T10:00:00Z t1 ERROR boom
2024-01-01T10:00:02Z t1 INFO start
2024-01-01T10:00:03Z t1 INFO end
t2
2024-01-01T10:00:01Z t2 WARN w1
2024-01-01T10:00:02Z t2 INFO ok
Example
Input
5
2024-01-01T10:00:02Z t1 INFO start
2024-01-01T10:00:01Z t2 WARN w1
2024-01-01T10:00:03Z t1 INFO end
2024-01-01T10:00:00Z t1 ERROR boom
2024-01-01T10:00:02Z t2 INFO ok
Output
t1
2024-01-01T10:00:00Z t1 ERROR boom
2024-01-01T10:00:02Z t1 INFO start
2024-01-01T10:00:03Z t1 INFO end
t2
2024-01-01T10:00:01Z t2 WARN w1
2024-01-01T10:00:02Z t2 INFO ok