Monster Warehouse
Time limit1sMemory limit2048 MB
Simulate a warehouse of nested containers under BUY, SELL, UNPACK, and PACK requests, then answer COUNT, CONTAINS, and MIN queries about goods.
- Level
Medium5 of 10
- Topics
- Simulation, Tree, String, BFS
- Solved
- No attempts yet
Problem
Mike and Sally work in the warehouse of Monster Inc. as storekeepers. Their daily tasks are to process incoming requests and update the inventory of the Warehouse. Requests only include buying, selling, unpacking, and packing containers. The warehouse holds goods and containers and has unlimited space. A container may contain goods or other containers as sub-containers.
The exact format of the requests is given below.
BUY <CONTAINER_DESCRIPTION>SELL <CONTAINER_ID>UNPACK <CONTAINER_ID>PACK <CONTAINER_DESCRIPTION>
Each container that is not inside any other container is uniquely identified by a positive integer ID. IDs are assigned sequentially, starting from . An ID is valid if and only if its container exists in the warehouse. Otherwise, it is invalid.
A container description is enclosed in parentheses and lists its contents, which are goods or sub-containers. A good is identified only by its name, which consists of case-insensitive English letters. Multiple units of a good may be available. To give a quantity, place a positive integer N before or after the good name, separated from it by one whitespace. Here is the number of units of the good. For example, ((tomato, potato), 4 celery, (wood, (silk 3, banana 2))) describes a container with four units of celery and two sub-containers.
The description of each request is as follows.
- BUY: A new container is transferred into the warehouse and is assigned an ID.
- SELL: An existing container with the given ID is shipped out, and its ID becomes invalid.
- UNPACK: All goods and sub-containers are extracted from the container and added to the warehouse. The sub-containers become new containers and get their own IDs. The new containers are numbered in the order they appear in the container description, from left to right. For instance, if the following two lines are the first requests, one unit of celery is added, three containers with IDs 2, 3, and 4 are added to the warehouse, and ID 1 becomes invalid.
BUY (celery , (Banana), (Celery), (celery))
UNPACK 1
- PACK: The goods specified in a PACK request are grouped into a new container, which is assigned the next available ID.
Mike and Sully process the requests in the order they are received. Any request with an invalid container ID is discarded. For a PACK request, they must also check that the warehouse has enough units of each good.
Roz, the agent of Monster Inc., once told Mike, "I'm watching you, Wazowski. Always watching. Always." She rolled her desk into their office and asked for requests and reports. She is looking for every detail. She reviews each request and might ask a few questions. Her questions can be any of the following types.
? COUNT <good>: How many units of the given good exist outside of containers?? CONTAINS <good>: How many containers with an ID have the given good? That is, the good is directly in the container, or a recursive sub-container of it contains that good.? MIN <good>: At least how many containers must be unpacked to reach one unit of the good? If it is impossible, the answer is -1.
Mike and Sully are expected to answer these queries using just one integer.
Before helping Mike and Sully, read the samples carefully.
Input
The input consists of requests or queries from Roz while she is reviewing (). Each appears on a separate line. The name of each good is limited to 100 characters. Each container description can have at most 5000 characters, and the input size is less than characters.
Output
After each BUY, SELL, PACK, or UNPACK request, print OK if the request is not discarded. Otherwise, print DISCARD. If the request is UNPACK, print OK and then the number of containers added to the warehouse. See the samples for details. For each question from Roz, print one integer on its own line.