A - ABA and BAB Editorial by evima
In competitive programming, a well-known technique is using parity to perform inversions. Let’s apply it to this problem.
Specifically, let’s invert the characters A and B only at even positions in the input string.
Then, the two available operations become the following:
AAA\(\to\)ABBB\(\to\)B
With this transformation, the solution becomes almost obvious.
For a contiguous segment of A or B, as long as its length does not become zero, we can reduce the length by \(2\) each time.
For example, if there is a segment AAAAA, the possible final states are AAAAA, AAA, and A: there are three.
Therefore, if the length of a segment of consecutive identical characters is \(L\), the number of possible final states for that segment is \(\operatorname{floor}((L+1)/2)\).
For all segments of consecutive identical characters, the final states can be chosen independently. Thus, the product of the above values for all such segments is the answer to this problem.
posted:
last update: