QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#117457#6299. Binary StringKostlinTL 4ms17932kbC++143.9kb2023-07-01 11:26:422023-07-01 11:26:45

Judging History

你现在查看的是最新测评结果

  • [2023-08-10 23:21:45]
  • System Update: QOJ starts to keep a history of the judgings of all the submissions.
  • [2023-07-01 11:26:45]
  • 评测
  • 测评结果:TL
  • 用时:4ms
  • 内存:17932kb
  • [2023-07-01 11:26:42]
  • 提交

answer

#include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#include <assert.h>
using namespace std;
typedef long long ll;
#define fi first
#define sc second
#define mkp make_pair
#define pii pair<int,int>
const int N=1e7+5;
inline int read() {
    int x=0,flag=0;char ch=getchar();
    while(ch<'0'||ch>'9') {flag|=(ch=='-');ch=getchar();}
    while('0'<=ch&&ch<='9') {x=(x<<3)+(x<<1)+ch-'0';ch=getchar();}
    return flag?-x:x;
}
inline int mx(int x,int y) {return x>y?x:y;}
inline int mn(int x,int y) {return x<y?x:y;}
inline void swp(int &x,int &y) {x^=y^=x^=y;}
inline int as(int x) {return x>=0?x:-x;}

int n,a[N],b[N],c[N],S[N],P[N],L[N],cnt,nqwq[N],ans1,ans2,fuck[N],fuc[N]; char s[N],ok[N];
bool vis[N];
inline int nxt(int x) {
    if(x==b[0]) return 1;
    else return x+1;
}
inline int Nxt(int x) {
    if(x==cnt) return 1;
    else return x+1;
}
int main() {
    int TT=read();
    while(TT--) {ans1=1;ans2=0;
        scanf("%s",s+1); n=strlen(s+1);
        if(n==1) {printf("1\n");continue;}
        int t0=0,t1=0;
        for(int i=1;i<=n;++i) t0+=(s[i]=='0'),t1+=(s[i]=='1');
        if(!t0||!t1) {printf("1\n");continue;}
        if(t0<t1) {
            swp(t0,t1);
            reverse(s+1,s+n+1);
            for(int i=1;i<=n;++i) s[i]=(s[i]=='0'?'1':'0');
        }
        int mpos=0;
        for(int i=1;i<=n;++i) 
            if(s[i]=='0'&&s[(i>1?i-1:n)]=='1') {
                mpos=i;break;
            }
        for(int i=mpos;i<=n;++i) a[++a[0]]=s[i]-'0';
        for(int i=1;i<mpos;++i) a[++a[0]]=s[i]-'0';
        for(int i=1,pos;i<=n;i=pos) {
            pos=i+1;
            while(pos<=n&&a[pos]==1) ++pos;
            b[++b[0]]=pos-i-1;
        }
        bool fl=0;
        for(int i=1;i<=b[0];++i) {
            if(!b[i]||b[i==1?b[0]:i-1]) continue;
            fl=1;
            int now=i,sum=0;
            while(b[now]) sum+=b[now],now=nxt(now);
            ++cnt; S[cnt]=sum-1; P[cnt]=i;
        }
        if(!fl) {printf("2\n");continue;}
        if(cnt>1) {
            P[cnt+1]=P[1];
            for(int i=1;i<=cnt;++i) {
                int pos=P[i];L[i]=0;
                while(pos!=P[i+1]) ++L[i],pos=nxt(pos);
            }
            // for(int i=1;i<=cnt;++i) printf("%d %d\n",S[i],L[i]);
            for(int i=1;i<=cnt;++i) {
                if(vis[i]||S[i==1?cnt:i-1]>=L[i==1?cnt:i-1]) continue;
                ok[i]=1;
                int sum=S[i],Sum=S[i]+1,now=i,op=0;
                while(sum>=L[now]) {++op;
                    sum-=L[now]; sum+=S[Nxt(now)]+1; Sum+=S[Nxt(now)]+1;
                    vis[now=Nxt(now)]=1;
                }
                fuck[Nxt(now)]=L[now]-sum,fuc[i]=Sum;
                now=P[i];
                while(b[now]==1) {
                    --Sum;
                    now=nxt(now);
                }
                ans1=mx(ans1,Sum);
            }
            for(int i=1;i<=cnt;++i)
                if(ok[i]) {
                    for(int j=1;j<=fuck[i];++j) c[++c[0]]=0;
                    for(int j=1;j<=fuc[i];++j) c[++c[0]]=1,c[++c[0]]=0;
                    --c[0];
                }
        } else {
            int now=P[1];ans1=S[1]+1;
            while(b[now]==1) --ans1,now=nxt(now);
            ans1=mx(ans1,1);
            for(int i=1;i<=S[1]+1;++i) c[++c[0]]=1,c[++c[0]]=0;
            for(int i=1;i<=n-(S[1]+1)*2;++i) c[++c[0]]=0;
        }
        // assert(c[0]==n);
        // cerr<<c[0]<<endl;
        // for(int i=1;i<=c[0];++i) printf("%d",c[i]);
        // printf("\n");
        int j=0; nqwq[1]=0;
        for(int i=2;i<=n;++i) {
            while(j&&c[j+1]!=c[i]) j=nqwq[j];
            if(c[j+1]==c[i]) ++j;
            nqwq[i]=j;
        }
        if(n%(n-nqwq[n])==0) ans2=n-nqwq[n];
        else ans2=n;
        printf("%d\n",ans1+ans2-1);
        for(int i=1;i<=n;++i) vis[i]=0,ok[i]=0;
        a[0]=b[0]=c[0]=cnt=0;
    }
    return 0;
}

Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 100
Accepted
time: 4ms
memory: 17932kb

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
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: