#C9951. Chemical Formula Composition Checker
Chemical Formula Composition Checker
Chemical Formula Composition Checker
This problem requires you to determine whether pairs of chemical formulas represent the same composition of elements. Each chemical formula is a string that includes element symbols (an uppercase letter followed by zero or more lowercase letters) and an optional positive integer indicating the number of atoms (if missing, assume 1). You will be given several datasets. Each dataset begins with an integer N which denotes the number of formula pairs, followed by 2×N strings representing the two formulas in each pair. A dataset starting with 0 marks the end of input.
Your task is to check each pair and print yes
if the compositions are identical, and no
otherwise. Formally, for two formulas F1 and F2, if the multi‐set of elements (with the respective counts) in both formulas are equal, the answer is yes
; otherwise, it is no
.
The formulas may include elements such as H, He, C, O etc., and the counts are given as numbers attached directly to the element symbol. All formulas are valid and do not contain parentheses or nested groups.
inputFormat
The input is read from standard input and consists of multiple datasets. Each dataset starts with an integer N, the number of formula pairs to check. This is followed by 2 * N lines, where each adjacent pair of lines provides two chemical formulas to compare. The input terminates with a line containing a single 0, which should not be processed.
N formula1 formula2 ... (repeated N times) 0
outputFormat
For each pair in each dataset, output a single line containing either yes
if the two formulas represent the same composition, or no
otherwise.
3
H2O
O1H2
CH4
C2H2
H
HH
2
O2
O2
N2
N2O
0
yes
no
no
yes
no
</p>