#K15776. Aggregate Ingredient Quantities
Aggregate Ingredient Quantities
Aggregate Ingredient Quantities
You are given T test cases. In each test case, you first receive an integer N representing the number of dishes. This is followed by N lines, each containing a string that describes a dish. A dish is represented as a list of ingredients in the format ingredient:quantity
separated by commas. For example, a dish could be tomato:2,cheese:3,lettuce:1
.
Your task is to calculate the total quantity required for each unique ingredient for each test case. Mathematically, for each test case, if we denote by \( a_{ij} \) the quantity of the \( i^{th} \) ingredient in the \( j^{th} \) dish, you need to output the aggregated sum for each ingredient:
[ S_i = \sum_{j=1}^{N} a_{ij} ]
The results for each test case should be output on a separate line, with the ingredients sorted in alphabetical order, and each pair formatted as ingredient:quantity
separated by commas. If a test case contains no dishes, output an empty dictionary {}
.
inputFormat
The input begins with an integer T indicating the number of test cases. For each test case:
- The first line contains an integer N, the number of dishes.
- The next N lines each contain a string representing a dish. Each dish is described as a list of comma-separated items, where each item is in the format
ingredient:quantity
. The quantity is a positive integer.
outputFormat
For each test case, output a single line. The line should list the aggregated ingredients and their total quantities in the format:
key1:total1,key2:total2,...,keyK:totalK
The keys must be sorted in alphabetical order. If no ingredients are present (i.e. when N is 0), output {}
.
1
3
tomato:2,cheese:3,lettuce:1
tomato:1,lettuce:2
cheese:2,tomato:3
cheese:5,lettuce:3,tomato:6
</p>