QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#153338 | #6299. Binary String | chen_zexing | WA | 110ms | 9852kb | C++17 | 2.9kb | 2023-08-29 22:20:35 | 2023-08-29 22:20:36 |
Judging History
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=1;
}
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()+2);
else printf("%d\n",temp[temp.size()-2]+n);
}
}
return 0;
}
//0 1 0 -1
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 9836kb
input:
3 1 001001 0001111
output:
1 3 9
result:
ok 3 number(s): "1 3 9"
Test #2:
score: -100
Wrong Answer
time: 110ms
memory: 9852kb
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 19 19 19 20 21 18 18 18 19 18 18 19 20 19 19 19 20 20 20 21 22 18 18 18 19 18 18 19 20 18 18 18 19 19 19 20 21 19 19 19 19 19 19 20 21 20 20 20 21 21 21 22 23 18 18 18 19 18 18 19 20 18 18 18 19 19 19 20 21 18 18 18 19 18 18 19 20 19 19 19 20 20 20 21 22 19 19 19 20 1...
result:
wrong answer 12th numbers differ - expected: '20', found: '19'