QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#400909#6299. Binary StringNyansWA 248ms4032kbC++141.5kb2024-04-27 18:05:552024-04-27 18:05:56

Judging History

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

  • [2024-04-27 18:05:56]
  • 评测
  • 测评结果:WA
  • 用时:248ms
  • 内存:4032kb
  • [2024-04-27 18:05:55]
  • 提交

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);
    }
}

Details

Tip: Click on the bar to expand more detailed information

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'