QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#117465#6299. Binary StringKostlinWA 381ms18136kbC++144.7kb2023-07-01 11:41:132023-07-01 11:41:14

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:41:14]
  • 评测
  • 测评结果:WA
  • 用时:381ms
  • 内存:18136kb
  • [2023-07-01 11:41:13]
  • 提交

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];
bool vis[N],ok[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");
            for(int i=1;i<=n;++i) vis[i]=0,ok[i]=0;
            a[0]=b[0]=c[0]=cnt=0;
            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]);
            fl=0;
            for(int i=1;i<=cnt;++i) {
                if(vis[i]||S[i]<L[i]) continue;
                ok[i]=1; fl=1; vis[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;
                }
                // cerr<<i<<" "<<now<<endl;
                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(vis[i]) continue;
                ok[i]=1; fl=1; vis[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;
                }
                // cerr<<i<<" "<<now<<endl;
                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]) {
                    // printf("%d %d===\n",fuck[i],fuc[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;
        }
        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,fuck[i]=fuc[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: 1ms
memory: 16080kb

input:

3
1
001001
0001111

output:

1
3
9

result:

ok 3 number(s): "1 3 9"

Test #2:

score: 0
Accepted
time: 107ms
memory: 18136kb

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: 223ms
memory: 17996kb

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: 0
Accepted
time: 381ms
memory: 15944kb

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

ok 952358 numbers

Test #5:

score: 0
Accepted
time: 335ms
memory: 15900kb

input:

645561
001000111110000
01001111000
01011010
110000110111111111
0110100010000100
0
010011
010011111
00000111100001101110101100001
10111111000
100
1101100000001010110110101
1001111101
000100
101
1110101100011111101001111
000111100111100
1111001101011000000
100101001001001010111011011111
10111001100111...

output:

19
14
4
21
18
1
4
11
37
13
3
34
11
6
3
29
21
27
38
24
13
22
26
39
26
31
10
22
1
17
34
40
18
9
29
31
11
3
28
15
20
27
29
16
2
23
18
31
5
14
33
18
1
1
18
7
36
17
34
1
18
6
16
20
19
3
18
36
21
23
21
4
30
12
4
35
16
18
35
2
25
20
31
17
26
24
3
24
6
26
25
17
13
7
24
27
25
18
22
10
20
4
6
23
17
30
16
22
3...

result:

ok 645561 numbers

Test #6:

score: -100
Wrong Answer
time: 316ms
memory: 15976kb

input:

488486
01101101
011000000100011010101011010
0100101110001011
00111110011110011010001101010000
11010010000010011111011010000010001
01001111001000010110
10110010011010100101010101111
10100000000001
1000010010
0
1111
10001010011011001111100101010
01010111100001011111011110101110101
11100
10101010001101...

output:

8
36
4
17
43
24
33
16
10
1
1
39
38
6
40
30
51
17
41
1
1
12
34
49
44
38
18
47
39
14
35
32
14
36
31
37
23
42
3
10
18
15
21
14
33
11
17
5
18
45
39
31
38
52
27
22
38
15
16
26
30
3
2
3
29
1
41
40
14
17
24
50
21
34
11
31
9
31
18
32
21
43
1
42
39
10
36
27
27
29
15
13
1
24
46
22
21
13
29
13
14
45
47
45
43
1...

result:

wrong answer 6007th numbers differ - expected: '18', found: '50'