QOJ.ac

QOJ

ID提交记录ID题目HackerOwner结果提交时间测评时间
#701#225542#7399. Balanced Binary Stringucup-team1005CrysflyFailed.2024-06-22 15:26:532024-06-22 15:26:54

详细

Extra Test:

Invalid Input

input:

1
1021
?????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????????...

output:


result:

FAIL Token parameter [name=s] equals to "1021", doesn't correspond to pattern "[01?]{1,1024}" (stdin, line 2)

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#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;
}
/*

*/