QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#367955 | #6299. Binary String | yaoxi_std | RE | 0ms | 9764kb | C++14 | 2.4kb | 2024-03-26 17:41:12 | 2024-03-26 17:41:13 |
Judging History
answer
#include <bits/stdc++.h>
using namespace std;
#define debug(fmt, ...) \
fprintf(stderr, "[%d] " fmt "\n", __LINE__, ##__VA_ARGS__)
template <class _Tx, class _Ty>
inline void chkmin(_Tx& x, const _Ty& y) { if (y < x) x = y; }
template <class _Tx, class _Ty>
inline void chkmax(_Tx& x, const _Ty& y) { if (x < y) x = y; }
bool Mbe;
using ll = long long;
constexpr int N = 1e7 + 10;
int n, a[N], b[N], kmp[N];
string str;
pair<int, int> stk[N];
void mian() {
cin >> str;
n = str.size();
for (int i = 0; i < n; ++i) a[i] = str[i] == '1';
if (count(a, a + n, 0) * 2 > n) {
reverse(a, a + n);
for_each(a, a + n, [](int& x) { x ^= 1; });
}
if (all_of(a, a + n, [](int x) { return x == 1; }))
return cout << "1\n", void();
int tp = 0, pos = 0, mini = 0;
for (int i = 0; i < n; ++i) {
if (tp < mini) {
pos = i;
mini = tp;
}
tp += a[i] == 1 ? 1 : -1;
}
rotate(a, a + pos, a + n);
if (a[n - 1] == 1) {
pos = n - 1;
while (a[pos - 1] == 1) --pos;
rotate(a, a + pos, a + n);
}
tp = 0;
int ans = 0;
for (int l = 0, r; l < n; l = r) {
r = l;
while (r < n && a[r] == a[l]) ++r;
if (r - l == 1) continue;
if (a[l] == 1) {
stk[++tp] = {l, r - l - 1};
} else {
int rem = r - l - 1;
while (rem) {
int val = min(rem, stk[tp].second - stk[tp].first);
chkmax(ans, (r - stk[tp].first) / 2 - 1);
rem -= val;
stk[tp].second -= val;
if (stk[tp].first == stk[tp].second) --tp;
}
}
}
fill(b, b + n, 0);
if (!tp) {
for (int i = 1; i < n; i += 2) b[i] = 1;
} else {
while (tp) {
for (int i = 0; i < stk[tp].second; ++i) b[stk[tp].first + i] = 1;
--tp;
}
for (int i = 0; i < n + n; ++i) {
if (b[i % n] == '1' && b[(i + 1) % n] == '0') {
b[(i + 2) % n] = '1';
}
}
}
kmp[0] = -1;
for (int i = 1; i < n; ++i) {
int p = kmp[i - 1];
while (p != -1 && b[p + 1] != b[i]) p = kmp[p];
kmp[i] = b[p + 1] == b[i] ? ++p : p;
}
ans += n % (n - kmp[n - 1] - 1) ? n : n - kmp[n - 1] - 1;
cout << ans << '\n';
}
bool Med;
int main() {
// debug("Mem: %.4lfMB.", fabs(&Med - &Mbe) / 1048576);
cin.tie(0)->sync_with_stdio(0);
int cas;
cin >> cas;
while (cas--) mian();
return 0;
}
/*
g++ -std=c++14 -O2 -o B B.cpp -Wall -Wextra
-Wshadow -fsanitize=address,undefined -g
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 9764kb
input:
3 1 001001 0001111
output:
1 3 9
result:
ok 3 number(s): "1 3 9"
Test #2:
score: -100
Runtime Error
input:
262144 000000000000000000 100000000000000000 010000000000000000 110000000000000000 001000000000000000 101000000000000000 011000000000000000 111000000000000000 000100000000000000 100100000000000000 010100000000000000 110100000000000000 001100000000000000 101100000000000000 011100000000000000 11110000...