This page is still under construction.

Parts of this page are still being built. What you see may change.

Computerizing a Stockroom

Time limit1sMemory limit128 MB

Summary
Parse handwritten stockroom transactions in chronological order, track computers and part inventories, then print owner and stock summaries sorted by a custom phrase order.
Level

Medium6 of 10

Topics
Implementation, String, Hash map, Sorting
Solved
No attempts yet

Problem

Jobs is a stock clerk who has worked at a computer-manufacturing company since it was founded. He is responsible for every transaction in the stockroom where computer parts are stored. Jobs is old-fashioned: he still records every transaction by hand in his booklet and has always resisted computerizing the records. That changed today, when his boss asked him for a summary report on the current state of the stockroom — the computers currently handed out to employees, and the working and non-working parts still in the stockroom. The report is due tonight and he has no time to compile it by hand from all the transactions in his booklet, so he asks you to write a program that reads all transactions and produces the report.

Each transaction in the booklet begins with a date-time, followed by one of these templates:

  • Bought <NUM> <PIECE>.
  • Assembled a computer for <PERSON> using <PIECES>.
  • Got the computer of <PERSON> back and disassembled it.
  • Found that <A> <PIECE> is not working.
  • <A> <PIECE> is repaired and now can be used again.

The placeholders above are defined as follows (the first letter is capitalized when the placeholder starts a sentence):

  • <A>: either "a" or "an", depending on the word that follows.
  • <NUM>: either <A> (meaning 1), or "K items of", where K is an integer greater than 1.
  • <PERSON>: the full name of an employee — one or more space-separated words, each starting with a capital letter.
  • <PIECES>: a list of <NUM> <PIECE> entries separated by commas. The list always has at least 2 entries, and an extra "and" is placed after the last comma (before the final entry).
  • <PIECE>: a phrase naming a computer part (such as RAM or CPU), possibly with extra details describing the model, speed, capacity, etc.

You may assume that each entity (a part type or an employee) is always referred to by a single, unique, case-sensitive phrase, and that no two different entities share the same phrase, even when compared case-insensitively. Every part that is bought is working when bought; only working parts are used to assemble a computer. Each employee owns at most one computer at any time, and every transaction is logically valid at the moment it is written.

Input

The input contains several test cases. Each test case begins with a line containing the integer nn (1≤n≤5001 \le n \le 500), the number of transactions, followed by nn transactions.

Each transaction begins with a date-time in the format "year-month-day hour", where year, month, day, and hour lie in the ranges [2000,2012][2000, 2012], [1,12][1, 12], [1,31][1, 31], and [0,23][0, 23] respectively; values below 10 may be padded with a leading "0". The date-times within a single test case are all distinct, and the transactions are not necessarily listed in chronological order — you must process them in increasing order of their date-times. The date-time is separated from the transaction sentence by the string " - " (space, hyphen, space).

Every number appearing in <NUM> is less than 10510^5. Consecutive words are separated by a single space. Employee names and part-type phrases are wrapped in double quotation marks, and every character inside the quotation marks is alphanumeric.

The input terminates with a line containing a single "0", which is not a test case.

Output

For each test case, output several lines.

First, output the number of employees who currently own a computer, XX, using the following format:

  • If X>1X > 1: There are X employees who currently have a computer:
  • If X=1X = 1: There is one employee who currently has a computer:
  • If X=0X = 0: No computer is currently given out to the employees.

Then, if X>0X > 0, print XX lines, each containing the full name of one such employee. These lines must be sorted in lexicographic order.

Next, print one line for every part type that appears in the booklet — exactly one line per type — sorted in lexicographic order of the part phrases. For a part phrase <PIECE>, its line must be one of:

  • No items left: There is no "<PIECE>" left in the stockroom.
  • Exactly one item, working: There is one "<PIECE>" left in the stockroom which is working.
  • Exactly one item, not working: There is one "<PIECE>" left in the stockroom which is not working.
  • XX items (X>1X > 1), all working: There are X items of "<PIECE>" left in the stockroom, all working.
  • XX items (X>1X > 1), all not working: There are X items of "<PIECE>" left in the stockroom, all not working.
  • XX items (X>1X > 1), YY working and ZZ not working (Y,Z>0Y, Z > 0): There are X items of "<PIECE>" left in the stockroom, Y working and Z not working.

Print a line containing "###" between every two consecutive test cases.

When comparing two multi-word phrases, compare their first words; if those are equal, compare the second words, and so on. If every word of the shorter phrase matches the corresponding word of the longer phrase, the shorter phrase comes first. When comparing two words character by character, digits rank before letters, and letters are compared case-insensitively. Thus "A AB" < "A10" < "A2" < "aa" = "AA" < "AA B".

Note: only parts physically in the stockroom count as "left in the stockroom"; parts inside a currently-assembled computer are not counted. When a computer is disassembled, all of its parts return to the stockroom as working. A "Found that ... is not working" transaction turns one working stockroom item of that part into a non-working one, and a "... is repaired ..." transaction turns one non-working stockroom item back into a working one.

Examples3

  1. Example 1

    Input
    6
    2011-3-18 12 - Bought 3 items of "CPU Pentium IV 2GHz".
    2011-11-1 10 - Found that a "1GB RAM" is not working.
    2012-1-20 11 - Bought an "optical mouse".
    2011-11-21 15 - A "CPU Pentium IV 2GHz" is repaired and now can be used again.
    2011-03-18 9 - Bought 2 items of "1GB RAM".
    2011-10-18 14 - Found that a "CPU Pentium IV 2GHz" is not working.
    16
    2012-2-1 08 - Bought 3 items of "motherboard".
    2012-2-1 09 - Bought 3 items of "Green case".
    2012-2-1 10 - Bought 3 items of "dual core CPU".
    2012-2-1 11 - Bought 3 items of "keyboard".
    2012-2-1 12 - Bought 2 items of "optical mouse".
    2012-2-1 13 - Bought 4 items of "2GB DDR3 RAM".
    2012-2-1 14 - Bought 4 items of "500GB Hard".
    2012-2-1 15 - Bought a "DVD Drive".
    2012-2-2 09 - Assembled a computer for "Abbaas" using a "motherboard", and a "Green case".
    2012-2-2 10 - Assembled a computer for "Dehghaan Fadaakaar" using a "motherboard", a "Green 
    case", a "dual core CPU", a "keyboard", an "optical mouse", 2 items of "2GB DDR3 RAM", a 
    "DVD Drive", and a "500GB Hard".
    2012-2-2 11 - Assembled a computer for "Kokab Khaanum" using a "motherboard", a "Green 
    case", a "dual core CPU", a "keyboard", an "optical mouse", 2 items of "2GB DDR3 RAM", 
    and a "500GB Hard".
    2012-2-3 10 - Got the computer of "Kokab Khaanum" back and disassembled it.
    2012-2-4 10 - Found that a "motherboard" is not working.
    2012-2-4 11 - Found that a "2GB DDR3 RAM" is not working.
    2012-2-4 12 - Found that a "2GB DDR3 RAM" is not working.
    2012-2-4 13 - Found that a "500GB Hard" is not working.
    0
    
    Expected output
    No computer is currently given out to the employees.
    There are 2 items of "1GB RAM" left in the stockroom, 1 working and 1 not working.
    There are 3 items of "CPU Pentium IV 2GHz" left in the stockroom, all working.
    There is one "optical mouse" left in the stockroom which is working.
    ###
    There are 2 employees who currently have a computer:
    Abbaas
    Dehghaan Fadaakaar
    There are 2 items of "2GB DDR3 RAM" left in the stockroom, all not working.
    There are 3 items of "500GB Hard" left in the stockroom, 2 working and 1 not working.
    There are 2 items of "dual core CPU" left in the stockroom, all working.
    There is no "DVD Drive" left in the stockroom.
    There is one "Green case" left in the stockroom which is working.
    There are 2 items of "keyboard" left in the stockroom, all working.
    There is one "motherboard" left in the stockroom which is not working.
    There is one "optical mouse" left in the stockroom which is working.
    
  2. Example 2

    Input
    4
    2005-1-1 10 - Bought 2 items of "CPU".
    2005-1-1 11 - Bought a "case".
    2005-1-2 09 - Assembled a computer for "John Doe" using a "CPU", and a "case".
    2005-1-3 08 - Found that a "CPU" is not working.
    0
    
    Expected output
    There is one employee who currently has a computer:
    John Doe
    There is no "case" left in the stockroom.
    There is one "CPU" left in the stockroom which is not working.
    
  3. Example 3

    Input
    4
    2000-1-1 01 - Bought a "A2".
    2000-1-1 02 - Bought a "AA B".
    2000-1-1 03 - Bought a "A10".
    2000-1-1 04 - Bought a "A AB".
    0
    
    Expected output
    No computer is currently given out to the employees.
    There is one "A AB" left in the stockroom which is working.
    There is one "A10" left in the stockroom which is working.
    There is one "A2" left in the stockroom which is working.
    There is one "AA B" left in the stockroom which is working.