QOJ.ac

QOJ

IDSubmission IDProblemHackerOwnerResultSubmit timeJudge time
#702#225542#7399. Balanced Binary Stringucup-team1005CrysflyFailed.2024-06-22 15:27:352024-06-22 15:27:36

Details

Extra Test:

Accepted
time: 437ms
memory: 3628kb

input:

1
??????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????...

output:

1041422

result:

ok "1041422"

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#225542#7399. Balanced Binary StringCrysflyAC ✓294ms3544kbC++171.6kb2023-10-24 19:31:412023-10-24 19:31:42

answer

// what is matter? never mind. 
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
//#pragma GCC target("sse,sse2,sse3,sse4,popcnt,abm,mmx,avx,avx2") 
#include<bits/stdc++.h>
#define For(i,a,b) for(int i=(a);i<=(b);++i)
#define Rep(i,a,b) for(int i=(a);i>=(b);--i)
#define ll long long
#define ull unsigned long long
//#define int long long
using namespace std;
inline int read()
{
    char c=getchar();int x=0;bool f=0;
    for(;!isdigit(c);c=getchar())f^=!(c^45);
    for(;isdigit(c);c=getchar())x=(x<<1)+(x<<3)+(c^48);
    if(f)x=-x;return x;
}

#define fi first
#define se second
#define pb push_back
#define mkp make_pair
typedef pair<int,int>pii;
typedef vector<int>vi;
 
#define maxn 200005
#define inf 0x3f3f3f3f

int n;
string str;

int nxt[maxn];
int period(string s){
	int n=s.size(); nxt[1]=0;
	int j=0;
	For(i,1,n-1){
		while(j&&s[j]!=s[i])j=nxt[j];
		if(s[j]==s[i])++j; nxt[i+1]=j;
	}
	int d=n-nxt[n];
	if(n%d==0)return d;
	else return n;
}

bool chk(string s){
	For(i,0,n-1) if(str[i]!='?' && s[i]!=str[i]) return 0;
	return 1;
}

void work()
{
	cin>>str;
	n=str.size();
	int res=0;
	For(x,0,n){
		int y=n-x;
		int nx=0,ny=0;
		string s="";
		auto up=[&](){
			++ny,s+='1';
		};
		auto ri=[&](){
			++nx,s+='0';
		};
		For(_,1,n){
			if(nx*y==ny*x){
				if(!y)ri();
				else up();
			}
			else if(nx*y<ny*x)ri();
			else up();
		}
		assert(nx==x);
		assert(ny==y);
		int t=period(s);
		For(i,0,t-1){
			if(chk(s))++res;
			rotate(s.begin(),s.begin()+1,s.end());
		}
	}
	cout<<res<<"\n";
}

signed main()
{
	int T=read();
	while(T--)work();
    return 0;
}
/*

*/