QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#390805#6299. Binary StringOccDreamerWA 0ms3804kbC++143.2kb2024-04-15 22:14:342024-04-15 22:14:34

Judging History

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

  • [2024-04-15 22:14:34]
  • 评测
  • 测评结果:WA
  • 用时:0ms
  • 内存:3804kb
  • [2024-04-15 22:14:34]
  • 提交

answer

//code by Emissary
#include<bits/stdc++.h>

#define vc vector
#define db double
#define fi first
#define se second
#define ll long long
#define mk make_pair
#define pb push_back
#define RI register int
#define PI pair<int,int>
#define ull unsigned long long
#define err cerr << "   -_-   " << endl
#define debug cerr << " ------------------- " << endl

#define input(x) freopen(#x".in","r",stdin)
#define output(x) freopen(#x".out","w",stdout)

#define NO puts("No")
#define YES puts("Yes")

//#define OccDreamer
//#define int long long

using namespace std;

namespace IO{
	inline int read(){
		int X=0, W=0; char ch=getchar();
		while(!isdigit(ch)) W|=ch=='-', ch=getchar();
		while(isdigit(ch)) X=(X<<1)+(X<<3)+(ch^48), ch=getchar();
		return W?-X:X;
	}
	inline void write(int x){
		if(x<0) x=-x, putchar('-');
		if(x>9) write(x/10);
		putchar(x%10+'0');
	}
	inline void sprint(int x){write(x), putchar(32);}
	inline void eprint(int x){write(x), putchar(10);}
}using namespace IO;

const int MAXN = 1e7 + 7;

int topf;
int n, tot;
int nxt[MAXN], stk[MAXN];

bool mark[MAXN<<1];

char s[MAXN<<1];

struct range{
	int l, r, num, nowtim;
}Q[MAXN];

inline void Rotate(int len){
	for(int i=n+1;i<n+len;++i) s[i]=s[i-n];
	for(int i=1;i<=n;++i) s[i]=s[i+len-1];
	return ;
}

inline int Nxt(int x){return x==n?1:x+1;}

inline void solve(){
	scanf("%s",s+1); n=strlen(s+1);
	int fir=0;
	for(int i=1;i<=n;++i) 
		if(s[i]!=s[1]){fir=i; break;}
	if(!fir) return eprint(1);
	Rotate(fir); int sum0=0, sum1=0;
	int nowlen=0, num=s[1]-'0';
	for(int i=1;i<=n;++i){
		if(s[i]-'0'==num) ++nowlen;	
		else{
			if(nowlen>1) sum0+=(num==0)*(nowlen-1), sum1+=(num==1)*(nowlen-1);
			num^=1; nowlen=1;
		}	
	}
	if(nowlen>1) sum0+=(num==0)*(nowlen-1), sum1+=(num==1)*(nowlen-1);
	if(sum1<sum0){
		for(int i=1;i<=n;++i) s[i]^=1;
		reverse(s+1,s+1+n);
	}
	sum0=0, sum1=0;
	nowlen=0, num=s[1]-'0'; int pos=1, lim=0;
	for(int i=1;i<=n;++i){
		if(s[i]-'0'==num) ++nowlen;	
		else{
			if(nowlen>1){
				sum0+=(num==0)*(nowlen-1), sum1+=(num==1)*(nowlen-1);
				if(sum1-sum0<lim) lim=sum1-sum0, pos=i;	
			}
			num^=1; nowlen=1;
		}	
	}
	if(nowlen>1) sum0+=(num==0)*(nowlen-1), sum1+=(num==1)*(nowlen-1);
	Rotate(pos); tot=0;
	nowlen=0, num=s[1]-'0';
	/*
	1
	1000001010101010111110011100001111110000101
	*/
	int maxn=0;
	for(int i=1;i<=n;++i){
		if(s[i]-'0'==num){
			if(s[i]=='0') maxn=max(maxn,(i-stk[topf])/2), --topf;
			else stk[++topf]=i;
		}
		else num=s[i]-'0';
	}
	int ans=maxn;
	for(int i=1;i<=n;++i) mark[i]=0; pos=1; mark[0]=1;
	for(int i=1;i<=topf;++i){
		int j=stk[i]+maxn;
		mark[j%n==0?n:j%n]=1, pos=j%n==0?n:j%n;
	}
	int las=pos;
	for(int i=Nxt(pos);i!=pos;i=Nxt(i)){
		if(mark[i]) continue;
		mark[i]=mark[las]^1; las=i;	
	}
	for(int i=1;i<=n;++i) s[i]='0'+(int(mark[i]));
	pos=1;
	for(int i=1;i<=n;++i) 
		if(s[i]!=s[1]) {pos=i; break;}
	Rotate(pos); int now=0;
	for(int i=1;i<=n;++i) s[i+n]=s[i];
	for(int i=2;i<=n<<1;++i){
		while(now && s[now+1]!=s[i]) now=nxt[now];
		if(s[now+1]==s[i]) ++now;
		nxt[i]=now;	
	}
	eprint(ans+((n<<1)-nxt[n<<1]));
}

signed main(){
	int t=read();
	while(t--) solve();
	return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 0ms
memory: 3804kb

input:

3
1
001001
0001111

output:

1
6
9

result:

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