QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#98819#6299. Binary StringstkwillWA 151ms3472kbC++141.6kb2023-04-20 10:47:202023-04-20 10:47:24

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-20 10:47:24]
  • 评测
  • 测评结果:WA
  • 用时:151ms
  • 内存:3472kb
  • [2023-04-20 10:47:20]
  • 提交

answer

#include <bits/stdc++.h>

inline int Main() {
    std::string s;
    std::cin >> s;
    int c0 = 0, c1 = 0;
    for (auto ch : s) ++(ch == '0' ? c0 : c1);
    if (c0 < c1) {
        std::reverse(s.begin(), s.end()), std::swap(c0, c1);
        for (auto &ch : s) ch ^= 1;
    }
    int n = s.size();
    std::vector<int> S(n + 1);
    for (int i = n - 1; i >= 0; --i) S[i] = S[i + 1] + (s[i] == '0' ? -1 : 1);
    std::vector<int> P(n);
    for (int i = 1; i < n; ++i) P[i] = S[P[i - 1]] > S[i] ? P[i - 1] : i;
    int Max = 0;
    std::string t;
    t.resize(n, '0');
    if (s[n - 1] == '1') Max = n - P[n - 1] - 1, t[(n - 1 + S[P[n - 1]] - 1) % n] = '1';
    int p1 = n - 1;
    for (int i = n - 2; i >= 0; --i) {
        if (s[i] == '1') {
            int M1 = S[0] - S[i + 1] + S[p1] - 1, p2 = P[i], M2 = S[p2] - S[i + 1] - 1;
            auto [Ma, pa] = std::max(std::make_pair(M1, p1), std::make_pair(M2, p2));
            t[(i + Ma) % n] = '1', Max = std::max(Max, (i + n - pa) % n);
        }
        p1 = S[p1] > S[i] ? p1 : i;
    }
    std::vector<int> Fail(n, -1);
    for (int i = 1; i < n; ++i) {
        int &j = Fail[i] = Fail[i - 1];
        while (j != -1 && t[j + 1] != t[i]) j = Fail[j];
        j += t[j + 1] == t[i];
    }
    for (int j = Fail[n - 1]; j != -1; j = Fail[j]) if (n % (n - 1 - j) == 0) return std::cout << Max + (n - 1 - j) << '\n', 0;
    std::cout << Max + n << '\n';
    return 0;
}

int main() {
    std::ios::sync_with_stdio(false);
    std::cin.tie(nullptr), std::cout.tie(nullptr);
    int T, Ret = 0;
    std::cin >> T;
    while (T--) Ret |= Main();
    return Ret;
}

详细

Test #1:

score: 100
Accepted
time: 2ms
memory: 3472kb

input:

3
1
001001
0001111

output:

1
3
9

result:

ok 3 number(s): "1 3 9"

Test #2:

score: -100
Wrong Answer
time: 151ms
memory: 3432kb

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
21
19
19
20
21
18
18
18
19
18
18
21
22
19
19
19
22
20
20
21
22
18
18
18
19
18
18
19
23
18
18
18
23
21
21
22
23
19
19
19
19
19
19
22
23
20
20
20
23
21
21
22
23
18
18
18
19
18
18
19
20
18
18
18
21
19
19
23
24
18
18
18
19
18
18
23
24
21
21
21
24
22
22
23
24
19
19
19
19
1...

result:

wrong answer 12th numbers differ - expected: '20', found: '21'