Editorial for COCI '07 Contest 1 #4 Zapis


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.

We start by observing the first character in the sequence. That character must be an opening bracket. Its closing bracket can only be located at an even position in the sequence (because a regular bracket sequence has even length).

For each even position x, and for each type of bracket, we now check if it is possible to place an opening bracket as the first character and a corresponding closing bracket as x^\text{th} character. If it is possible then we add to the total number of solutions the number of regular bracket-sequences from the second character to (x-1)^\text{st} character multiplied by the number of regular bracket-sequences from (x+1)^\text{st} character to the last character.

By doing this we have reduced the large problem to two smaller subproblems of the same type, so it can be solved using dynamic programming or memoization.


Comments

There are no comments at the moment.