QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#301282#6299. Binary StringyllcmWA 114ms3716kbC++141.7kb2024-01-09 17:06:242024-01-09 17:06:25

Judging History

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

  • [2024-01-09 17:06:25]
  • 评测
  • 测评结果:WA
  • 用时:114ms
  • 内存:3716kb
  • [2024-01-09 17:06:24]
  • 提交

answer

#include<bits/stdc++.h>
#define ll long long
#define db double
#define ull unsigned long long
#define pb push_back
#define pii pair<int, int>
#define FR first
#define SE second
using namespace std;
inline int read() {
  int x = 0; bool op = false;
  char c = getchar();
  while(!isdigit(c))op |= (c == '-'), c = getchar();
  while(isdigit(c))x = (x << 1) + (x << 3) + (c ^ 48), c = getchar();
  return op ? -x : x;
}
const int N = 2e7 + 10;
const int INF = 1e9;
int n;
int a[N], b[N], f[N], nxt[N];
char buff[N];
void solve() {
  scanf("%s", buff + 1); n = strlen(buff + 1);
  for(int i = 1; i <= n; i++)a[i] = buff[i] - '0';
  int cnt = 0;
  for(int i = 1; i <= n; i++)cnt += a[i];
  if(cnt > n / 2) {
    for(int i = 1; i <= n; i++)a[i] ^= 1;
    reverse(a + 1, a + 1 + n);
  }
  int pos = 0, ans = 0;
  for(int i = 1; i <= n; i++)ans += (a[i] && a[i % n + 1]);
  for(int i = 1; i <= n; i++)a[i + n] = a[i];
  for(int i = 1, j = 0; i <= n * 2; i++) {
    if(!a[i])continue;
    if(!j || f[j] < i - j - 1)f[i] = 0;
    else f[i] = f[j] + 1;
    j = i; ans = max(ans, f[i]);
  }
  for(int i = 1; i <= n; i++)if(a[i + n] && !f[i + n]) {pos = i; break;}
  rotate(a + 1, a + pos, a + n + 1);
  cnt = 0;
  for(int i = 1; i <= n; i++) {
    if(a[i])cnt++;
    if(!b[i - 1] && cnt)b[i] = 1, cnt--;
    else b[i] = 0;
  }
  for(int i = 2, j = 0; i <= n; i++) {
    while(j && b[j + 1] != b[i])j = nxt[j];
    if(b[j + 1] == b[i])j++;
    nxt[i] = j;
  }
  int cur = nxt[n];
  while(n % (n - cur))cur = nxt[cur];
  ans += n - cur;
  printf("%d\n", ans);
  return ;
}
int main() {
  int test = read();
  while(test--)solve();
  return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3
1
001001
0001111

output:

1
3
9

result:

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

Test #2:

score: -100
Wrong Answer
time: 114ms
memory: 3716kb

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
20
19
19
21
22
20
20
20
22
21
21
22
23
18
18
18
19
18
18
19
20
18
18
18
21
19
19
21
22
18
18
18
19
18
18
21
22
20
20
20
22
21
21
22
23
19
19
19
20
1...

result:

wrong answer 52nd numbers differ - expected: '19', found: '20'