#P8324. [COCI 2021/2022 #5] Kemija

[COCI 2021/2022 #5] Kemija

Problem Description

You are given nn chemical equations.

Each molecule participating in the reaction consists of several atoms. Each atom is represented by a letter and a number, which stand for the element name and the number of that atom in the molecule, respectively. If multiple molecules participate in the reaction, a number is added in front of the molecule to represent the stoichiometric coefficient. In this problem, you only need to consider the number of each atom, and you do not need to consider whether the equation is chemically valid. For example, 3AC4B\texttt{3AC4B} can be directly regarded as 33 A\texttt A, 33 B\texttt B, and 1212 C\texttt C participating in the reaction. When different molecules are on the same side of the equation, they are connected with +\texttt +.

Your task is to check for each equation whether it is already balanced (you only need to check whether the numbers of atoms on the left and right sides match exactly).

Input Format

The first line contains a positive integer nn, representing the number of equations.

The next nn lines each contain one equation. The testdata guarantees that the length of each equation string does not exceed 10001000, and each element consists of only one uppercase letter.

Output Format

For each equation, output DA\texttt{DA} if it is already balanced; otherwise output NE\texttt{NE}.

3
A+B->AB
AB+CD->AC+DB
AB+B->AB
DA
DA
NE
2
2AB+A->3AB
2AB+2AC+2BC->4ABC
NE
DA
4
2H2O+2CO2->2H2CO3
H2SO4->H2O4
NH3+H2SO4->NH4SO4
CH4+2O2->CO2+2H2O
DA
NE
NE
DA

Hint

[Sample 3 Explanation]

No. Atoms on the left side Atoms on the right side Result
11 $4 \times \texttt H, 6 \times \texttt O, 2 \times \texttt C$ DA\green{\texttt{DA}}
22 $2 \times \texttt H, \red{1 \times \texttt S}, 4 \times \texttt O$ 2×H,4×O2 \times \texttt H, 4 \times \texttt O NE\red{\texttt{NE}}
33 $1 \times \texttt N, \red{5 \times \texttt H}, 1 \times \texttt S, 4 \times \texttt O$ $1 \times \texttt N, \red{4 \times \texttt H}, 1 \times \texttt S, 4 \times \texttt O$
44 $1 \times \texttt C, 4 \times \texttt H, 4 \times \texttt O$ DA\green{\texttt{DA}}

[Constraints and Notes]

This problem uses bundled tests.

  • Subtask 1 (10 pts): There are no digits in the equation.
  • Subtask 2 (10 pts): There are no digits in the molecular formulas.
  • Subtask 3 (30 pts): No special restrictions.

For 100%100\% of the data, 1n10001 \le n \le 1000.

[Source] COCI 2021-2022#5 Task 1 Kemija。

Translated by ChatGPT 5