QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#404266#6299. Binary Stringzzy0922RE 1ms3500kbC++142.3kb2024-05-03 17:33:402024-05-03 17:33:42

Judging History

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

  • [2024-05-03 17:33:42]
  • 评测
  • 测评结果:RE
  • 用时:1ms
  • 内存:3500kb
  • [2024-05-03 17:33:40]
  • 提交

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

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 1ms
memory: 3500kb

input:

3
1
001001
0001111

output:

1
3
9

result:

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

Test #2:

score: -100
Runtime Error

input:

262144
000000000000000000
100000000000000000
010000000000000000
110000000000000000
001000000000000000
101000000000000000
011000000000000000
111000000000000000
000100000000000000
100100000000000000
010100000000000000
110100000000000000
001100000000000000
101100000000000000
011100000000000000
11110000...

output:


result: