C - No Streak 解説 by evima
For ease of explanation, we will restate the problem in terms of strings, as follows:
How many strings \(S\) consisting of characters
A,B, andXsatisfy the following conditions?
- The string \(S\) contains exactly \(a\) occurrences of
A, \(b\) occurrences ofB, and \(x\) occurrences ofX.- Neither
AAnorBBappears as a substring.- For every prefix \(T\) of \(S\), the number of
Ain \(T\) is at least the number ofBin \(T\).
We count this in a manner analogous to Catalan number arguments. Recall that one way to derive the usual Catalan numbers is to count paths that never go below the diagonal, using the idea of flipping A and B after the crossing point to derive that the answer is (the number of ways to reach \((a, b)\)) - (the number of ways to reach \((b-1, a+1)\)) = \(\binom{a+b}{a}\) minus \(\binom{a+b}{b-1}\). We apply this method here.
In this problem, the extra condition is that neither AA nor BB can appear. We again use the approach of counting strings where A and B are flipped after crossing the diagonal and subtracting them from the total.
The condition “neither AA nor BB appears” is mostly unaffected even if A and B are flipping after crossing the diagonal. However, we must care that immediately after the diagonal crossing, BB and BX are allowed but BA is not allowed.
Thus, roughly speaking, the answer can be found as follows:
- From (the number of ways to reach \((a, b, x)\)),
- subtract (the number of ways to reach \((b-1, a+1, x)\) with
BBimmediately after the diagonal crossing) and - (the number of ways to reach \((b-1, a+1, x)\) with
BXimmediately after the diagonal crossing).
Let \(f(a, b, x)\) denote the number of ways to arrange \(a\) As, \(b\) Bs, and \(x\) Xs so that neither AA nor BB appears as a substring. Then, the above three numbers can be represented as follows:
- \(f(a,b,x)\) ways
- We can correspond such sequences with the ones where
BBare replaced withB, so \(f(b-1,a,x)\) ways - We have two cases based on whether the character following the
BXisBor(A|X)(this character always exists):BXB: We can correspond such sequences with the ones whereBXBare replaced withB, so \(f(b-1,a,x-1)\) waysBX(A|X): We can correspond such sequences with the ones whereBXare replaced withB, so \(f(b-1,a+1,x-1)\) ways
Thus, if we can compute \(f(a,b,x)\) in linear time, we can solve the entire problem efficiently.
To compute \(f(a,b,x)\), proceed as follows. Suppose \(a>0\) by handling \(a=0\) as a corner case. Let \(k\) be the number of times two As appear consecutively when only looking at A and X in the string \((0 \leq k \leq a-1)\). Then:
- There are \(\binom{a-1}{k}\) ways to choose the positions for adjacent
As. - There are \((a-1) - k + 2 = a - k + 1\) gaps where
Xs may be inserted, and for \(a-k-1\) of them we must insert at least oneX. Thus, there are \(\binom{x - (a - k - 1) + (a-k)}{(a-k)} = \binom{x + 1}{a - k}\) ways to putXs. - There are \(a + x + 1\) gaps where
Bs may be inserted, and for \(k\) of them we must insertBs. Thus, there are \(\binom{a+x+1-k}{b-k}\).
\(f(a, b, x)\) is the product of these, so we can compute it in linear time.
Therefore, the problem can be solved in \(\mathrm{O}(N)\) time or similar, which is sufficiently fast.
投稿日時:
最終更新: