QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#96597#6299. Binary Stringwilliam555WA 212ms11692kbC++141.4kb2023-04-14 16:42:592023-04-14 16:43:01

Judging History

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

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-04-14 16:43:01]
  • 评测
  • 测评结果:WA
  • 用时:212ms
  • 内存:11692kb
  • [2023-04-14 16:42:59]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
const int N=1e7+5;
int n,tp,fail[N];
pair<int,int> st[N];
char a[N],b[N];
void trans(int p){
	static char b[N];
	for(int i=0,j=p;i<n;i++,j=(j+1)%n)b[i]=a[j];
	for(int i=0;i<n;i++)a[i]=b[i];
}
void solve(){
	scanf("%s",a);
	n=strlen(a);
	int c0=0,c1=1;
	for(int i=0;i<n;i++)(a[i]=='0'?c0:c1)++;
	if(c0>c1){
		for(int i=0;i<n;i++)a[i]^=1;
		reverse(a,a+n);
		swap(c0,c1);
	}
	if(c1==n){puts("1");return;}
	int p=n-1;
	for(int i=0;i+1<n;i++)if(a[i]=='0'&&a[i+1]=='1')p=(i+1)%n;
	trans(p);
	int now=0,mn=0;p=0;
	for(int i=0,j;i<n;i=j+1){
		for(j=i;j+1<n&&a[j+1]==a[i];j++);
		if(i<j){
			if(a[i]=='0')now-=j-i;
			else now+=j-i;
			if(now<mn)mn=now,p=(j+1)%n;
		}
	}
	trans(p);
	int mx=0;tp=0;
	for(int i=0,j;i<n;i=j+1){
		for(j=i;j+1<n&&a[j+1]==a[i];j++);
		if(i==j)continue;
		if(a[i]=='1'){
			st[++tp]=make_pair(i,j);
			continue;
		}
		int l=j-i;
		while(l){
			int x=min(l,st[tp].second-st[tp].first);
			l-=x,st[tp].second-=x;
			mx=max(mx,j-st[tp].second-1>>1);
			if(st[tp].second<=st[tp].first)tp--;
		}
	}
	for(int i=1;i<=n;i++)b[i]=0;
	for(int i=1;i<=tp;i++)
		for(int j=st[i].first+1;j<=st[i].second+1;j++)
			b[j]=1;
	for(int i=2,j=0;i<=n;i++){
		while(j&&b[j+1]!=b[i])j=fail[j];
		if(b[j+1]==b[i])j++;
		fail[i]=j;
	}
	int k=n-fail[n];
	if(n%k)k=n;
	printf("%d\n",mx+k);
}
int main(){
	int T;scanf("%d",&T);
	while(T--)solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 3ms
memory: 9776kb

input:

3
1
001001
0001111

output:

1
3
9

result:

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

Test #2:

score: -100
Wrong Answer
time: 212ms
memory: 11692kb

input:

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

output:

1
18
18
18
18
18
19
19
18
18
18
20
19
19
20
20
18
18
18
19
18
18
20
21
19
19
19
21
20
20
21
21
18
18
18
19
18
18
19
21
18
18
18
21
20
20
21
22
19
19
19
19
19
19
21
22
20
20
20
22
21
21
22
22
18
18
18
19
18
18
19
20
18
18
18
20
19
19
21
22
18
18
18
19
18
18
21
22
20
20
20
22
21
21
22
23
19
19
19
19
1...

result:

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