#P4029. Reconstructing the Blurred Compound
Reconstructing the Blurred Compound
Reconstructing the Blurred Compound
You are given several chemical equations, each containing exactly one blurred (unclear) compound represented by a '?' character. A chemical equation uses a single '=' to separate the left and right sides, and compounds on each side are separated by a '+' character. Each compound is a string created by concatenating tokens. A token consists of a capital letter (from A to Z) representing an element, followed by an optional non‐negative integer (at most 9) as its subscript.
Note that in the minimal representation of a compound:
- A token with a subscript of 1 is written with the digit omitted.
- A token with a subscript of 0 is omitted entirely.
- The tokens are arranged in lexicographical order (i.e. alphabetically by the element letter).
The equation is element-conserved, meaning that the total count of each element is the same on both sides once the blurred compound is correctly restored. Your task is to determine the minimal representation for the blurred compound. Depending on which side the '?' appears, compute the missing counts by subtracting the counts from the known compounds on that side from the other side.
Input Format: The first line contains an integer t, the number of equations. Each of the following t lines contains a chemical equation. Each equation includes exactly one '=' and exactly one compound is replaced by '?'.
Output Format: For each equation, output a single line containing the minimal representation of the blurred compound.
inputFormat
The first line contains an integer t (t ≥ 1), the number of chemical equations.
Each of the next t lines contains one chemical equation. In each equation exactly one of the compounds is replaced by a '?' character. The equation has exactly one '=' sign and compounds on each side are separated by '+'.
Example:
3 JQY2+?=J3QY2 JQY2+J2=? ?+C2H3=C3H5
outputFormat
Output t lines, each containing the minimal representation of the blurred compound for the corresponding equation.
For the sample input above, the output should be:
J2 J3QY2 CH2
sample
3
JQY2+?=J3QY2
JQY2+J2=?
?+C2H3=C3H5
J2
J3QY2
CH2
</p>