QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#112330 | #6299. Binary String | ethening | WA | 211ms | 3500kb | C++20 | 2.1kb | 2023-06-11 10:50:44 | 2023-06-11 10:50:48 |
Judging History
answer
#include "bits/stdc++.h"
#include <algorithm>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
void solve() {
string s;
cin >> s;
int n = s.size();
int cnt0 = count(begin(s), end(s), '0');
int cnt1 = count(begin(s), end(s), '1');
if (cnt0 == 0 || cnt1 == 0) {
cout << 1 << "\n";
return;
}
if (cnt0 > cnt1) {
swap(cnt0, cnt1);
for (auto &ch : s) ch = '0' + '1' - ch;
reverse(begin(s), end(s));
}
// cnt0 <= cnt1 after this
{
int cnt = 0;
int mx = -1;
int mxindex = -1;
for (int i = 0; i < n; i++) {
if (s[i] == '0') --cnt;
else ++cnt;
if (cnt > mx) {
mx = cnt;
mxindex = i;
}
}
string tmp;
for (int i = mxindex + 1; i < n; i++) tmp += s[i];
for (int i = 0; i <= mxindex; i++) tmp += s[i];
s = tmp;
}
// every 0 there is at least a 1 to pair with after it
vector<int> prev0, after0;
for (int i = 0; i < n; i++) {
if (s[i] == '0') prev0.push_back(i);
}
after0.push_back(prev0[0]);
for (int i = 1; i < cnt0; i++) {
after0.push_back(max(prev0[i], after0[i - 1] + 2));
}
int ans = 0;
for (int i = 0; i < cnt0; i++) {
ans = max(ans, after0[i] - prev0[i]);
}
string s2(n, '1');
for (int i = 0; i < cnt0; i++) {
s2[after0[i]] = '0';
}
const ll MOD = 998244353ll;
const ll base = 53;
vector<ll> pwr(n + 1);
for (int i = 0; i < n; i++) {
if (i == 0) {
pwr[0] = base;
}
else {
pwr[i] = pwr[i - 1] * base % MOD;
}
}
ll cur_hash = 0;
for (int i = 0; i < n; i++) {
if (s2[i] == '1') {
cur_hash += pwr[n - 1 - i];
if (cur_hash > MOD) cur_hash -= MOD;
}
}
ll first_hash = cur_hash;
int first_rep = n;
for (int i = 0; i < n - 1; i++) {
if (s2[i] == '1') {
cur_hash -= pwr[n - 1];
}
cur_hash = cur_hash * pwr[0];
if (s2[i] == '1') {
cur_hash += pwr[0];
}
cur_hash = (cur_hash % MOD + MOD) % MOD;
if (cur_hash == first_hash) {
first_rep = i + 1;
break;
}
}
ans += first_rep;
cout << ans << "\n";
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int t;
cin >> t;
while (t--) {
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 2ms
memory: 3500kb
input:
3 1 001001 0001111
output:
1 3 9
result:
ok 3 number(s): "1 3 9"
Test #2:
score: -100
Wrong Answer
time: 211ms
memory: 3420kb
input:
262144 000000000000000000 100000000000000000 010000000000000000 110000000000000000 001000000000000000 101000000000000000 011000000000000000 111000000000000000 000100000000000000 100100000000000000 010100000000000000 110100000000000000 001100000000000000 101100000000000000 011100000000000000 11110000...
output:
1 18 18 19 18 18 19 20 18 18 18 19 19 19 20 21 18 18 18 19 18 18 19 20 19 19 19 20 20 20 21 22 18 18 18 19 18 18 19 20 18 18 18 19 19 19 20 21 19 19 19 19 19 19 20 21 20 20 20 21 21 21 22 23 18 18 18 19 18 18 19 20 18 18 18 19 19 19 20 21 18 18 18 19 18 18 19 20 19 19 19 20 20 20 21 22 19 19 19 19 1...
result:
wrong answer 12th numbers differ - expected: '20', found: '19'