QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#404273 | #6299. Binary String | zzy0922 | WA | 1ms | 3464kb | C++14 | 2.3kb | 2024-05-03 17:45:24 | 2024-05-03 17:45:24 |
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 - 1);
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;
while (testcase--) {
solve();
}
}
/*
*/
詳細信息
Test #1:
score: 0
Wrong Answer
time: 1ms
memory: 3464kb
input:
3 1 001001 0001111
output:
1 3 8
result:
wrong answer 3rd numbers differ - expected: '9', found: '8'