QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#226333#6299. Binary Stringucup-team1198RE 0ms3620kbC++145.0kb2023-10-25 20:24:202023-10-25 20:24:20

Judging History

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

  • [2023-10-25 20:24:20]
  • 评测
  • 测评结果:RE
  • 用时:0ms
  • 内存:3620kb
  • [2023-10-25 20:24:20]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;
#define ll long long
#define pii pair<int, int>
#define ld long double
#define all(a) (a).begin(), (a).end()

const int MAXN = 2e7 + 100;
int pref[MAXN];
vector<int> rel;

int md(int i, int n) {
    if (i >= n) return i - n;
    return i;
}

void solve() {
    string s;
    cin >> s;
    int n = s.size();
    int bal = 0;
    for (int i = 0; i < n; ++i) {
        if (s[i] == '0') --bal;
        else ++bal;
    }
    if (bal == n || bal == -n) {
        cout << "1\n";
        return;
    }
    if (s[0] != '0' || s.back() != '1') {
        int id = -1;
        for (int i = 1; i < n; ++i) {
            if (s[i - 1] == '1' && s[i] == '0') {
                id = i;
            }
        }
        string s1;
        for (int i = id; i < n; ++i) {
            s1.push_back(s[i]);
        }
        for (int i = 0; i < id; ++i) {
            s1.push_back(s[i]);
        }
        s = s1;
    }
    if (bal < 0) {
        reverse(s.begin(), s.end());
        for (auto& ch : s) {
            ch = '0' + '1' - ch;
        }
        bal = -bal;
    }
    rel.clear();
    for (int i = 0; i < 2 * n; ++i) {
        pref[i + 1] = pref[i] + (s[md(i, n)] == '0');
    }
    for (int i = 1; i < n; ++i) {
        if (s[i] == s[i - 1] && s[i] == '1') {
            rel.push_back(i);
        }
    }
    int start = 0;
    for (int i = n; i < 2 * n; ++i) {
        if (s[i - n] == '1' && s[md(i - 1, n)] == '1') {
            rel.push_back(i);
        } else if (s[i - n] == '0') {
            int k = 0;
            while (i + 1 < 2 * n && s[i + 1 - n] == '0') {
                ++k;
                ++i;
            }
            if (k == 0) continue;
            int ind = rel[rel.size() - k];
            k = pref[i] - pref[ind];
            start = max(start, k);
        }
    }

    if (s[0] != '1' || s.back() != '0') {
        int id = -1;
        for (int i = 1; i < n; ++i) {
            if (s[i - 1] == '0' && s[i] == '1') {
                id = i;
            }
        }
        string s1;
        for (int i = id; i < n; ++i) {
            s1.push_back(s[i]);
        }
        for (int i = 0; i < id; ++i) {
            s1.push_back(s[i]);
        }
        s = s1;
    }

    rel.clear();
    for (int i = 0; i < 2 * n; ++i) {
        pref[i + 1] = pref[i] + (s[md(i, n)] == '1');
    }
    for (int i = 2 * n - 1; i >= n; --i) {
        if (s[i - n] == '1') {
            rel.push_back(i);
        } else if (i != n && s[i - n] == '0' && s[i - 1 - n] == '0') {
            rel.push_back(i);
        }
    }
    /// assert(start <= (int)rel.size());
    string res;
    int lst = 0;
    int cur = 0;
    /// cerr << s << endl;
    if (false) {
        res = s;
    } else {
        for (int i = n - 1; i >= 0; --i) {
            if (s[i] == '0') ++cur;
            if (i != 0 && s[i] == '0' && s[i - 1] == '0') {
                rel.push_back(i);
                /// /// cerr << "add " << i << endl;
            } else if (s[i] == '1') {
                int go = pref[rel[rel.size() - start - 1]] - pref[i + 1];
                int k = 0;
                rel.push_back(i);
                /// /// cerr << "add " << i << endl;
                while (i > 0 && s[i - 1] == '1') {
                    ++k;
                    --i;
                    rel.push_back(i);
                    /// /// cerr << "add " << i << endl;
                }
                if (k + go - start > 0) {
                    res.push_back('0');
                    ++lst;
                    while (lst != cur) {
                        res.push_back('1');
                        res.push_back('0');
                        ++lst;
                    }
                    int len = k + go - start + 1;
                    while (len) {
                        res.push_back('1');
                        --len;
                    }
                }
            }
        }
        char lst = '0';
        if (!res.empty()) {
            lst = res.back();
        }
        while (res.size() < n) {
            res.push_back('0' + '1' - lst);
            lst = '0' + '1' - lst;
        }
    }
    assert(res.size() == s.size());
    vector<int> pi(res.size());
    for (int i = 1; i < res.size(); ++i) {
        int j = pi[i - 1];
        while (j != -1 && res[j] != res[i])
            j = j == 0 ? -1 : pi[j - 1];
        pi[i] = j + 1;
    }
    /// cerr << "start: "<< start << " " << res << "\n";
    for (int i = res.size(); i > 0; i = pi[i - 1]) {
        if (res.size() % (res.size() - pi[i - 1]) == 0) {
            /// cerr << "!!!!!!!\n";
            cout << start + res.size() - pi[i - 1] << "\n";
            return;
        }
    }
    /// cerr << "!!!!!\n";
    cout << start + res.size() << "\n";
}

signed main() {
    ios_base::sync_with_stdio(0);
    cin.tie(0);
    cout.tie(0);

    int tst;
    cin >> tst;
    while (tst--) {
        solve();
    }

    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 0ms
memory: 3620kb

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: