QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#154743 | #6299. Binary String | 275307894a | TL | 0ms | 10192kb | C++14 | 1.8kb | 2023-08-31 21:41:46 | 2023-08-31 21:41:46 |
Judging History
answer
#include<bits/stdc++.h>
#define Gc() getchar()
#define Me(x,y) memset(x,y,sizeof(x))
#define Mc(x,y) memcpy(x,y,sizeof(x))
#define d(x,y) ((m)*(x-1)+(y))
#define R(n) (rnd()%(n)+1)
#define Pc(x) putchar(x)
#define LB lower_bound
#define UB upper_bound
#define fi first
#define se second
using namespace std;using ll=long long;using db=double;using lb=long db;using ui=unsigned;using ull=unsigned long long;using pii=pair<int,int>;using LL=__int128;
const int N=1e6+5,M=N*40+5,K=(1<<25)+5,mod=998244353,Mod=mod-1;const db eps=1e-9;const ll INF=1e18+7;mt19937 rnd(time(0));
int n,H,T,st[N],op[N],Nt[N];char s[N],c[N];
void Solve(){
int i,j;scanf("%s",s+1);n=strlen(s+1);for(i=1;i<=n;i++) s[i]-='0';
int C[2]={0,0};
for(i=1;i<=n;i++) C[s[i]]++;
if(C[0]<C[1]){
for(i=1;i<=n;i++) s[i]^=1;
swap(C[0],C[1]);
}
if(!C[1]){puts("1");return;}
for(i=1;i<=n;i++) if(!s[i]) {rotate(s+1,s+i,s+n+1);break;}
st[T=1]=0;op[1]=0;
for(i=1;i<=n;i++){
if(!s[i]){
if(op[T]==0) st[T]++;
else st[++T]=1,op[T]=0;
}else{
if(!op[T]&&op[T-1]&&st[T-1]>st[T])st[T-1]++;
else {
if(op[T]==1) st[T]++;
else st[++T]=1,op[T]=1;
}
}
}
if(!op[T]) st[1]+=st[T],T--;
H=1;
while(st[T]>st[H]) st[T]+=st[H+1],H+=2;
// for(i=H;i<=T;i++) cerr<<st[i]<<' ';
int ans=0;for(i=H+1;i<=T;i+=2) ans=max(ans,st[i]-1);
int m=0;
for(i=H;i<T;i+=2){
for(j=1;j<=st[i]-st[i^H?i-1:T];j++) c[++m]='0';
for(j=1;j<=st[i+1];j++) c[++m]='0',c[++m]='1';
}
Nt[1]=0;for(i=2;i<=m;i++){
Nt[i]=Nt[i-1];while(Nt[i]&&c[i]^c[Nt[i]+1]) Nt[i]=Nt[Nt[i]];
if(c[i]==c[Nt[i]+1]) Nt[i]++;
}
// cerr<<ans<<'\n';
// cerr<<Nt[n]<<'\n';
if(n%(n-Nt[n])==0) ans+=n-Nt[n];else ans+=n;
printf("%d\n",ans);
}
int main(){
int t;
scanf("%d",&t);
// t=1;
while(t--) Solve();
cerr<<clock()*1.0/CLOCKS_PER_SEC<<'\n';
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 10192kb
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...
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 21 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...