QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#404034#6299. Binary Stringalpha1022TL 1ms3796kbC++141.9kb2024-05-03 09:30:322024-05-03 09:30:33

Judging History

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

  • [2024-05-03 09:30:33]
  • 评测
  • 测评结果:TL
  • 用时:1ms
  • 内存:3796kb
  • [2024-05-03 09:30:32]
  • 提交

answer

#include <bits/stdc++.h>

using namespace std;

const int N = 1e7;

char a[N + 5];
int n, s[N + 5], pos;
pair<int, int> stk[N + 5];
int top, res;
int b[N + 5], nxt[N + 5], ans;

void solve() {
  scanf("%s", a + 1), n = strlen(a + 1); int c1 = 0;
  for (int i = 1; i <= n; ++i) a[i] ^= '0', c1 += a[i];
  if (c1 < n - c1) {
    reverse(a + 1, a + n + 1);
    for (int i = 1; i <= n; ++i) a[i] ^= 1;
  }
  pos = -1;
  for (int i = 1; i <= n; ++i) {
    s[i] = s[i - 1] + (a[i] ? 1 : -1);
    if (pos == -1 || s[i - 1] < s[pos - 1]) pos = i;
  }
  rotate(a + 1, a + pos, a + n + 1), top = res = 0;
  for (int l = 1, r; l <= n; l = r + 1) {
    for (r = l; r < n && a[r + 1] == a[l]; ++r);
    if (l == r) continue;
    if (a[l]) stk[++top] = {l, r - l + 1};
    else {
      int len = r - l + 1;
      while (top && len > 1) {
        auto &[l1, len1] = stk[top];
        int c = min(len1 - 1, len - 1);
        len1 -= c, len -= c, res = max(res, (r - len - l1 - len1 + 1) / 2);
        if (len1 == 1) --top;
      }
    }
  }
  if (!top) { printf("%d\n", res + (n & 1 ? n : 2)); return ; }
  for (int i = 1; i <= top; ++i) { auto &[l, len] = stk[i]; l = (l + res - 1) % n + 1; }
  fill_n(b + 1, n, 0);
  for (int i = 1; i <= top; ++i) {
    auto [l, len] = stk[i];
    for (int j = l; j < l + len; ++j) b[(j - 1) % n + 1] = 1;
    if ((l + len - 1) % n + 1 == stk[i % top + 1].first) continue;
    for (int j = (l + len) % n + 1; j != stk[i % top + 1].first; j = (j + 1) % n + 1) b[j] = 1;
  }
  for (int i = 2, j = 0; i <= n; ++i) {
    for (; j && b[j + 1] != b[i]; j = nxt[j]);
    if (b[j + 1] == b[i]) ++j;
    nxt[i] = j;
  }
  ans = res + n;
  if (!(n % (n - nxt[n]))) ans -= nxt[n];
  printf("%d\n", ans);
}

int T;

int main() {
  //freopen("game.in", "r", stdin), freopen("game.out", "w", stdout);
  for (scanf("%d", &T); T; --T) solve();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3
1
001001
0001111

output:

1
3
9

result:

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

Test #2:

score: -100
Time Limit Exceeded

input:

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

output:


result: