There’s a printing war going on in The Office. Everyone in the office can no longer print whatever and whenever they want. Michael and Dwight are the protector of the printer, and they need your help to manage the printing queue!
You will receive Q commands, and each command is either SUBMIT, PRINT, CANCEL, and STATUS . Here are the rules in details:
SUBMIT, the command shape will look like this: SUBMIT <user> <file> <pages>
<file> must be <name>.<ext>, where:
<name> is at least 1 character, using only ABCDEFGHIJKLMNOPQRSTUVWXYZ, abcdefghijklmnopqrstuvwxyz, 0123456789, _- and <ext> is exactly pdf, txt or docx (lowercase). Otherwise, output REJECT BADNAME.<pages> must be digits only, 1 ≤ pages ≤ 100, if it is not within range, output REJECT BADPAGES .REJECT QUOTA .OK <id> . The <id> is the i-th validated printing job in the queue.<user> is 1-20 alphanumeric characters, case-sensitive.PRINT , the command shape will look like this: PRINT <n>
n jobs (n is integer), from the oldest to the latest.PRINTED <id> <user> <file> . Printing fewer than n jobs is not an error and does not produce IDLE.IDLE .CANCEL , the command shape will look like this: CANCEL <user>
CANCELLED <count> .STATUS, the command shape will look like this: STATUS
QUEUE <len> followed with a line of the front job NEXT <id> <user> .NEXT NONE .TOTAL <pages_printed> after all the commands have finished being inputted, followed with line(s) of who printed ≥ 1 page, sorted by pages descending, and then name ascending, i.e. <user> <pages> .REJECT BADREQUEST .Michael and Dwight thank your services!
Note: Crash Course about Loop: https://hackmd.io/@gladysvalerie/S1wWLYCYze
First line: integer Q (1 ≤ Q ≤ 1000)
Next Q lines: one command each, tokens separated by single spaces, according to the rules explained in the description.
Anything else described from the description, outputs REJECT BADREQUEST and the command is ignored.
Command keywords are uppercase.
Per command, checked in this exact order:
SUBMIT
<file> fails ^[A-Za-z0-9_-]+\.(pdf|txt|docx)$ → REJECT BADNAME<pages> not digits, or not 1 ≤ pages ≤ 100 → REJECT BADPAGESused[user] + pages > 50 → REJECT QUOTAOK <id>Ids start at 1 and increase by 1 per accepted job only; rejected submissions never consume an id. A job keeps its id after leaving the queue.
PRINT <n>
IDLE (one line, regardless of n).min(n, queue_length) jobs from the front, one PRINTED <id> <user> <file> line each. n = 0 on a non-empty queue outputs nothing.CANCEL <user>
CANCELLED <count>. Unknown user → CANCELLED 0. Already-printed pages are never refunded.STATUS
QUEUE <len>, then NEXT <id> <user> for the front job, or NEXT NONE if empty.After all Q commands:
TOTAL <pages_printed> — exactly once.<user> <pages> per user who printed ≥ 1 page, sorted by pages descending, then username ascending. Users with 0 printed pages are omitted entirely.