C - No Streak Editorial 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
, andX
satisfy the following conditions?
- The string \(S\) contains exactly \(a\) occurrences of
A
, \(b\) occurrences ofB
, and \(x\) occurrences ofX
.- Neither
AA
norBB
appears as a substring.- For every prefix \(T\) of \(S\), the number of
A
in \(T\) is at least the number ofB
in \(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
BB
immediately after the diagonal crossing) and - (the number of ways to reach \((b-1, a+1, x)\) with
BX
immediately after the diagonal crossing).
Let \(f(a, b, x)\) denote the number of ways to arrange \(a\) A
s, \(b\) B
s, and \(x\) X
s 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
BB
are replaced withB
, so \(f(b-1,a,x)\) ways - We have two cases based on whether the character following the
BX
isB
or(A|X)
(this character always exists):BXB
: We can correspond such sequences with the ones whereBXB
are replaced withB
, so \(f(b-1,a,x-1)\) waysBX(A|X)
: We can correspond such sequences with the ones whereBX
are 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 A
s 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
A
s. - There are \((a-1) - k + 2 = a - k + 1\) gaps where
X
s 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 putX
s. - There are \(a + x + 1\) gaps where
B
s may be inserted, and for \(k\) of them we must insertB
s. 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.
posted:
last update: