QOJ.ac

QOJ

IDProblemSubmitterResultTimeMemoryLanguageFile sizeSubmit timeJudge time
#153336#6299. Binary Stringchen_zexingWA 2ms7900kbC++172.9kb2023-08-29 22:17:502023-08-29 22:17:50

Judging History

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

  • [2023-08-29 22:17:50]
  • 评测
  • 测评结果:WA
  • 用时:2ms
  • 内存:7900kb
  • [2023-08-29 22:17:50]
  • 提交

answer

#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
char s[10000005];
int a[20000005];
long long pre1[10000005],pre2[10000005],fac1[10000005],fac2[10000005];
long long base=29,mod1=998244353,mod2=1e9+7;
pair<long long,long long> query(int l,int r){
    return {((pre1[r]-pre1[l-1]*fac1[r-l+1])%mod1+mod1)%mod1,((pre2[r]-pre2[l-1]*fac2[r-l+1])%mod2+mod2)%mod2};
}
int main() {
    int T = 1, kase = 0;
    cin >> T;
    while (T--) {
        scanf("%s",s+1);
        int n=strlen(s+1);
        for(int i=1;i<=n;i++) s[i+n]=s[i];
        for(int i=1;i<=n;i++) a[i]=s[i+1]-s[i],a[i+n]=a[i];
        int fl=0,fl2=0;
        for(int i=1;i<=n;i++){
            if(a[i]!=0) fl=1;
            else fl2=0;
        }
        if(!fl){
            puts("1");
            continue;
        }
        if(!fl2){
            puts("2");
            continue;
        }
        int pos=-1;
        for(int i=1;i<=n;i++) if(a[i]==0&&a[i+1]!=0){
                pos=i+1;
                break;
            }
        vector <pair<int,int>> v;
        fl=0;
        for(int i=pos,st=i;i<pos+n;i++){
            if(a[i]==0){
                st=i+1;
                continue;
            }
            if(i+1==pos+n||a[i+1]==0) v.push_back({st,i}),fl|=(i-st+1)%2;
        }
        if(!fl){
            fac1[0]=fac2[0]=1;
            for(int i=1;i<=n;i++){
                pre1[i]=(pre1[i-1]*base+(s[i]-'0'+1))%mod1;
                pre2[i]=(pre2[i-1]*base+(s[i]-'0'+1))%mod2;
                fac1[i]=fac1[i-1]*base%mod1,fac2[i]=fac2[i-1]*base%mod2;
            }
            int ans=0;
            for(int i=1;i<=n;i++)
                if(n%i==0){
                    auto ref=query(1,i);
                    int f=1;
                    for(int j=i+1;j<=n;j+=i) if(query(j,j+i-1)!=ref) f=0;
                    if(f){
                        ans=i;
                        break;
                    }
                }
            printf("%d\n",ans);
        }
        else{
            int minus=0,lstr=-1;
            vector <int> temp;
            for(auto t:v){
                if((t.second-t.first+1)%2){
                    if(lstr!=-1) temp.push_back(t.first-lstr-1-minus);
                    lstr=t.second,minus=0;
                }
                else minus+=t.second-t.first+1;
            }
            for(auto t:v){
                if((t.second-t.first+1)%2){
                    if(lstr!=-1) temp.push_back(t.first+n-lstr-1-minus);
                    lstr=t.second,minus=0;
                    break;
                }
                else minus+=t.second-t.first+1;
            }
            sort(temp.begin(),temp.end());
            assert(temp.size()>=2);
            if(temp.back()==temp[temp.size()-2]) printf("%d\n",temp.back()+1);
            else printf("%d\n",temp[temp.size()-2]+n);
        }
    }
    return 0;
}



Details

Tip: Click on the bar to expand more detailed information

Test #1:

score: 0
Wrong Answer
time: 2ms
memory: 7900kb

input:

3
1
001001
0001111

output:

1
2
2

result:

wrong answer 2nd numbers differ - expected: '3', found: '2'