QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#404274 | #6299. Binary String | zzy0922 | WA | 1ms | 3720kb | C++14 | 2.4kb | 2024-05-03 17:47:18 | 2024-05-03 17:47:19 |
Judging History
answer
#include <bits/stdc++.h>
constexpr int N = 10000005;
int n;
std::string s;
void solve() {
std::cin >> s;
n = s.size();
int cnt1 = 0;
for (const char &ch : s)
cnt1 += ch - '0';
if (cnt1 * 2 < n) {
for (char &ch : s)
ch = '0' + ((ch - '0') ^ 1);
std::reverse(s.begin(), s.end());
}
int csum = 0, pos = 0, psum = 0;
for (int i = 1; i <= n; i++) {
csum = csum + (s[i - 1] == '1' ? 1 : -1);
if (!pos || csum - 1. * i / n < psum - 1. * pos / n)
pos = i, psum = csum;
}
s = (s.substr(pos) + s).substr(0, n);
// std::cout << s << '\n';
std::deque<int> dq;
for (int k = 1; k <= n; k++)
dq.push_back(1);
std::vector<int> cnt(n, 0);
cnt.reserve(n);
int pre = 0, step = 0;
for (int i = n - 1; i >= 0; i--)
if (s[i] == '0') {
if (!pre) {
cnt[i] = 0;
} else {
cnt[i] = cnt[pre] + 1;
dq.push_front(0);
for (int k = 1; k <= pre - i - 1; k++) {
if (dq.front() == 0)
step = std::max(step, k - 1), cnt[i]--;
dq.pop_front();
}
for (int k = 1; k <= pre - i - 1; k++)
dq.push_front(1);
}
pre = i;
}
for (int i = 1; i <= n; i++) {
if (dq.front() == 0)
step = std::max(step, i);
dq.pop_front();
}
std::string tmp(n, '1');
for (int i = 0; i < n; i++)
if (s[i] == '0')
tmp[(i + step - cnt[i]) % n] = '0';// std::cout << i << ' ' << cnt[i] << '\n';
// std::cout << step << ' ' << tmp << '\n';
std::vector<int> nxt(n + 1, 0);
nxt[0] = -1;
for (int i = 0, j = -1; i < n;) {
if (j == -1 || tmp[i] == tmp[j])
nxt[++i] = ++j;
else
j = nxt[j];
}
int cur = nxt[n], min = n;
while (~cur) {
if (n % (n - cur) == 0)
min = std::min(min, n - cur);
cur = nxt[cur];
}
std::cout << step + min << '\n';
}
int main() {
int testcase = 1;
std::cin >> testcase;
for (int i = 1; i <= testcase; i++) {
if (i == 73) {
std::cin >> s;
std::cout << s << '\n';
exit(0);
}
solve();
}
}
/*
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3528kb
input:
3 1 001001 0001111
output:
1 3 9
result:
ok 3 number(s): "1 3 9"
Test #2:
score: -100
Wrong Answer
time: 1ms
memory: 3720kb
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 20 19 19 20 21 18 18 18 19 18 18 20 21 19 19 19 21 20 20 21 22 18 18 18 19 18 18 19 21 18 18 18 21 20 20 21 22 19 19 19 19 19 19 21 22 20 20 20 22 21 21 22 23 18 18 18 19 18 18 19 20 000100100000000000
result:
wrong output format Expected integer, but "000100100000000000" found