#C8650. Inventory Transactions Processing
Inventory Transactions Processing
Inventory Transactions Processing
You are given a series of datasets, each representing a group of transactions on product inventories. Each dataset begins with a positive integer n indicating the number of transactions. This is followed by n lines, each containing a transaction in the following format:
op product_id quantity
Here, op is either add
to increase the inventory or remove
to decrease the inventory. The product_id is a string consisting of up to 10 alphanumeric characters, and quantity is a positive integer (not exceeding 1,000).
Your task is to process each dataset independently, computing the final inventory level for every product. The outcome for each dataset should be sorted in lexicographical order according to product_id. If a product has a negative inventory, append the string "(negative)" right after the negative number.
When there are multiple datasets, the first line of the input contains an integer T (the number of datasets), followed by the description of each dataset consecutively.
Note: Read from standard input (stdin
) and write your results to standard output (stdout
).
inputFormat
The input begins with an integer T, representing the number of datasets. For each dataset, the first line is an integer n, the number of transactions in that dataset. The following n lines describe the transactions in the format:
op product_id quantity
Each transaction specifies the operation (add
or remove
), the product ID (a string of up to 10 alphanumeric characters), and the quantity (a positive integer up to 1000).
outputFormat
For each dataset, output the resulting inventory for each product in lexicographical order by product ID. Each line should contain the product ID, a space, and the final inventory level. If the inventory is negative, append " (negative)" after the number. Outputs for different datasets are printed consecutively, each on a new line.## sample
1
4
add A12 100
remove A12 50
add B23 200
remove B23 250
A12 50
B23 -50 (negative)
</p>