You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/combinatorics/bracket_sequences.md
+10-11Lines changed: 10 additions & 11 deletions
Original file line number
Diff line number
Diff line change
@@ -32,7 +32,7 @@ We iterate over all character of the string, if the current bracket character is
32
32
If at any time the variable $\text{depth}$ gets negative, or at the end it is different from $0$, then the string is not a balanced sequence.
33
33
Otherwise it is.
34
34
35
-
If there are several bracket types involved, then the algorithm needs to be changes.
35
+
If there are several bracket types involved, then the algorithm needs to be changed.
36
36
Instead of a counter $\text{depth}$ we create a stack, in which we will store all opening brackets that we meet.
37
37
If the current bracket character is an opening one, we put it onto the stack.
38
38
If it is a closing one, then we check if the stack is non-empty, and if the top element of the stack is of the same type as the current closing bracket.
@@ -49,7 +49,7 @@ The number of balanced bracket sequences of length $2n$ ($n$ pairs of brackets)
49
49
50
50
$$\frac{1}{n+1} \binom{2n}{n}$$
51
51
52
-
If we allow $k$ types of brackets, then each pair be of any of the $k$ types (independently of the others), thus the number of balanced bracket sequences is:
52
+
If we allow $k$ types of brackets, then each pair can be of any of the $k$ types (independently of the others), thus the number of balanced bracket sequences is:
53
53
54
54
$$\frac{1}{n+1} \binom{2n}{n} k^n$$
55
55
@@ -82,7 +82,7 @@ When we meet an opening brackets, we will decrement $\text{depth}$, and when we
82
82
If we are at some point meet an opening bracket, and the balance after processing this symbol is positive, then we have found the rightmost position that we can change.
83
83
We change the symbol, compute the number of opening and closing brackets that we have to add to the right side, and arrange them in the lexicographically minimal way.
84
84
85
-
If we find do suitable position, then this sequence is already the maximal possible one, and there is no answer.
85
+
If we don't find a suitable position, then this sequence is already the maximal possible one, and there is no answer.
86
86
87
87
```{.cpp file=next_balanced_brackets_sequence}
88
88
boolnext_balanced_sequence(string & s) {
@@ -117,7 +117,7 @@ To generate then, we can start with the lexicographically smallest sequence $((\
117
117
118
118
However, if the length of the sequence is not very long (e.g. $n$ smaller than $12$), then we can also generate all permutations conveniently with the C++ STL function `next_permutation`, and check each one for balanceness.
119
119
120
-
Also they can be generate using the ideas we used for counting all sequences with dynamic programming.
120
+
Also they can be generated using the ideas we used for counting all sequences with dynamic programming.
121
121
We will discuss the ideas in the next two sections.
122
122
123
123
## Sequence index
@@ -142,19 +142,19 @@ Thus we can compute this array in $O(n^2)$.
142
142
Now let us generate the index for a given sequence.
143
143
144
144
First let there be only one type of brackets.
145
-
We will us the counter $\text{depth}$ which tells us how nested we currently are, and iterate over the characters of the sequence.
145
+
We will use the counter $\text{depth}$ which tells us how nested we currently are, and iterate over the characters of the sequence.
146
146
If the current character $s[i]$ is equal to $($, then we increment $\text{depth}$.
147
147
If the current character $s[i]$ is equal to $)$, then we must add $d[2n-i-1][\text{depth}+1]$ to the answer, taking all possible endings starting with a $($ into account (which are lexicographically smaller sequences), and then decrement $\text{depth}$.
148
148
149
149
New let there be $k$ different bracket types.
150
150
151
-
Thus, when we look at the current character $s[i]$ before recomputing $\text{depth}$, we have to go through all bracket types that are smaller than the current character, and try to put this bracket into the current position (obtaining a new balance $\text{ndepth} = \text{depth} \pm 1$), and add the number of ways to finish the sequence (length $2n-i-1$, balance $ndepth$) to the answer:
151
+
Thus, when we look at the current character $s[i]$ before recomputing $\text{depth}$, we have to go through all bracket types that are smaller than the current character, and try to place this bracket into the current position (obtaining a new balance $\text{ndepth} = \text{depth} \pm 1$), and add the number of ways to finish the sequence (length $2n-i-1$, balance $ndepth$) to the answer:
152
152
153
153
$$d[2n - i - 1][\text{ndepth}] \cdot k^{\frac{2n - i - 1 - ndepth}{2}}$$
154
154
155
155
This formula can be derived as follows:
156
156
First we "forget" that there are multiple bracket types, and just take the answer $d[2n - i - 1][\text{ndepth}]$.
157
-
Now we consider how the answer will change is we have $k$ types of brackets.
157
+
Now we consider how the answer will change if we have $k$ types of brackets.
158
158
We have $2n - i - 1$ undefined positions, of which $\text{ndepth}$ are already predetermined because of the opening brackets.
159
159
But all the other brackets ($(2n - i - 1 - \text{ndepth})/2$ pairs) can be of any type, therefore we multiply the number by such a power of $k$.
160
160
@@ -169,10 +169,9 @@ First, we start with only one bracket type.
169
169
170
170
We will iterate over the characters in the string we want to generate.
171
171
As in the previous problem we store a counter $\text{depth}$, the current nesting depth.
172
-
In each position we have to decide if we use an opening of a closing bracket.
173
-
To have to put an opening bracket character, it $d[2n - i - 1][\text{depth}+1] \ge k$.
174
-
We increment the counter $\text{depth}$, and move on to the next character.
175
-
Otherwise we decrement $k$ by $d[2n - i - 1][\text{depth}+1]$, put a closing bracket and move on.
172
+
At each position, we have to decide whether to place an opening or a closing bracket. To place an opening bracket, $d[2n - i - 1][\text{depth}+1] \ge k$ must be true.
173
+
If so, we increment the counter $\text{depth}$, and move on to the next character.
174
+
Otherwise, we decrement $k$ by $d[2n - i - 1][\text{depth}+1]$, place a closing bracket, and move on.
0 commit comments