Editorial
A terminal string has no two adjacent different characters. Therefore it is either empty or consists of only one kind of character.
Lemma 1. A string of length can be completely deleted if and only if is even and every character appears at most times.
Proof
Whenever one character is deleted, a different character must be deleted with it, so necessity is immediate.
For sufficiency, use induction on . If some character appears exactly times, delete a pair at a boundary between that character and another character. Otherwise, delete any adjacent pair of different characters. In both cases the remaining string satisfies the same condition.
Fix a character and consider terminal strings consisting only of . Let the other two characters be .
After choosing the occurrences of that remain, every segment between them and at both ends must disappear.
Lemma 2. It is enough to require that, in each such segment, both and appear at most half of the segment length. This does not change the maximum possible number of surviving 's.
Proof
Every completely deletable segment satisfies this condition by Lemma 1.
Conversely, if a segment satisfying this condition also contains at most half , the whole segment can be deleted by Lemma 1. If appears more than half the time, every can be deleted together with an , leaving only 's in that segment.
Hence, if occurrences of can remain under this condition, actual operations can leave at least copies of . Therefore the maximum is unchanged.
Let this maximum be .
Let be the numbers of in . For a position with , define and .
Two occurrences of can be consecutive survivors exactly when the segment between them satisfies Lemma 2. This is equivalent to , , and .
A first surviving at position must satisfy that is odd, , and . Let and . A last surviving at position must satisfy , , and .
Let be the maximum number of surviving 's when position is the last survivor. A predecessor must satisfy , , , and have the opposite parity. This is a three-dimensional dominance DP on the index, , and , so CDQ divide and conquer finds in .
Lemma 3. Among positions with , if and , then always holds.
Proof
Let be the number of in . Then .
Among positions containing , is strictly increasing in their original order. Thus and imply , so and therefore .
By Lemma 3, the index condition is unnecessary. Process the points by and maintain the dominance maximum by with a Fenwick Tree. Keep the two parities separately. This finds in .
Repeat this for .
Suppose appears times in the whole string. If the terminal string is , then because every operation removes two characters. Also, deleting one requires one non-, so .
Thus the smallest positive possible length is when is odd, and when is even.
Lemma 4. If , the possible lengths are exactly .
Proof
Suppose is reachable and . Let be the completely deleted segments around the surviving 's.
Let be the length of and the number of in it. Then is a nonnegative integer. Also, , so some is positive.
Delete two consecutive surviving 's so that the merged segment contains such a segment. In the merged segment, all three characters appear at most half of its length. By Lemma 1, the whole merged segment can be deleted, so is also reachable.
Repeating this gives every length .
Therefore, if , the number of terminal strings consisting only of is .
By Lemma 1, the empty string is reachable exactly when is even and . Add the counts for the three characters, and add one more if the empty string is reachable.
Therefore, the overall time complexity is .