Editorial for LCC '24 Contest 1 S2 - Graham's Magic Spells


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.

Author: nelimalu

This problem is intended to be solved using a stack.

An easier way to think about it is to replace a spell being cast with an open bracket and an undoing spell with a closing bracket This means a spellset like CCJRSJ -> (()[]) or CRJS -> ([)] The goal is to make the brackets open an close properly.

To do so, you could loop through each character (or spell cast). If the character is an opening spell, like C or R, it is added to the stack. If a closing spell is encountered, peek the top of the stack. If the value returned is the opposite of the current spell in the loop, all is well, and you can pop from the top of the stack. If it is not, then something went wrong.


Comments

There are no comments at the moment.