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.
For a fixed cut after position , you compare with . Since the string is binary, the only pair that can kill the cut is prefix , suffix .
Look at the cut after position . It compares only and . So if , the string is automatically perfect.
Now assume . If there is another zero later, let be the first position after with .
Cut right before that first later zero, so . The first compared pair is , and every other compared prefix character is before but after position , so it must be .
Therefore the only non-perfect binary string is exactly . Count all completions, then subtract this one bad completion only if it is compatible with the pattern of fixed characters.
Do not try to count valid cuts directly. A string can have many valid cuts, so that path turns into duplicated-counting sludge fast. Instead, characterize the strings that have no valid cut.
Let the completed binary string be .
For a cut after position , the prefix is and the suffix starts at . The condition is:
for all compared positions .
Since characters are only and , the only bad compared pair is:
Everything else works: , , and .
Key claim: a binary string is not perfect iff it is exactly
There are two cases.
First, if , use the cut after position . Then only one pair is compared: with . Since , we have , so the cut works.
Now suppose .
If there is another zero somewhere after the first position, let be the first such position:
and there is no zero among positions .
So all those middle characters are .
Cut after position .
The comparisons are between:
For , this is:
which is:
so it is fine.
For every compared , the prefix character lies in positions . By the choice of , all of those are . A prefix can dominate anything, so every remaining compared pair is also fine.
So if the string starts with but contains any later , it is perfect.
If the string is , then for every cut after position , the first compared pair is:
Here , and since , we have . So every cut starts with:
which fails instantly. Brutal, but clean.
So the entire problem collapses to this:
Let be the number of question marks in . There are
possible completions total.
We subtract only if the bad string can actually be formed from .
That requires:
1;0.If both are true, exactly one completion is bad: set the first character to and every later unknown to .
So:
Implementation is just one scan. Count question marks, check whether any fixed zero appears after the first character, and subtract the bad completion if needed. Complexity is per test case, with total over all tests.
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 998244353;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
string s;
cin >> s;
ll total = 1;
for (char c : s) {
if (c == '?') total = total * 2 % MOD;
}
bool badCompatible = (s[0] != '1');
for (int i = 1; i < (int)s.size(); i++) {
if (s[i] == '0') badCompatible = false;
}
ll ans = total - (badCompatible ? 1 : 0);
ans %= MOD;
if (ans < 0) ans += MOD;
cout << ans << '\n';
}
}#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll MOD = 998244353;
void setIO() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
}
int main() {
setIO();
int t;
cin >> t;
while (t--) {
string s;
cin >> s;
ll total = 1;
for (char c : s) {
if (c == '?') total = total * 2 % MOD;
}
bool badCompatible = (s[0] != '1');
for (int i = 1; i < (int)s.size(); i++) {
if (s[i] == '0') badCompatible = false;
}
ll ans = total - (badCompatible ? 1 : 0);
ans %= MOD;
if (ans < 0) ans += MOD;
cout << ans << '\n';
}
}