IBMOA #28
Get Latest K Requests
Estimated EasyJudge-ready
Topics (AI-labelled)Hashing
After receiving all requests, output the most recent k distinct request IDs, ordered from most recent to least recent by the last time each ID appeared. The original example makes clear that a repeated ID is reported only once. If there are fewer than k distinct IDs, this site's output convention is -1.
Input
The first line contains n k, followed by n lines of request IDs. On this site 1≤n,k≤100000, and each ID consists of 1 to 20 non-newline Unicode scalar characters and may contain spaces; read input line by line without trimming spaces.
Output
If there is no solution, output only -1; otherwise output k on the first line, followed by k lines of request IDs from most recent to least recent, with spaces preserved.
Example 1
Input:
5 3
item1
item2
item3
item1
item3
Output:
3
item3
item1
item2
Example 2
Input:
2 2
a
a
Output:
-1
Example 3
Input:
3 2
a b
a
a b
Output:
2
a b
a
Example notes
The examples output, line by line, 3, item3, item1, item2; -1; and 2, a b, a surrounded by spaces, respectively.
Editorial
Loading solution…