QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#390753#6299. Binary StringwtcWA 95ms8040kbC++141.4kb2024-04-15 21:08:222024-04-15 21:08:23

Judging History

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

  • [2024-04-15 21:08:23]
  • 评测
  • 测评结果:WA
  • 用时:95ms
  • 内存:8040kb
  • [2024-04-15 21:08:22]
  • 提交

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];
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]);
	}
	fo(i,1,n) f[i]=f[i-1]+(s[i]=='0'?-1:1);
	mx=-1;
	fo(i,1,n)
	{
		if(f[i-1]>mx) mx=f[i-1],st=i;
	}
	fu(i,1,st) s[n+i]=s[i];
	fo(i,1,n) s[i]=s[i+st-1];
	fo(i,1,n)
	{
		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]++;t[o-1]--;
		if(t[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]);
	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();
}

詳細信息

Test #1:

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

input:

3
1
001001
0001111

output:

1
3
9

result:

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

Test #2:

score: -100
Wrong Answer
time: 95ms
memory: 8040kb

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

result:

wrong answer 12th numbers differ - expected: '20', found: '19'