QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#367973#6299. Binary Stringyaoxi_stdWA 85ms9812kbC++142.4kb2024-03-26 17:48:222024-03-26 17:48:22

Judging History

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

  • [2024-03-26 17:48:22]
  • 评测
  • 测评结果:WA
  • 用时:85ms
  • 内存:9812kb
  • [2024-03-26 17:48:22]
  • 提交

answer

#include <bits/stdc++.h>
using namespace std;
#define debug(fmt, ...) \
  fprintf(stderr, "[%d] " fmt "\n", __LINE__, ##__VA_ARGS__)
template <class _Tx, class _Ty>
inline void chkmin(_Tx& x, const _Ty& y) { if (y < x) x = y; }
template <class _Tx, class _Ty>
inline void chkmax(_Tx& x, const _Ty& y) { if (x < y) x = y; }
bool Mbe;
using ll = long long;
constexpr int N = 1e7 + 10;
int n, a[N], b[N], kmp[N];
string str;
pair<int, int> stk[N];
void mian() {
  cin >> str;
  n = str.size();
  for (int i = 0; i < n; ++i) a[i] = str[i] == '1';
  if (count(a, a + n, 0) * 2 > n) {
    reverse(a, a + n);
    for_each(a, a + n, [](int& x) { x ^= 1; });
  }
  if (all_of(a, a + n, [](int x) { return x == 1; }))
    return cout << "1\n", void();
  int tp = 0, pos = 0, mini = 0;
  for (int i = 0; i < n; ++i) {
    if (tp < mini) {
      pos = i;
      mini = tp;
    }
    tp += a[i] == 1 ? 1 : -1;
  }
  rotate(a, a + pos, a + n);
  if (a[n - 1] == 1) {
    pos = n - 1;
    while (a[pos - 1] == 1) --pos;
    rotate(a, a + pos, a + n);
  }
  tp = 0;
  int ans = 0;
  for (int l = 0, r; l < n; l = r) {
    r = l;
    while (r < n && a[r] == a[l]) ++r;
    if (r - l == 1) continue;
    if (a[l] == 1) {
      stk[++tp] = {l, r - l - 1};
    } else {
      int rem = r - l - 1;
      while (rem) {
        int val = min(rem, stk[tp].second);
        chkmax(ans, (r - stk[tp].first) / 2 - 1);
        rem -= val;
        stk[tp].second -= val;
        if (!stk[tp].second) --tp;
      }
    }
  }
  fill(b, b + n, 0);
  if (!tp) {
    for (int i = 1; i < n; i += 2) b[i] = 1;
  } else {
    while (tp) {
      for (int i = 0; i < stk[tp].second; ++i) b[stk[tp].first + i] = 1;
      --tp;
    }
    for (int i = 0; i < n + n; ++i) {
      if (b[i % n] == '1' && b[(i + 1) % n] == '0') {
        b[(i + 2) % n] = '1';
      }
    }
  }
  kmp[0] = -1;
  for (int i = 1; i < n; ++i) {
    int p = kmp[i - 1];
    while (p != -1 && b[p + 1] != b[i]) p = kmp[p];
    kmp[i] = b[p + 1] == b[i] ? ++p : p;
  }
  ans += n % (n - kmp[n - 1] - 1) ? n : n - kmp[n - 1] - 1;
  cout << ans << '\n';
}
bool Med;
int main() {
  // debug("Mem: %.4lfMB.", fabs(&Med - &Mbe) / 1048576);
  cin.tie(0)->sync_with_stdio(0);
  int cas;
  cin >> cas;
  while (cas--) mian();
  return 0;
}
/*
g++ -std=c++14 -O2 -o B B.cpp -Wall -Wextra
-Wshadow -fsanitize=address,undefined -g
*/

詳細信息

Test #1:

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

input:

3
1
001001
0001111

output:

1
3
9

result:

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

Test #2:

score: -100
Wrong Answer
time: 85ms
memory: 9732kb

input:

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

output:

1
18
18
26
18
18
26
26
18
18
18
26
26
25
26
26
18
18
18
19
18
18
26
26
26
24
25
26
26
25
26
26
18
18
18
19
18
18
19
26
18
18
18
26
26
25
26
26
26
24
24
24
25
24
26
26
26
24
25
26
26
25
26
26
18
18
18
20
18
18
19
20
18
18
18
20
19
19
26
26
18
18
18
19
18
18
26
26
26
24
25
26
26
25
26
26
26
23
24
23
2...

result:

wrong answer 4th numbers differ - expected: '19', found: '26'