QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#390789#6299. Binary StringwtcWA 278ms10132kbC++141.4kb2024-04-15 21:46:142024-04-15 21:46:14

Judging History

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

  • [2024-04-15 21:46:14]
  • 评测
  • 测评结果:WA
  • 用时:278ms
  • 内存:10132kb
  • [2024-04-15 21:46:14]
  • 提交

answer

#include<bits/stdc++.h>
#define fo(i,l,r) for(int i=(l);i<=(r);++i)
#define fd(i,l,r) for(int i=(l);i>=(r);--i)
#define fu(i,l,r) for(int i=(l);i<(r);++i)
#define ll long long
using namespace std;
const int N=1e7+7;
int n,m,o,p[N],t[N],f[N],g[N],ans,cnt,mx,st;char s[N<<1];
void kmp(int n,int *s,int *f)
{
	f[1]=0;
	fo(i,2,n)
	{
		f[i]=f[i-1];
		while(f[i]&&s[f[i]+1]!=s[i]) f[i]=f[f[i]];
		if(s[f[i]+1]==s[i]) f[i]++;
	}
}
void work()
{
	scanf("%s",s+1);n=strlen(s+1);
	fo(i,1,n) cnt+=s[i]=='0';
	if(cnt==0||cnt==n){printf("1\n");return;}
	if((cnt<<1)<n)
	{
		fo(i,1,n) s[i]^=1;
		for(int i=1;i<n-i+1;++i) swap(s[i],s[n-i+1]);
	}
	f[n+1]=0;
	fd(i,n,1) f[i]=f[i+1]+(s[i]=='0'?-1:1);
	mx=-1;
	fd(i,n,1)
	{
		if(f[i+1]>mx) mx=f[i+1],st=i;
	}
	fo(i,1,st) s[n+i]=s[i];
	fo(i,1,n) s[i]=s[i+st];
	fd(i,n,1)
	{
		if(s[i]=='0')
		{
			if(!(o&1)) o++;
			p[o]++;
			continue;
		}
		if(o&1)
		{
			p[o]--;
			if(!p[o]&&o>1) o--,p[o]++;
			else p[++o]=1,t[o]=0;
			continue;
		}
		t[o]=max(t[o],p[o]);p[o]++;p[o-1]--;
		if(p[o-1]==0&&o>2) t[o-2]=max(t[o-2],t[o]),p[o-2]+=p[o],o-=2;
	}
	fo(i,1,o) if(!(i&1)) ans=max(ans,t[i]);
	m=n+1;
	fo(i,1,o)
	{
		fo(j,1,p[i])
		{
			g[--m]=0;
			if(!(i&1)) g[--m]=1;
		}
	}
	kmp(n,g,f);
	m=f[n];
	while(n%(n-m)) m=f[m];
	printf("%d\n",ans+n-m);
	fo(i,0,n) t[i]=p[i]=0;
	cnt=0;o=0;m=0;ans=0;
}
int main()
{
	int T;scanf("%d",&T);
	while(T--) work();
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3
1
001001
0001111

output:

1
3
9

result:

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

Test #2:

score: 0
Accepted
time: 79ms
memory: 10132kb

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
19
19
19
21
22
20
20
20
22
21
21
22
23
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:

ok 262144 numbers

Test #3:

score: 0
Accepted
time: 172ms
memory: 10072kb

input:

524288
0000000000000000000
1000000000000000000
0100000000000000000
1100000000000000000
0010000000000000000
1010000000000000000
0110000000000000000
1110000000000000000
0001000000000000000
1001000000000000000
0101000000000000000
1101000000000000000
0011000000000000000
1011000000000000000
0111000000000...

output:

1
19
19
20
19
19
20
21
19
19
19
21
20
20
21
22
19
19
19
20
19
19
21
22
20
20
20
22
21
21
22
23
19
19
19
20
19
19
20
22
19
19
19
22
21
21
22
23
20
20
20
20
20
20
22
23
21
21
21
23
22
22
23
24
19
19
19
20
19
19
20
21
19
19
19
21
20
20
22
23
19
19
19
20
19
19
22
23
21
21
21
23
22
22
23
24
20
20
20
20
2...

result:

ok 524288 numbers

Test #4:

score: -100
Wrong Answer
time: 278ms
memory: 10084kb

input:

952358
0011101111
101010101101
101111111010100
0101011000110001100
0101111101110
010
100100000111011
011010110110011
1010111
1
1111101010
11111011001111110
0101101101011
001
1100111100
100011
10
10
0001
011100
1100010
111111101010010
01001111110011011
01100
1010
10101111
01000111100011111110
10101
0...

output:

11
12
18
20
14
3
21
16
7
1
10
18
13
3
11
4
2
2
4
4
8
18
19
6
2
8
24
5
1
2
5
25
1
14
1
15
20
3
7
24
12
10
20
21
23
1
25
18
22
5
1
6
18
12
1
3
5
12
13
12
21
1
5
12
21
8
1
8
18
4
1
12
13
6
3
3
16
6
8
1
1
17
1
1
1
6
6
4
4
10
7
5
4
5
24
6
11
4
8
15
3
9
9
19
5
16
11
5
6
9
17
1
25
14
6
1
4
20
1
1
20
14
14
...

result:

wrong answer 30th numbers differ - expected: '1', found: '2'