Skip to content

Commit a4207b9

Browse files
authored
Merge pull request cp-algorithms#1304 from alaahmet/patch-1
fixing typos in bracket_sequences.md
2 parents fd41529 + 6d75626 commit a4207b9

File tree

1 file changed

+10
-11
lines changed

1 file changed

+10
-11
lines changed

src/combinatorics/bracket_sequences.md

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ We iterate over all character of the string, if the current bracket character is
3232
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.
3333
Otherwise it is.
3434

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.
3636
Instead of a counter $\text{depth}$ we create a stack, in which we will store all opening brackets that we meet.
3737
If the current bracket character is an opening one, we put it onto the stack.
3838
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)
4949

5050
$$\frac{1}{n+1} \binom{2n}{n}$$
5151

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:
5353

5454
$$\frac{1}{n+1} \binom{2n}{n} k^n$$
5555

@@ -82,7 +82,7 @@ When we meet an opening brackets, we will decrement $\text{depth}$, and when we
8282
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.
8383
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.
8484

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.
8686

8787
```{.cpp file=next_balanced_brackets_sequence}
8888
bool next_balanced_sequence(string & s) {
@@ -117,7 +117,7 @@ To generate then, we can start with the lexicographically smallest sequence $((\
117117
118118
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.
119119
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.
121121
We will discuss the ideas in the next two sections.
122122
123123
## Sequence index
@@ -142,19 +142,19 @@ Thus we can compute this array in $O(n^2)$.
142142
Now let us generate the index for a given sequence.
143143
144144
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.
146146
If the current character $s[i]$ is equal to $($, then we increment $\text{depth}$.
147147
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}$.
148148
149149
New let there be $k$ different bracket types.
150150
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:
152152
153153
$$d[2n - i - 1][\text{ndepth}] \cdot k^{\frac{2n - i - 1 - ndepth}{2}}$$
154154
155155
This formula can be derived as follows:
156156
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.
158158
We have $2n - i - 1$ undefined positions, of which $\text{ndepth}$ are already predetermined because of the opening brackets.
159159
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$.
160160
@@ -169,10 +169,9 @@ First, we start with only one bracket type.
169169
170170
We will iterate over the characters in the string we want to generate.
171171
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.
176175
177176
```{.cpp file=kth_balances_bracket}
178177
string kth_balanced(int n, int k) {

0 commit comments

Comments
 (0)