Under construction

Still being built. Things may change

Monster Warehouse

Time limit1sMemory limit2048 MB

Summary
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 11. 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 N<100N < 100 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 nn requests or queries from Roz while she is reviewing (1≤n≤50001 \le n \le 5000). 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 10610^6 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.

Examples3

  1. Example 1

    Input
    BUY (10 apple, 10 carrot, 10 banana, ())
    UNPACK 1
    UNPACK 2
    PACK (3 apple, (5 cArrot, 2 banana))
    PACK (3 apple, (5 carrOt, 1 banana))
    PACK (5 apple, (3 banana))
    ? MIN apple
    ? MIN CaRRoT
    ? CONTAINS apple
    
    Expected output
    OK
    OK, 1 container added.
    OK, No containers added.
    OK
    OK
    DISCARD
    0
    2
    2
    
  2. Example 2

    Input
    BUY (apple, (banana, 3 carrot))
    BUY ((banana, apple), (banana, carrot))
    ? MIN apple
    UNPACK 1
    ? COUNT apple
    ? COUNT carrot
    ? CONTAINS banana
    
    Expected output
    OK
    OK
    1
    OK, 1 container added.
    1
    0
    2
    
  3. Example 3

    Input
    BUY ((duck 2, carrot), 1 celery)
    ? MIN duck
    ? MIN carrot
    ? MIN celery
    ? MIN test
    SELL 10
    PACK (celery)
    UNPACK 1
    UNPACK 1
    PACK (celery)
    ? COUNT celery
    ? COUNT carrot
    ? CONTAINS celery
    ? CONTAINS carrot
    BUY ((duck 8, carrot), ((7 duck), celery))
    UNPACK 4
    UNPACK 5
    UNPACK 6
    ? COUNT DUCK
    ? MIN duck
    
    Expected output
    OK
    2
    2
    1
    -1
    DISCARD
    DISCARD
    OK, 1 container added.
    DISCARD
    OK
    0
    0
    1
    1
    OK
    OK, 2 containers added.
    OK, No containers added.
    OK, 1 container added.
    8
    0