One cumbersome problem in chemistry is the task of making the number of atoms balanced in a chemical equation. Our problem is concerned with this.
Chemists obey these rules when they present chemical equations:
- Each element name is abbreviated by at most two letters. The first letter is always in upper-case and the second letter if exists, is a lower-case letter (e.g. Calcium is represented by Ca, Oxygen by O, and Chlorine by Cl).
- Each molecule is composed of a number of atoms. To represent a molecule, we concatenate the abbreviated names of its composite atoms. For example, NaCl represents Sodium Chloride. Each atom name may be followed by a frequency number. For example, Calcium Chloride CaCl2 consists of one atom of Calcium and two atoms of Chlorine. If the frequency is not given, it is assumed to be 1 (so HCl is equivalent to H1Cl1). For the sake of simplicity, you may assume that the frequency of an occurrence of an atom is at most 9 (so we do not have C11H22O11 in the problem input). Note that there may be several occurrences of the same atom in the molecule formula, like H atom in CH3COOH.
- In ordinary chemical reactions, a number of molecules combine and result in a number of other molecules. For example a well known sample of neutralization is:
2HCl + CaO2H2 → CaCl2 + 2H2O
This means two molecules of chlorohydric acid (HCl) with one molecule of Calcium Hydroxide results in one molecule of Calcium Chloride (CaCl2) and two molecules of water.
- In every chemical reaction, the total number of each atom in the right side of the equation equals the total number of that atom in the left side (that is why it is called an equation!).
Your task is to write a program to find appropriate coefficients x1 , x2 , …, xM and y1 , y2 , …, yN to balance an (imbalanced) equation like
A1+A2+A3+ ... +AM → B1+B2+B3+ ... +BN
in the following way:
x1 A1+ x2 A2+ x3 A3+ ... + xM AM → y1 B1+ y2 B2+ y3 B3+ ... + yN BN