To solve this problem, we need to maximize the number of operations performed on the string S by applying the following rules:
- A-B Operation: Delete any character
A and a B that appears after it.
- B-C Operation: Delete any character
B and a C that appears after it.
Each character in the string can be part of at most one operation. To ensure we maximize the number of operations, a greedy approach can be employed. Here's the step-by-step strategy:
-
First Pass (B-C Operations):
- Iterate through the string from left to right.
- Keep track of the number of available
B characters (available_B).
- When a
C is encountered, if there's an available B, perform a B-C operation and decrement available_B.
-
Second Pass (A-B Operations):
- After the first pass, some
B characters might still be available.
- Also, keep track of the number of available
A characters (available_A).
- Pair the remaining
B characters with A characters to perform A-B operations.
The total number of operations is the sum of B-C and A-B operations.
Here's the implementation in C++:
#include <bits/stdc++.h>
using namespace std;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
string S;
cin >> S;
long long available_A = 0;
long long available_B = 0;
long long operations = 0;
for(char c: S){
if(c == 'A'){
available_A++;
}
else if(c == 'B'){
available_B++;
}
else if(c == 'C'){
if(available_B > 0){
operations++;
available_B--;
}
}
}
// Now pair remaining B's with A's
operations += min(available_A, available_B);
cout << operations;
}
Explanation of the Code:
-
Input Handling:
- The string
S is read from the input.
-
Initialization:
available_A and available_B are initialized to count the number of A and B characters that can potentially be part of an operation.
operations keeps track of the total number of operations performed.
-
Processing the String:
- Iterate through each character in the string:
- If the character is
A, increment available_A.
- If the character is
B, increment available_B.
- If the character is
C, check if there's an available B to pair with. If so, perform a B-C operation, increment operations, and decrement available_B.
-
Final Pairing (A-B Operations):
- After processing all characters for
B-C operations, pair the remaining B characters with A characters.
- The number of such possible
A-B operations is the minimum of available_A and available_B.
- Add this to the total
operations.
-
Output:
- Finally, the total number of operations is printed.
Example:
Consider the string ABCBA:
- First Pass (B-C Operations):
- Pair
B2 with C3 → 1 operation.
- Second Pass (A-B Operations):
- Pair
A1 with B4 → 1 operation.
Total operations = 2, which matches the sample explanation.
Complexity Analysis:
- Time Complexity: O(N), where N is the length of the string
S, because we iterate through the string only once.
- Space Complexity: O(1), as we use a fixed amount of additional space regardless of the input size.
This approach ensures that we maximize the number of operations by first prioritizing B-C pairings and then using any remaining B characters for A-B pairings.