QOJ.ac

QOJ

ID题目提交者结果用时内存语言文件大小提交时间测评时间
#390162#6299. Binary StringNATURAL6WA 94ms5880kbC++142.2kb2024-04-15 09:07:122024-04-15 09:07:13

Judging History

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

  • [2024-04-15 09:07:13]
  • 评测
  • 测评结果:WA
  • 用时:94ms
  • 内存:5880kb
  • [2024-04-15 09:07:12]
  • 提交

answer

#include<bits/stdc++.h>
using namespace std;
inline int qread()
{
    int a=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch)){(a*=10)+=(ch^48);ch=getchar();}
    return a*f;
}
int T,n,ans,cnt0,cnt1,que[10000010],ed,mn,pos,A[10000010],kmp[10000010];
char ch[10000010],S[10000010];
inline int qread(char *c)
{
    int len=0;
    char ch=getchar();
    while(!isdigit(ch))ch=getchar();
    while(isdigit(ch))c[++len]=ch,ch=getchar();
    return len;
}
signed main()
{
    T=qread();
    while(T--)
    {
        n=qread(ch);ans=cnt0=cnt1=0;
        for(int i=1;i<=n;++i)
        {
            if(ch[i]=='0')++cnt0;
            else ++cnt1;
        }
        if(cnt0<cnt1)
        {
            for(int i=1;i<=n;++i)ch[i]^=1;
            reverse(ch+1,ch+1+n);swap(cnt0,cnt1);
        }
        for(int i=1;i<=n;++i)
        {
            if(ch[i]=='0')
            {
                for(int j=1;j<=n;++j)S[j]=ch[(i+j-2)%n+1];
                for(int j=1;j<=n;++j)swap(S[j],ch[j]);
				break;
            }
        }
        ed=mn=pos=0;
        for(int l=1,r;l<=n;l=r)
        {
            r=l+1;
            while(r<=n&&ch[r]=='1')++r;
            que[++ed]=r-l-1;
        }
        for(int i=1,sum=0;i<=ed;++i)
        {
            sum+=que[i]-1;
            if(sum<mn)mn=sum,pos=i;
        }
        for(int i=1;i<=ed;++i)A[i]=que[(i+pos-1)%ed+1];
        for(int i=1;i<=ed;++i)que[i]=A[i];
        for(int i=1,sum,cnt,j;i<=ed;++i)
        {
            if(que[i]<=1)continue;
            j=i,sum=0,cnt=0;
            while(1)
            {
                sum+=que[j]-1;
                if(sum<=0)break;
                ++j;++cnt;
            }
            ans=max(ans,cnt);
            for(int k=i;k<=j;++k)que[k]=1;
        }
        n=0;
        for(int i=1;i<=ed;++i)
        {
            ch[++n]='0';
            if(que[i]==1)ch[++n]='1';
        }
        for(int i=2,j;i<=n;++i)
        {
            j=kmp[i-1];
            while(j&&ch[i]^ch[j+1])j=kmp[j];
            if(ch[i]==ch[j+1])kmp[i]=j+1;
        }
        if(n%(n-kmp[n])==0)ans+=(n-kmp[n]);
        else ans+=n;
        printf("%d\n",ans);
    }
	return 0;
}

詳細信息

Test #1:

score: 100
Accepted
time: 1ms
memory: 5828kb

input:

3
1
001001
0001111

output:

1
3
9

result:

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

Test #2:

score: -100
Wrong Answer
time: 94ms
memory: 5880kb

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 513th numbers differ - expected: '18', found: '9'