Recently Used Documents
InterviewTime limit1sMemory limit512 MB
Simulate a most-recently-used list of capacity k: each opened document moves to the front, new ones are inserted there and the back is dropped when full, then print the final list.
- Level
Easy2 of 10
- Topics
- Simulation, Implementation, Array, Linked list
- Solved
- No attempts yet
Problem
Many programs keep a list of recently used documents, called the NKD list. As the name says, the list holds the documents the user opened most recently, so the user can reopen one without searching through every document. The list has a capacity, which is the largest number of documents it can hold at one time.
Every time the user opens a document, one of the following two things happens. The rule is the same whether the user picks the document from the list or opens it some other way.
- If the document is already somewhere in the list, it moves to the front of the list.
- Otherwise it is inserted at the front of the list. If that puts the list over its capacity, the last document in the list is thrown out.
The list is empty at the start. Given the capacity of the list and the order in which the user opens documents, determine the contents of the list after all of them have been opened.
Input
The first line contains the capacity of the list ().
The second line contains the number of documents the user opens, ().
Each of the next lines contains the name of one document the user opens, given in the order the user opens them. Every name is a string of at most 10 lowercase English letters and contains no spaces.
Output
Print the contents of the list after all of the documents have been opened, one document per line. The document printed on the first line is the one at the front of the list.