Problem A
Alcohol
Languages
da
de
en
An organic chemical compound is a molecule consisting of atoms that are pairwise connected by bonds. Atom are of different types, such as oxygen, carbon, and hydrogen. A hydroxy group is a hydrogen–oxygen bond. A compound is an alcohol if it contains a hydroxy group bound to a carbon atom that is itself bound to three other neighbours. An example is methanol:
.
On the other hand, formic acid
is not an alcohol, because its hydroxy group is bound to a carbon atom with only two other neighbours, one of them using a double bond.
If the carbon atom is bound to $0$ or $1$ other carbon atoms, then the alcohol is primary. For instance, methanol is a primary alcohol. If the carbon atom is bound to $2$ or $3$ other carbon atoms, then the alcohol is secondary or tertiary, respectively. A compound can have many hydroxy groups, and therefore simultaneously be a primary, secondary, and tertiary alcohol.
The task is to classify a given organic chemical compound as an alcohol.
Input
Input is a single line consisting of at most $1000$ symbols from
\[ \verb!()CFNO=12345689! \]specifying the given compound in the Simplified Molecular-Input Line-Entry system (SMILES) used in cheminformatics.1 Here are some example expressions and their corresponding compounds:
Atoms are represented by single capital letters. Their valence is the possible number of bonds they can form with their neighbouring atoms given by the following table:
Symbol |
C |
N |
O |
F, H |
Valence |
4 |
3 |
2 |
1 |
Atoms that form a bond are written next to each other. For instance, a single O with two different F neighbours (forming the compound oxygen difloride OF$_2$) is written as FOF in SMILES. The symbol $\texttt=$ specifies a double bond between its adjacent atoms. For instace, two double-bonded O atoms (forming molecular oxygen O$_2$) are written as O=O in SMILES. Hydrogen atoms are not specified and follow from the valences of the specified atoms. For instance, water H$_2$O is just written as O instead of HOH. This convention helps to de-clutter and linearise notation; for instance methane CH$_4$ is written as a single C.
The occurrence of a pair of digits like $\cdots a6\cdots b6\cdots $, specifies a single bond between $a$ and $b$; more generally between the pair of atoms preceding the digits. For instance, C1CCCCC1 is cyclohexane. Digits can be reused (so the first occurrence of a digit is paired with the second; the third with the fourth, etc.).
An atom can be followed by one or more expressions in parenthesis, like $a\mathtt(S\mathtt) \mathtt(T\mathtt)$. This means that the compounds described by $S$ and $T$ are attached to the atom described by $a$. If the expression continues with an atom, such as $a\mathtt(S\mathtt) \mathtt(T\mathtt)b$ then also $b$ is attached to $a$. For instance, C(=O)(NF)F, C(F)(=O)NF, and even the nested expression O(=C(NF)F) describe the same compound: N-fluorocarbamoyl fluoride. Both $S$ and $T$ can start with the double-bond indicator =, and $a$ can be followed by digits.
Output
If the compound is not an alcohol, print $0$. Otherwise print $i\in \{ 1,2,3\} $, if the compound is a primary, secondary, or tertiary alcohol, respectively. If you print more than one integer, print them in order and separated by whitespace.
Sample Input 1 | Sample Output 1 |
---|---|
O |
0 |
Sample Input 2 | Sample Output 2 |
---|---|
CCCO |
1 |
Sample Input 3 | Sample Output 3 |
---|---|
OCC(O)CO |
1 2 |
Sample Input 4 | Sample Output 4 |
---|---|
C(=O)O |
0 |
Sample Input 5 | Sample Output 5 |
---|---|
C12=CC=CC=C1N=C3C(=C2)CN4C(C5=C(C=C34)C(CC(OC5)=O)(CC)O)=O |
3 |
Footnotes
- In this task, we ignore many features of SMILES, such as the bracket notation, many types of atoms, multiple and higher valences, nonstandard isotopes, charges, double-digit cycle numbers, aromaticity, and chirality.