Search for a command to run...
Progressive hints first, then the full explanation and implementation when you're ready to cash out.
Review status
AI-generated and still unreviewed. Double-check the details before internalizing them.
Hints
Open only as much as you need to keep the solve alive.
After any removals, the remaining part of is still one contiguous substring of the original alternating string.
Look at a current substring of even length. Its two ends are different. If you take one end now, what letter is forced on the next move?
An even-length substring always contributes a 2-character block: either ab or ba. The order can vary, but the two letters in that block must be different.
If is odd, the original starts and ends with a, so the first output character is forced to be a. After that, the remaining substring has even length.
So check blocks when is even, and blocks when is odd. A block is impossible only if both known characters are equal; ? can always be filled.
Research checked: the official problem statement, the official round editorial page whose 2202B section was only a placeholder in the fetched page, and a Codeforces personal tutorial describing the greedy/DP view. The statement is the source of truth here; the proof below comes from the invariant, not copied text.
Key Invariant
At every moment, the remaining is a contiguous substring of the original alternating string abab.... Since it is alternating, the two ends are:
That tiny parity fact is basically the whole problem. The DP tag is not lying, but the state collapses so hard that it becomes a greedy check. Nice when that happens.
Even-Length Substrings
Suppose the current substring has even length. Then its endpoints are different, one a and one b.
If Bob removes endpoint character , the remaining substring has odd length. In an alternating odd-length substring, both endpoints are equal, and here they are both the opposite character . Therefore the next removed character is forced to be .
So every two moves starting from an even-length substring output exactly one a and one b, in either order:
After those two moves, the remaining substring again has even length, so the same logic repeats. This means that if is even, the generated string is split into blocks
and each block must contain different letters.
Do not check every adjacent pair. That is the classic faceplant here. For example, when , baab is valid, even though the middle adjacent pair is aa. Only the operation-aligned pairs matter.
Odd
If is odd, the initial string is
so both endpoints are a. The first output character is forced to be a.
After removing that first a, the remaining substring has even length, so the previous block argument applies to positions
Again, each such block must contain one a and one b.
Handling ?
We are not asked to construct the final string, only to decide if some completion exists.
For a required 2-character block:
ab and ba are fine,a?, ?a, b?, ?b, and ?? are all fine because we can choose the missing letters to make the block different,aa and bb are impossible.For odd , position must be a, so if is fixed as b, the answer is immediately NO.
Algorithm
Use 0-based indexing in code.
s[0] == 'b', print NO.start = n % 2.
start = 0, checking pairs (0,1), (2,3), ....start = 1, checking pairs (1,2), (3,4), ....NO.YES.Correctness Proof
We prove the algorithm prints YES exactly when a valid completion exists.
First, consider any even-length remaining substring. Its endpoints are different. If the first removed character in a two-move block is , then the remaining substring has odd length and both endpoints are , so the second removed character is forced to be . Therefore every such block contains one a and one b. Thus, for even , every valid generated string must have different letters in positions .
For odd , the initial endpoints are both a, so the first generated character must be a. After that move, the remaining substring has even length, so the same two-move block argument applies to positions . Therefore every valid generated string must satisfy exactly the conditions checked by the algorithm.
Now we prove sufficiency. If the algorithm accepts, then every required block can be completed as either ab or ba, and for odd the first character can be a. Starting from any even-length substring, Bob can choose the endpoint matching the desired first character of the next block; the second character is then forced to be the opposite one, exactly matching the completed block. Repeating this realizes all blocks. For odd , Bob first removes either endpoint a, then follows the same construction. Hence a valid generated completion exists.
So the algorithm is both necessary and sufficient.
Complexity
Each test case is scanned once, so the time complexity is and the memory usage is besides the input string. Across all tests, this is , easily inside the limits.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
string s;
cin >> n >> s;
bool ok = true;
if (n % 2 == 1 && s[0] == 'b') ok = false;
for (int i = n % 2; ok && i + 1 < n; i += 2) {
if (s[i] != '?' && s[i + 1] != '?' && s[i] == s[i + 1]) {
ok = false;
}
}
cout << (ok ? "YES\n" : "NO\n");
}
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
int n;
string s;
cin >> n >> s;
bool ok = true;
if (n % 2 == 1 && s[0] == 'b') ok = false;
for (int i = n % 2; ok && i + 1 < n; i += 2) {
if (s[i] != '?' && s[i + 1] != '?' && s[i] == s[i + 1]) {
ok = false;
}
}
cout << (ok ? "YES\n" : "NO\n");
}
}