15085 - Cabi lab   

Description

Capybara's friend CABI-bara is a chemist who is currently learning about organic chemistry. His work is to analyze various elements. However, the machine in his lab is malfunctioning and can only output complex chemical formulas. He needs the students' help to analyze the elements!

Given a chemical formula (a string), please parse all the chemical elements inside and calculate the total amount of each element.

Finally, you MUST output the results sorted by their Atomic Number in ascending order.

The chemical formula strictly follows these rules:

  • Element Symbols: An element symbol always starts with exactly one uppercase English letter, followed by zero or more lowercase English letters (e.g., C, H, O, Na, Cl).
  • Quantity Indicators: If a number directly follows an element symbol, it represents the quantity of that element (e.g., in H2O, the quantity of H is 2). If there is no number, the quantity is implicitly 1 (e.g., in H2O, the quantity of O is 1).
  • The number can be a multi-digit integer.
  • Parentheses Multiplication: The formula may contain parentheses (). A closing parenthesis ) will always be immediately followed by a number , meaning the quantities of all elements inside the parentheses are multiplied by . For example, Ca(OH)2 contains 1 Ca, 2 O, and 2 H. Parentheses are guaranteed to be valid, balanced, and can be nested.

To help you sort the elements, CABI-bara has provided the periodic table used in this homework.

(Note: Although the chart shows the entire periodic table, CABI-bara guarantees that you only need to handle the first 36 elements, from Hydrogen (1) to Krypton (36).)

Input

The input contains only one line, which is a valid chemical formula string .

  • All quantity numbers and the final count of each element will fit in a standard 32-bit signed integer.
  • The string only contains uppercase letters, lowercase letters, digits, (, and ).
  • It is guaranteed that all elements appearing in the test cases have an atomic number .

Output

Output multiple lines. For each element present in the formula, output in the format:
Element: Quantity.
The output lines MUST be sorted in ascending order of their Atomic Numbers.

Sample Input  Download

Sample Output  Download




Discuss