QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#411221#6299. Binary StringliuhengxiWA 69ms5692kbC++141.8kb2024-05-15 10:06:372024-05-15 10:06:37

Judging History

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

  • [2024-05-15 10:06:37]
  • 评测
  • 测评结果:WA
  • 用时:69ms
  • 内存:5692kb
  • [2024-05-15 10:06:37]
  • 提交

answer

// created:  May/15/2024 09:42:30
#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
#define F(i,l,r) for(int i=(l),i##_end=(r);i<i##_end;++i)
#define I128 //||is_same<T,__int128_t>::value||is_same<T,__uint128_t>::value
using namespace std;
template<typename T>enable_if_t<is_integral<T>::value I128,void> readmain(T &x)
{
	bool neg=false;int c=getchar();
	for(;!isdigit(c);c=getchar())if(c=='-')neg=true;
	for(x=0;isdigit(c);c=getchar())x=(T)(10*x+(c-'0'));
	if(neg)x=-x;
}
template<typename T>T& read(T &x){readmain(x);return x;}
template<typename T,typename ...Tr>void read(T &x,Tr&... r){readmain(x);read(r...);}
constexpr int N=1e6+5;
int n,a[N],p,z[N];
char s[N];
int flip()
{
	int one=0;
	F(i,0,n)one+=s[i];
	one-='0'*n;
	if(2*one<n)
	{
		F(i,0,n)s[i]^=1;
		reverse(s,s+n);
		one=n-one;
	}
	return one;
}
void rotate()
{
	static char t[N];
	int h=0,mh=0,mp=0;
	F(i,0,n)
	{
		h+=2*s[i]-(2*'0'+1);
		if(h<mh)mh=h,mp=i+1;
	}
	if(!mp&&s[n-1]=='1')
	{
		mp=n-1;
		while(mp&&s[mp-1]=='1')--mp;
	}
	memcpy(t,s+mp,n-mp);
	memcpy(t+n-mp,s,mp);
	memcpy(s,t,n);
}
int period()
{
	int m=0,r=0;
	z[0]=0;
	F(i,1,n)
	{
		z[i]=i<r?z[i-m]:0;
		while(s[z[i]]==s[i+z[i]])++z[i];
		if(i+z[i]>r)m=i,r=i+z[i];
		if(i+z[i]==n)return i;
	}
	return n;
}
int solve()
{
	int one=flip();
	if(one==n)return 1;
	if(one<n)rotate();
	int ans=0;
	p=0;
	for(int l=0,r=0;l<n;l=r+1)
	{
		while(s[l]==s[r+1])++r;
		if(s[l]=='0')F(i,l,r)ans=max(ans,i-a[--p]);
		else F(i,l,r)a[p++]=i;
	}
	memset(s,'0',n);
	F(i,0,p)s[a[i]]='1';
	ans>>=1;
	ans+=2*one==n?2:period();
	return ans;
}
int main()
{
	int tt;
	read(tt);
	while(tt--)
	{
		scanf("%s",s);
		n=(int)strlen(s);
		printf("%d\n",solve());
	}
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

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

input:

3
1
001001
0001111

output:

1
3
9

result:

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

Test #2:

score: -100
Wrong Answer
time: 69ms
memory: 5692kb

input:

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

output:

1
3
3
6
3
5
6
9
3
3
5
9
6
8
9
12
3
4
3
8
5
7
9
12
6
6
8
12
9
11
12
15
3
5
4
9
3
8
8
12
5
5
7
12
9
11
12
15
6
7
6
10
8
10
12
15
9
9
11
15
12
14
15
18
3
6
5
8
4
7
9
11
3
3
8
11
8
10
12
15
5
6
5
10
7
9
12
15
9
9
11
15
12
14
15
18
6
8
7
6
6
6
10
15
8
8
10
15
12
14
15
18
9
10
9
13
11
13
15
18
12
12
14
18...

result:

wrong answer 2nd numbers differ - expected: '18', found: '3'