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.
Ignore the * visually for a second. After several moves, the non-* characters are still in their original relative order.
A legal move is exactly deleting two equal adjacent characters from the current remaining string.
So the question becomes: can the string be reduced to empty by repeatedly applying for some letter ?
Try processing left to right with a stack. If the new character equals the stack top, those two can annihilate; otherwise it has to wait.
The stack is not just a greedy hack. The rewrite rule has a unique reduced form, so the string is winnable iff the final stack is empty.
Research found the official Codeforces Round 1084 editorial for 2200C. Its key hint is to think of the operation as deleting characters instead of replacing them with *, and its accepted-code idea is either repeated adjacent-pair deletion or a stack cancellation pass. The supplied statement matches that exactly.
The annoying-looking * condition is actually the whole trick. Suppose we look only at the characters that are not *. Their relative order never changes. A move chooses two equal non-* characters with only * between them, meaning they are adjacent in the current remaining string.
So the game is equivalent to this simpler process:
That is the full reduction. No hidden magic. The stars are just deleted characters wearing a cheap disguise.
Now process the string from left to right using a stack:
At the end, answer YES iff the stack is empty.
Why this works:
The operation is the rewrite rule
for any lowercase letter . The stack algorithm computes the reduced form after applying all possible adjacent equal cancellations.
We need to justify that this greedy reduction does not accidentally choose the wrong pair. Consider any place where choices could conflict. The only real overlap is something like : deleting the first two or the last two both leaves exactly . For longer runs, the same idea repeats. Different letters cannot conflict because only equal adjacent letters may be removed. Therefore the final reduced string is unique.
The stack maintains this invariant:
After reading the first characters, the stack equals the reduced form of the prefix .
When reading a new character :
By induction, after all characters, the stack is the unique reduced form of the entire string. The original game is winnable exactly when this reduced form is empty.
Edge cases:
NO.aaaa reduce fully: aa cancels, then aa cancels.abba do not reduce fully: no adjacent equal pair appears at the start, so there is no legal first move.Complexity is clean:
per test case, with
memory. Since , this is miles under the limit.
#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;
vector<char> st;
for (char c : s) {
if (!st.empty() && st.back() == c) st.pop_back();
else st.push_back(c);
}
cout << (st.empty() ? "YES" : "NO") << '\n';
}
return 0;
}#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;
vector<char> st;
for (char c : s) {
if (!st.empty() && st.back() == c) st.pop_back();
else st.push_back(c);
}
cout << (st.empty() ? "YES" : "NO") << '\n';
}
return 0;
}