P8324 [COCI 2021/2022 #5] Kemija
Description
You are given $n$ 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, $\texttt{3AC4B}$ can be directly regarded as $3$ $\texttt A$, $3$ $\texttt B$, and $12$ $\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 $n$, representing the number of equations.
The next $n$ lines each contain one equation. The testdata guarantees that the length of each equation string does not exceed $1000$, and each element consists of only one uppercase letter.
Output Format
For each equation, output $\texttt{DA}$ if it is already balanced; otherwise output $\texttt{NE}$.
Explanation/Hint
**[Sample 3 Explanation]**
|No.|Atoms on the left side|Atoms on the right side|Result|
| :----------: | :----------: | :----------: | :----------: |
|$1$|$4 \times \texttt H, 6 \times \texttt O, 2 \times \texttt C$|$4 \times \texttt H, 6 \times \texttt O, 2 \times \texttt C$|$\green{\texttt{DA}}$|
|$2$|$2 \times \texttt H, \red{1 \times \texttt S}, 4 \times \texttt O$|$2 \times \texttt H, 4 \times \texttt O$|$\red{\texttt{NE}}$|
|$3$|$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$|$\red{\texttt{NE}}$|
|$4$|$1 \times \texttt C, 4 \times \texttt H, 4 \times \texttt O$|$1 \times \texttt C, 4 \times \texttt H, 4 \times \texttt O$|$\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\%$ of the data, $1 \le n \le 1000$.
**[Source]** [COCI 2021-2022#5](https://hsin.hr/coci/contest5_tasks.pdf) Task 1 Kemija。
Translated by ChatGPT 5