QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#96599 | #6299. Binary String | william555 | WA | 206ms | 11760kb | C++14 | 1.4kb | 2023-04-14 16:47:23 | 2023-04-14 16:47:27 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=1e7+5;
int n,tp,fail[N];
pair<int,int> st[N];
char a[N],b[N];
void trans(int p){
static char b[N];
for(int i=0,j=p;i<n;i++,j=(j+1)%n)b[i]=a[j];
for(int i=0;i<n;i++)a[i]=b[i];
}
void solve(){
scanf("%s",a);
n=strlen(a);
int c0=0,c1=0;
for(int i=0;i<n;i++)(a[i]=='0'?c0:c1)++;
if(c0>c1){
for(int i=0;i<n;i++)a[i]^=1;
reverse(a,a+n);
swap(c0,c1);
}
if(c1==n){puts("1");return;}
int p=0;
for(int i=0;i+1<n;i++)if(a[i]=='0'&&a[i+1]=='1')p=i+1;
trans(p);
int now=0,mn=0;p=0;
for(int i=0,j;i<n;i=j+1){
for(j=i;j+1<n&&a[j+1]==a[i];j++);
if(i<j){
if(a[i]=='0')now-=j-i;
else now+=j-i;
if(now<mn)mn=now,p=(j+1)%n;
}
}
trans(p);
int mx=0;tp=0;
for(int i=0,j;i<n;i=j+1){
for(j=i;j+1<n&&a[j+1]==a[i];j++);
if(i==j)continue;
if(a[i]=='1'){
st[++tp]=make_pair(i,j);
continue;
}
int l=j-i;
while(l){
int x=min(l,st[tp].second-st[tp].first);
l-=x,st[tp].second-=x;
mx=max(mx,j-st[tp].second-1>>1);
if(st[tp].second<=st[tp].first)tp--;
}
}
for(int i=1;i<=n;i++)b[i]=0;
for(int i=1;i<=tp;i++)
for(int j=st[i].first+1;j<=st[i].second+1;j++)
b[j]=1;
for(int i=2,j=0;i<=n;i++){
while(j&&b[j+1]!=b[i])j=fail[j];
if(b[j+1]==b[i])j++;
fail[i]=j;
}
int k=n-fail[n];
if(n%k)k=n;
printf("%d\n",mx+k);
}
int main(){
int T;scanf("%d",&T);
while(T--)solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 4ms
memory: 11680kb
input:
3 1 001001 0001111
output:
1 3 9
result:
ok 3 number(s): "1 3 9"
Test #2:
score: -100
Wrong Answer
time: 206ms
memory: 11760kb
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 512th numbers differ - expected: '10', found: '9'