B - Maximum Bracket Subsequence Editorial by evima
A bracket sequence is called wonderful if it is a correct bracket sequence but cannot be written in the form \(A + B\) using correct bracket sequences \(A\) and \(B\) (for example, () and (()())).
Among correct bracket sequences of the same length, the lexicographically largest one is the repetition of ().
Hence, if \(S\) is a repetition of (), the condition is that \(T\) contains \(S\) as a subsequence, so the answer can be computed easily.
From here on, assume that \(S\) is not a repetition of ().
[1] Algorithm for extracting the lexicographically largest correct bracket subsequence \(T'\) of length \(K\) from \(T\)
Consider a string \(T\) made up of parentheses. Among all its subsequences that are correct bracket sequences of length \(K\), let \(f(T)\) be the lexicographically largest one. The following procedure obtains \(f(T)\):
The string \(T\) can be uniquely written in the form \(t_1 + \)
)\(+t_2 + \))\(+ \dots +\))\( + t_k + \)(\(+t_{k+1}+\)(\(+\dots +t_n\) using some correct bracket sequences \(t_1, t_2, \dots, t_n\). Remove those))...)(...((from \(T\), and replace it with \(t_1 + t_2 + \dots + t_n\). (If, after this replacement, \(|T|\) becomes less than \(K\), there is no correct bracket subsequence of length \(K\).)Next, let \(k_{\mathrm{max}}\) be the maximum value of \(k\) for which the length-\(2k\) correct bracket sequence obtained by repeating
()satisfies all of the following conditions:- It is a subsequence of \(T\).
- Let \(T_{\mathrm{suf}}\) be the suffix of \(T\) that remains when we greedily take the subsequence from the front. \(T_{\mathrm{suf}}\) has a subsequence that is a correct bracket sequence of length \(K - 2k\).
Then, the first \(2k_{\mathrm{max}}\) characters of \(f(T)\) will be a repetition of ().
- Let \(T'\) be the suffix of \(T\) that remains when we greedily take a subsequence that is
()repeated \(k_{\mathrm{max}}\) times from the front. After applying the replacement in step 1 to \(T'\), repeat the following until the length of \(T'\) becomes \(K - 2k_{\mathrm{max}}\):- Write \(T'\) in the form
(+ \(A\) +)+ \(B\) for correct bracket sequences \(A,B\). Then, replace \(T'\) with \(A + B\).
- Write \(T'\) in the form
Finally, \(f(T)\) is obtained by concatenating the () repeated \(k_{\mathrm{max}}\) times and the resulting \(T'\), in this order.
Regarding step 1, if one tries to use the first ) in those ))...)(...(( as a part of the subsequence, then there is some earlier ) not used before it, and swapping them yields a lexicographically larger subsequence. Repeating this reasoning shows that those ) and ( in ))...)(...(( will not be used.
Step 2 is clearly valid.
Lastly, in step 3, the correctness follows from the fact that we are minimizing the number of leading ( in the process. If at some point there were exactly one leading (, it would contradict what happens in step 2.
[2] Counting the ways to reconstruct \(T\) from \(S\)
Suppose \(S\) is constructed by first repeating () \(a\) times, then concatenating \(b\) wonderful bracket sequences (the first of which is not ()). We can find \(a\) and \(b\) in \(O(K)\) time, and we have \(b \neq 0\).
Based on [1], we can reconstruct \(T\) roughly as follows:
- Into the string that is the concatenation of the \(b\) wonderful bracket sequences, repeatedly insert
(and then)in that order, without changing how brackets match each other. Here,(must always be inserted at the beginning. - For the resulting correct bracket sequence from step 1, insert
),),), …, then(,(,(in that order, again without changing how brackets match, to obtain a string \(X\). - Pick some string \(Y\) that contains
()repeated \(a\) times as a subsequence, such that if we take that subsequence greedily from the front, the last character of \(Y\) is chosen (i.e., if the last character were removed from \(Y\), it would no longer contain that()-repeated subsequence). - Let \(T = Y + X\).
We will use formal power series (FPS) to count the number of ways to follow these steps to reach a string of length \(N\).
First, regarding step 3, the generating function in terms of the number of inserted characters for \(Y\) is \(\frac{1}{(1-x)^{2a}}\).
Next, consider step 1. We have \(b\) possible positions at which to insert ), and let us focus on the position of the rightmost insertion among these \(b\). Suppose it is the \(i\)-th from the left; the generating function for the number of inserted characters in step 1 is then
\(\frac{x^2}{(1-x^2)^i}\). After step 1, the string decomposes into \(b-i+1\) wonderful bracket sequences, so the generating function for the number of inserted characters in step 2 is \(\sum_{n=0}^{\infty} (n+1)[x^n]\frac{1}{(1-x)^{b-i+2}} = \frac{((b-i+1)x+1)}{(1-x)^{b-i+3}}\).
Therefore, if there is at least one insertion in step 1, the overall generating function with respect to the total number of inserted characters is
\[\displaystyle \frac{1}{(1-x)^{2a}}\sum_{i=1}^{b} \frac{x^2((b-i+1)x+1)}{(1-x^2)^i (1-x)^{b-i+3}} = \frac{bx^2}{(1-x)^{2a+b+3}}.\]
The coefficient of \(x^{N-K}\) in the above series can be expressed using binomial coefficients and computed in \(O(N)\) time.
If step 1 involves no insertions, we can find the count easily.
posted:
last update: