Time limit
2s
Memory limit
128 MB
A carbon compound is a compound made only of the three atom types C, H, and O. We want to balance a simple chemical equation of the form
M1 + M2 = M3
Each M is a molecule. A molecule is written as a sequence of atoms, and each atom may be followed by one digit:
atom[digit]atom[digit]...atom[digit]
Each molecule contains at least one atom. If a digit follows an atom, it tells how many times that atom appears; the digit is between 2 and 9. If no digit is written, that atom appears once.
For example, HC3OH2 represents a molecule with 3 H atoms, 3 C atoms, and 1 O atom. The equation C2HOH + CH = C5O2H4 is one possible input equation.
An equation is balanced when, for each of C, H, and O, the total number of atoms on the left side of = is equal to the total number on the right side. For instance, C + CH = C2H is already balanced.
If an equation is not balanced, you may choose coefficients for the three molecules and treat the equation as containing that many copies of each molecule. For example, C + CC = CCCCC can be balanced by coefficients 1, 2, 1, because C + 2(CC) = CCCCC.
Given an equation of the form M1 + M2 = M3 made only of carbon-compound molecules, write a program that chooses coefficients from 1 through 10 and balances it.
The first line contains a chemical equation in the form M1+M2=M3. Its length is at most 50, and it consists only of C, H, O, +, =, and digits from 2 through 9.
Print three integers X1 X2 X3 in this order, separated by spaces. Each integer must be between 1 and 10, inclusive, and they are the coefficients of M1, M2, and M3, respectively.
If there is more than one solution, regard each answer as the sequence (X1, X2, X3) and print the lexicographically smallest one. In other words, minimize X1 first; if there is still more than one choice, minimize X2; then minimize X3.