QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#390152 | #6299. Binary String | NATURAL6 | TL | 38ms | 101592kb | C++14 | 2.1kb | 2024-04-15 08:54:40 | 2024-04-15 08:54:41 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
inline int qread()
{
int a=0,f=1;char ch=getchar();
while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
while(isdigit(ch)){(a*=10)+=(ch^48);ch=getchar();}
return a*f;
}
int T,n,ans,cnt0,cnt1,que[10000010],ed,mn,pos,A[10000010],kmp[10000010];
char ch[10000010],S[10000010];
signed main()
{
T=qread();
while(T--)
{
scanf("%s",ch+1);ans=0;
cnt0=cnt1=0;n=strlen(ch+1);
for(int i=1;i<=n;++i)
{
if(ch[i]=='0')++cnt0;
else ++cnt1;
}
if(cnt0<cnt1)
{
for(int i=1;i<=n;++i)ch[i]^=1;
reverse(ch+1,ch+1+n);swap(cnt0,cnt1);
}
for(int i=1;i<=n;++i)
{
if(ch[i]=='0')
{
for(int j=1;j<=n;++j)S[j]=ch[(i+j-2)%n+1];
swap(ch,S);break;
}
}
ed=mn=pos=0;
for(int l=1,r;l<=n;l=r)
{
r=l+1;
while(r<=n&&ch[r]=='1')++r;
que[++ed]=r-l-1;
}
for(int i=1,sum=0;i<=ed;++i)
{
sum+=que[i]-1;
if(sum<mn)mn=sum,pos=i;
}
for(int i=1;i<=ed;++i)A[i]=que[(i+pos-1)%ed+1];
swap(A,que);
// for(int i=1;i<=ed;++i)printf("%d ",que[i]);puts("");
for(int i=1,sum,cnt,j;i<=ed;++i)
{
if(que[i]<=1)continue;
j=i,sum=0,cnt=0;
while(1)
{
sum+=que[j]-1;
if(sum<=0)break;
++j;++cnt;
}
ans=max(ans,cnt);
for(int k=i;k<=j;++k)que[k]=1;
}
n=0;
for(int i=1;i<=ed;++i)
{
ch[++n]='0';
if(que[i]==1)ch[++n]='1';
}
for(int i=2,j;i<=n;++i)
{
j=kmp[i-1];
while(j&&ch[i]^ch[j+1])j=kmp[j];
if(ch[i]==ch[j+1])kmp[i]=j+1;
}
if(n%(n-kmp[n])==0)ans+=(n-kmp[n]);
else ans+=n;
printf("%d\n",ans);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 38ms
memory: 101592kb
input:
3 1 001001 0001111
output:
1 3 9
result:
ok 3 number(s): "1 3 9"
Test #2:
score: -100
Time Limit Exceeded
input:
262144 000000000000000000 100000000000000000 010000000000000000 110000000000000000 001000000000000000 101000000000000000 011000000000000000 111000000000000000 000100000000000000 100100000000000000 010100000000000000 110100000000000000 001100000000000000 101100000000000000 011100000000000000 11110000...