QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#99219#6299. Binary StringfansizheWA 105ms9744kbC++231.2kb2023-04-21 17:15:072023-04-21 17:15:08

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-21 17:15:08]
  • 评测
  • 测评结果:WA
  • 用时:105ms
  • 内存:9744kb
  • [2023-04-21 17:15:07]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
int n;
char s[2000005],t[2000005];
int sum[2000005];
int num[1000005];
int nxt[1000005];
int main(){
	int _;scanf("%d",&_);
	while(_--){
		scanf("%s",s+1);n=strlen(s+1);
		{
			int cnt[2]={};
			for(int i=1;i<=n;i++)cnt[s[i]^'0']++;
			if(cnt[0]<cnt[1]){
				for(int i=1;i<=n;i++)s[i]^=1;
				reverse(s+1,s+n+1);
			}
		}
		int mx=0;
		for(int i=1;i<=n;i++)sum[i]=sum[i-1]+(s[i]^'0')*2-1,mx=max(mx,sum[i]);
		for(int i=1;i<=n;i++)if(sum[i-1]==mx){
			for(int j=1;j<=n;j++)t[j]=s[j];
			for(int j=i;j<=n;j++)s[j-i+1]=t[j];
			for(int j=1;j<i;j++)s[n-i+1+j]=t[j];
			break;
		}
		for(int i=1;i<=n;i++)sum[i]=sum[i-1]+(s[i]^'0')*2-1,assert(sum[i]<=0);
//		printf("s=%s\n",s+1);
		int len=0;
		for(int i=1,j=0;i<=n;i++)if(s[i]=='1'){
			if(sum[i-1]<=sum[j])j=i-1;
			num[i]=sum[i]-sum[j]-1;
			len=max(len,i-j-1);
		}
		for(int i=1;i<=n;i++)t[i]='0';
		for(int i=1;i<=n;i++)if(s[i]=='1')t[(i-(len-num[i])+n-1)%n+1]='1';
//		printf("len=%d\n",len);
//		printf("%s\n",t+1);
		for(int i=2,j=0;i<=n;i++){
			while(j&&t[j+1]!=t[i])j=nxt[j];
			if(t[j+1]==t[i])j++;
			nxt[i]=j;
		}
		printf("%d\n",len+n-nxt[n]);
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3
1
001001
0001111

output:

1
3
9

result:

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

Test #2:

score: -100
Wrong Answer
time: 105ms
memory: 9744kb

input:

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

output:

1
18
17
19
16
16
19
20
15
15
17
18
18
19
20
21
14
14
17
17
16
16
18
20
17
16
19
20
20
20
21
22
13
13
17
19
16
16
17
19
15
15
17
16
19
18
20
21
16
15
19
15
18
19
20
21
19
20
20
21
21
21
22
23
12
12
17
19
16
16
19
18
15
15
17
18
18
17
19
21
14
14
17
15
16
16
16
18
21
19
18
20
20
20
21
22
15
14
19
14
1...

result:

wrong answer 3rd numbers differ - expected: '18', found: '17'