Editorial for Baltic OI '12 P1 - Brackets
Submitting an official solution before solving the problem yourself is a bannable offence.
Let's define a correct string of parentheses (round brackets):
()
is a correct string of parentheses;- If
A
is a correct string of parentheses, then(A)
also is a correct string of parentheses; - If both,
A
andB
, are correct parentheses strings, then the concatenationAB
is also a correct string of brackets.
In other words, a correct parentheses string is a correct string of brackets, but containing no square brackets.
Let us take an arbitrary correct string of parentheses and replace some (perhaps all, but at least one) of the closing (right) parentheses with the opening (left) parentheses. We shall name the resulting string a broken string of parentheses.
It is easy to see that every broken string of brackets is also a broken string of parentheses, and vice versa. In fact, both of them fulfil the following two conditions:
- the number of opening brackets is greater than the number of closing brackets;
- the number of opening brackets is not less than the number of closing brackets for every prefix of the string.
Accordingly, we will name both, the broken string of brackets and the broken strings of parentheses, broken strings.
For further explanation, let be a broken string. Let denote the set of correct strings of brackets that match the broken string of brackets . Let denote the set of correct strings of parentheses that match the broken string of brackets .
Lemma
For any broken string the sets and contain the same number of elements.
Proof
Let be a broken string. We set a one-to-one correspondence between the elements of sets and .
- Let be an arbitrary correct string of brackets from . Substitute its every square bracket with a corresponding round bracket (opening with opening, closing with closing). As a result we obtain some correct string of parentheses that matches .
- Let be an arbitrary correct string of parentheses from . We mark all closing round brackets of that correspond to closing round brackets in (that appear at the same position in and ). Then we mark all opening round brackets in that correspond to the already marked closing round brackets. Finally, we substitute all unmarked round brackets in with square brackets (opening with opening, closing with closing). As a result we obtain some correct string of brackets that matches .
It follows from (1) and (2) that and contain the same number of elements. QED.
Thus, we have reduced the task to the following: calculate the number of distinct correct strings of parentheses that correspond to the given broken string. This task is solved using standard dynamic programming.
Let denote the number of distinct prefixes with length of correct strings of brackets that match the given broken string such that the difference between opening and closing brackets is .
- , for all
- If
)
then for all , . - If
(
then , for all ,
Note: denotes the symbol of the given broken string . contains the answer, where is the length of the given broken string .
This directly leads to a solution with time and memory complexity. However, this is insufficient to score maximum points.
Firstly, we have to avoid using a two-dimensional array; we have to somehow obtain the same result by a manipulation of one-dimensional arrays. Note that to calculate for all , we need to remember only for all (we don't need any where ). Thus, it is enough to maintain only a one-dimensional array throughout the calculations.
Secondly, straight forward implementation of the algorithm is a bit too slow. The correct solution is still , but we should speed it up by some constant factor.
Note that if and have different parity then . Note also that we are not interested in the values of when or . The first case does not matter because it essentially means that there have been more opening brackets than brackets in total in the prefix, which is impossible. The second case does not matter because it essentially means that there are currently more unclosed opening brackets in the prefix than the number of brackets left until reaching the length ; this is also an impossible case.
The overall time complexity is and memory complexity . Note that the observations mentioned in the previous two paragraphs greatly decrease the number of interesting states and speeds up the execution approximately times.
Side note: This task was proposed for IOI 2011.
Comments