Here's the statements of problem A, "ABC String" in Educational Codeforces Round 105 (Rated for Div. 2)
You are given a string a, consisting of n characters, n is even. For each i from 1 to n $a_i$ is one of 'A', 'B' or 'C'.
A bracket sequence is a string containing only characters "(" and ")". A regular bracket sequence is a bracket sequence that can be transformed into a correct arithmetic expression by inserting characters "1" and "+" between the original characters of the sequence. For example, bracket sequences "()()" and "(())" are regular (the resulting expressions are: "(1)+(1)" and "((1+1)+1)"), and ")(", "(" and ")" are not.
You want to find a string b that consists of n characters such that:
b is a regular bracket sequence; if for some i and j $(1≤i,j≤n1≤i,j≤n) a_i=a_j$, then $b_i=b_j$. In other words, you want to replace all occurrences of 'A' with the same type of bracket, then all occurrences of 'B' with the same type of bracket and all occurrences of 'C' with the same type of bracket.
Your task is to determine if such a string b exists.
I submitted my code and it's accepted.
The idea is brute-force. There're only $2^3 - 2$ cases for $A, B, C$ to consider, since $A, B, C$ cannot be all "(" nor all ")", because a RBS(regular bracket sequence) must contain at least one "(" and one ")".
So the problem is to check for validity of each case. What's the algorithm for that?
The idea is to keep track of the number of close brackets and open brackets so that these two conditions hold throughout the search from left to right:
- $cnt_{close} \leq cnt_{open}$
- At the end of the search, $cnt_{close} = cnt_{open}$
cnt stands for counter.
But does this algorithm actually work? We have to prove that a BS is considered RBS if and only if the two above conditions hold throughout the search from left to right.
The first side is easy to prove. If a BS is a RBS, we clearly must have $cnt_{close} = cnt_{open}$ at the end of the loop.
And we can't have $cnt_{close} > cnt_{open}$ at any point of the search since if we've found a redundant close bracket, it cannot match with any open bracket before it. However, for every close and open brackets in a RBS, it always has a partner. So the two theories are contrary.
Therefore, the two conditions must hold when a BS is a RBS.
The reverse side is more tricky. I'm wrapping my head around it. I tried induction on the second condition and the reductio method but nothing works out.
My reductio approach: So let's assume that when the two conditions hold, we do not have a RBS which means only two cases happen: Either we have $cnt_{close} \neq cnt_{open}$ at the end of the loop or a pair of "(" and ")" follow the wrong order, namely "...(1)... ) ...(2)...( ...(3)...".
The former can't happen due to the second condition.
While searching throughout "area (1)", we cannot have $cnt_{close} \geq cnt_{open}$ since that would fail the first condition.
That's how far I've gone. Nothing more. I'd like to hear from you your approach to this problem. Thank you in advance!
25/11/2021: What if we define a "RBS" to be a BS such that:
- For every close bracket, it has a unique partner from the left.
- The number of open brackets equal to the number of close brackets.