QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#400909 | #6299. Binary String | Nyans | WA | 248ms | 4032kb | C++14 | 1.5kb | 2024-04-27 18:05:55 | 2024-04-27 18:05:56 |
Judging History
answer
#include <bits/stdc++.h>
int main() {
int T;
std::cin >> T;
while (T--) {
std::string s;
std::cin >> s;
int cnt = 0, n = s.size();
for (int i = 0; i < n; ++i) cnt += s[i] == '1';
if (cnt < n - cnt) {
for (int i = 0; i < n; ++i) s[i] = s[i] ^ 1;
std::reverse(s.begin(), s.end());
}
std::vector <int> preSum(n);
for (int i = 0; i < n; ++i)
preSum[i] = (s[i] == '1'? 1: -1) + (i > 0? preSum[i - 1]: 0);
std::rotate(s.begin(), s.begin() + (std::min_element(preSum.begin(), preSum.end()) - preSum.begin()) + 1, s.end());
int sum = 0, pos = 0;
std::vector <int> one;
int ans = 0;
for (int i = 0; i < n - 1; ++i) {
if (s[i] == '1' && s[i + 1] == '1') one.push_back(i);
if (s[i] == '0' && s[i + 1] == '0') {
ans = std::max(ans, (i - one.back()) / 2);
one.pop_back();
}
}
std::fill(s.begin(), s.end(), 0);
for (int p: one) s[p - one[0]] = s[p - one[0] + 1] = '1';
for (int i = 0; i < n; ++i) if (!s[i]) s[i] = s[i - 1] ^ 1;
std::vector <int> kmp(n);
for (int i = 1, k = 0; i < n; ++i) {
while (k > 0 && s[i] != s[k]) k = kmp[k - 1];
kmp[i] = s[i] == s[k]? ++k: 0;
}
if (n % (n - kmp[n - 1]) == 0) ans += (n - kmp[n - 1]);
else ans += n;
printf("%d\n", ans);
}
}
详细
Test #1:
score: 100
Accepted
time: 0ms
memory: 4032kb
input:
3 1 001001 0001111
output:
1 3 9
result:
ok 3 number(s): "1 3 9"
Test #2:
score: -100
Wrong Answer
time: 248ms
memory: 3796kb
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 18 18 18 20 19 19 21 22 18 18 18 19 18 18 21 22 20 20 20 22 21 21 22 23 19 19 19 19 1...
result:
wrong answer 7696th numbers differ - expected: '12', found: '21'