QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#360078 | #6299. Binary String | jinqihao2023 | WA | 1107ms | 20516kb | C++14 | 2.3kb | 2024-03-21 11:17:55 | 2024-03-21 11:17:55 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=1e7+5;
int T,n,a[N],b[N],c[N];
string s;
int prt[N],sum[N],r[N],ans[N],ans1[N],ansx[N];
int gd(int x,int y){return (y-x+c[0])%c[0];}
int gf(int x){if(x==prt[x])return x;return prt[x]=gf(prt[x]);}
int nxt[N];
int main()
{
// freopen("1.in","r",stdin);
scanf("%d",&T);
int cntt=0;
while(T--)
{
cin>>s,n=s.length(),s=' '+s;
for(int i=1;i<=n;i++)a[i]=s[i]-'0';
int cnt0=0,cnt1=0;
for(int i=1;i<=n;i++)if(a[i])cnt1++;else cnt0++;
if(cnt1>cnt0){for(int i=1;i<=n;i++)a[i]^=1;reverse(a+1,a+n+1);}
b[0]=c[0]=0;
for(int i=1;i<=n;i++)if(a[i]==0)
{
for(int j=i+1;j!=i;j=j%n+1)b[++b[0]]=a[j];
b[++b[0]]=a[i];
}
int now=0;
for(int i=1;i<=n;i++)
{
if(b[i])now++;
else c[++c[0]]=now,now=0;
}
for(int i=1;i<=c[0];i++)prt[i]=i,sum[i]=c[i];
// for(int i=1;i<=c[0];i++)printf("%d ",c[i]);printf("\n");
vector<int>temp;
for(int i=1;i<=c[0];i++)if(c[i])temp.push_back(i);
int len=temp.size();
queue<pair<int,int> >q;
for(int i=0;i<len;i++)
{
int j=(i+1)%len;r[temp[i]]=(temp[j]-2+c[0])%c[0]+1;
// cout<<temp[i]<<" "<<temp[j]<<" "<<c[temp[i]]<<" "<<gd(temp[i],temp[j])<<" "<<r[temp[i]]<<endl;
if(gd(temp[i],temp[j])<c[temp[i]])q.push({temp[i],temp[j]});
}
while(!q.empty())
{
int x=q.front().first,y=q.front().second;q.pop();
x=gf(x),y=gf(y);if(x==y)continue;
prt[y]=x,sum[x]+=sum[y],r[x]=r[y];
int j=gf(r[x]%c[0]+1);
// cout<<x<<" "<<y<<" "<<j<<endl;
if(j!=x && gd(x,j)<sum[x])q.push({x,j});
}
for(int i=1;i<=c[0];i++)ansx[i]=0;
int ans=0;
for(int i=0;i<len;i++)if(prt[temp[i]]==temp[i])
{
// cout<<temp[i]<<" "<<sum[temp[i]]<<endl;
ans=max(ans,sum[temp[i]]-1);
for(int j=temp[i];j<=temp[i]+sum[temp[i]]-1;j++)ansx[(j-1)%c[0]+1]=1;
}
// cout<<ans<<endl;
// for(int i=1;i<=c[0];i++)printf("%d ",ansx[i]);printf("\n");
int m=0;
for(int i=1;i<=c[0];i++)
{
ans1[++m]=0;
if(ansx[i])ans1[++m]=1;
}
// for(int i=1;i<=m;i++)printf("%d ",ans1[i]);printf("\n");
for(int i=1;i<=n;i++)nxt[i]=0;
for(int i=2;i<=n;i++)
{
nxt[i]=nxt[i-1];
while(nxt[i] && ans1[nxt[i]+1]!=ans1[i])nxt[i]=nxt[nxt[i]];
if(ans1[nxt[i]+1]==ans1[i])nxt[i]++;
}
int now1=nxt[n];
while(n%(n-now1)!=0)now1=nxt[now1];
ans+=n-now1;
printf("%d\n",ans);
}
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 3ms
memory: 20228kb
input:
3 1 001001 0001111
output:
1 3 9
result:
ok 3 number(s): "1 3 9"
Test #2:
score: 0
Accepted
time: 408ms
memory: 20516kb
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:
ok 262144 numbers
Test #3:
score: 0
Accepted
time: 772ms
memory: 20224kb
input:
524288 0000000000000000000 1000000000000000000 0100000000000000000 1100000000000000000 0010000000000000000 1010000000000000000 0110000000000000000 1110000000000000000 0001000000000000000 1001000000000000000 0101000000000000000 1101000000000000000 0011000000000000000 1011000000000000000 0111000000000...
output:
1 19 19 20 19 19 20 21 19 19 19 21 20 20 21 22 19 19 19 20 19 19 21 22 20 20 20 22 21 21 22 23 19 19 19 20 19 19 20 22 19 19 19 22 21 21 22 23 20 20 20 20 20 20 22 23 21 21 21 23 22 22 23 24 19 19 19 20 19 19 20 21 19 19 19 21 20 20 22 23 19 19 19 20 19 19 22 23 21 21 21 23 22 22 23 24 20 20 20 20 2...
result:
ok 524288 numbers
Test #4:
score: -100
Wrong Answer
time: 1107ms
memory: 20476kb
input:
952358 0011101111 101010101101 101111111010100 0101011000110001100 0101111101110 010 100100000111011 011010110110011 1010111 1 1111101010 11111011001111110 0101101101011 001 1100111100 100011 10 10 0001 011100 1100010 111111101010010 01001111110011011 01100 1010 10101111 01000111100011111110 10101 0...
output:
11 12 18 20 14 3 21 16 7 1 10 18 13 3 11 4 1 1 4 4 8 18 19 6 2 8 24 5 1 1 5 25 1 14 1 15 20 3 7 24 12 10 20 21 23 1 22 18 22 5 1 6 18 12 1 4 5 12 13 12 21 1 5 12 21 8 1 8 18 4 1 12 13 6 3 3 16 6 8 1 1 17 1 1 1 6 6 4 4 10 7 5 4 5 24 6 11 4 8 15 3 9 9 19 5 16 11 5 6 9 17 1 25 14 6 1 4 20 1 4 20 14 14 ...
result:
wrong answer 17th numbers differ - expected: '2', found: '1'