Editorial for COCI '21 Contest 2 #1 Kaučuk


Remember to use this editorial only when stuck, and not to copy-paste code from it. Please be respectful to the problem author and editorialist.
Submitting an official solution before solving the problem yourself is a bannable offence.

For the subtask in which n3, it is sufficient to examine a few possible cases. Because only the sections can stand by themselves, the first command has to be section. The second command can be either section or subsection. Depending on the second command, the third is either section or subsection, or in the other case section, subsection or subsubsection. These five options can be checked using if statements.

In the subtask in which only the section command appears, it is sufficient to number the titles of sections with positive integers from 1 to n.

The subtask in which only the section and subsection commands appear are solved analogously to the entire solution, but without taking into account the subsubsection commands.

For all the points, the task can be solved by keeping track of a counter for each level: S0, S1 and S2. In the beginning, all of the counters are set to zero. We iterate with a for loop over the input:

  • If the current command is section title, we increase S0 by one and print S0 title, and set S1 and S2 to zero because in each section the numeration of the (sub)subsections starts over again.
  • If the current command is subsection title, we increase S1 by one and print S0.S1 title, and set S2 to zero because in each subsection the numeration of the subsubsections starts over again.
  • If the current command is subsubsection title, we increase only S2 and print S0.S1.S2 title.

Comments

There are no comments at the moment.