QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#411223 | #6299. Binary String | liuhengxi | WA | 70ms | 5672kb | C++14 | 1.8kb | 2024-05-15 10:08:31 | 2024-05-15 10:08:31 |
Judging History
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?min(r-i,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;
}
詳細信息
Test #1:
score: 100
Accepted
time: 1ms
memory: 5636kb
input:
3 1 001001 0001111
output:
1 3 9
result:
ok 3 number(s): "1 3 9"
Test #2:
score: -100
Wrong Answer
time: 70ms
memory: 5672kb
input:
262144 000000000000000000 100000000000000000 010000000000000000 110000000000000000 001000000000000000 101000000000000000 011000000000000000 111000000000000000 000100000000000000 100100000000000000 010100000000000000 110100000000000000 001100000000000000 101100000000000000 011100000000000000 11110000...
output:
1 18 18 19 18 18 19 20 18 18 18 20 19 19 20 21 18 18 18 19 18 18 20 21 19 19 19 21 20 20 21 22 18 18 18 19 18 18 19 21 18 18 18 21 20 20 21 22 19 19 19 19 19 19 21 22 20 20 20 22 21 21 22 23 18 18 18 19 18 18 19 20 18 18 18 20 19 19 21 22 18 18 18 19 18 18 21 22 20 20 20 22 21 21 22 23 19 19 19 19 1...
result:
wrong answer 770th numbers differ - expected: '19', found: '11'