QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#99187#6299. Binary StringAFewSunsWA 161ms3452kbC++142.2kb2023-04-21 15:59:182023-04-21 15:59:20

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 15:59:20]
  • 评测
  • 测评结果:WA
  • 用时:161ms
  • 内存:3452kb
  • [2023-04-21 15:59:18]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
namespace my_std{
	#define ll long long
	#define bl bool
	ll my_pow(ll a,ll b,ll mod){
		ll res=1;
		if(!b) return 1;
		while(b){
			if(b&1) res=(res*a)%mod;
			a=(a*a)%mod;
			b>>=1;
		}
		return res;
	}
	ll qpow(ll a,ll b){
		ll res=1;
		if(!b) return 1;
		while(b){
			if(b&1) res*=a;
			a*=a;
			b>>=1;
		}
		return res;
	}
	#define db double
	#define pf printf
	#define pc putchar
	#define fr(i,x,y) for(register ll i=(x);i<=(y);i++)
	#define pfr(i,x,y) for(register ll i=(x);i>=(y);i--)
	#define go(u) for(ll i=head[u];i;i=e[i].nxt)
	#define enter pc('\n')
	#define space pc(' ')
	#define fir first
	#define sec second
	#define MP make_pair
	#define il inline
	#define inf 8e18
	#define random(x) rand()*rand()%(x)
	#define inv(a,mod) my_pow((a),(mod-2),(mod))
	il ll read(){
		ll sum=0,f=1;
		char ch=0;
		while(!isdigit(ch)){
			if(ch=='-') f=-1;
			ch=getchar();
		}
		while(isdigit(ch)){
			sum=sum*10+(ch^48);
			ch=getchar();
		}
		return sum*f;
	}
	il void write(ll x){
		if(x<0){
			x=-x;
			pc('-');
		}
		if(x>9) write(x/10);
		pc(x%10+'0');
	}
	il void writeln(ll x){
		write(x);
		enter;
	}
	il void writesp(ll x){
		write(x);
		space;
	}
}
using namespace my_std;
#define mod 998244353
ll T,n,len[10000010],nxt[10000010];
char s[10000010],t[10000010];
int main(){
	T=read();
	while(T--){
		scanf("%s",s+1);
		n=strlen(s+1);
		ll cnt1=0;
		fr(i,1,n) cnt1+=(s[i]=='1');
		if(cnt1*2>n){
			reverse(s+1,s+n+1);
			fr(i,1,n) s[i]^=1;
		}
		ll sum=0,minn=inf,pos=0;
		fr(i,1,n){
			if(s[i]=='1') sum++;
			else sum--;
			if(sum<minn){
				pos=i;
				minn=sum;
			}
		}
		pos=pos%n+1;
		ll nlen=0,nmax=0,maxx=0,cnt0=0;
		fr(i,1,n-1){
			pos=pos%n+1;
			if(s[pos]=='0'){
				cnt0++;
				continue;
			}
			nmax++;
			nlen+=1-cnt0;
			cnt0=0;
			if(nlen<0) nmax=nlen=0;
			maxx=max(maxx,nmax);
			len[pos]=nlen;
		}
		fr(i,1,n) t[i]='0';
		fr(i,1,n) if(s[i]=='1') t[(i-(maxx-len[i])+2*n-1)%n+1]='1';
		fr(i,2,n){
			ll tmp=nxt[i-1];
			while(tmp&&t[i]!=t[tmp+1]) tmp=nxt[tmp];
			if(t[i]==t[tmp+1]) nxt[i]=tmp+1;
		}
		if(n%(n-nxt[n])) nxt[n]=0;
		writeln((maxx+n-nxt[n])%mod);
		fr(i,1,n) len[i]=nxt[i]=0;
	}
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 2ms
memory: 3452kb

input:

3
1
001001
0001111

output:

1
3
9

result:

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

Test #2:

score: -100
Wrong Answer
time: 161ms
memory: 3404kb

input:

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

output:

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

result:

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