QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#440084 | #6299. Binary String | grass8cow | WA | 131ms | 16184kb | C++17 | 1.8kb | 2024-06-13 07:51:46 | 2024-06-13 07:51:47 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=1e7+10;
int n;char c[N*2],c_[N];
int s1[N],qz[N],hz[N];
int M(int x){return (x>=n)?(x-n):x;}
int sta[N],top;
bool vis[N];
int kmp[N];
void sol(){
scanf("%s",c),n=strlen(c);
if(n==1){puts("1");return;}
int s=0;
for(int i=0;i<n;i++){
if(c[i]=='1'&&c[(i+1)%n]=='1')s++;
if(c[i]=='0'&&c[(i+1)%n]=='0')s--;
}
if(s<0){reverse(c,c+n);for(int i=0;i<n;i++)if(c[i]=='0')c[i]='1';else c[i]='0';}
for(int i=0;i<n;i++){
s1[i+1]=s1[i];
if(c[i]=='1'&&c[M(i+n-1)]=='1')s1[i+1]++;
if(c[i]=='0'&&c[M(i+1)]=='0')s1[i+1]--;
}
qz[0]=0;for(int i=1;i<=n;i++)qz[i]=min(qz[i-1],s1[i]);
hz[n]=s1[n];for(int i=n-1;i>=0;i--)hz[i]=min(hz[i+1],s1[i]);
int t=-1;
for(int i=0;i<n;i++)if(s1[i]<=hz[i]&&s1[i]<=s1[n]+qz[i]){t=i;break;}
assert(t!=-1);
for(int i=0;i<n;i++)c_[i]=c[M(i+t)];for(int i=0;i<n;i++)c[i]=c_[i];
int T=0;
top=0;
for(int i=0;i<n;i++){
vis[i]=0;
if(c[i]=='1'&&c[M(i+n-1)]=='1')sta[++top]=i;
if(c[i]=='0'&&c[M(i+1)]=='0')T=max(T,i-sta[top--]);
}
T=(T+1)/2;
if(!top)T+=2;
else{
for(int i=1;i<=top;i++)vis[sta[i]]=1,vis[M(sta[i]+n-1)]=1;
int pr=-1;
for(int i=0;i<n;i++)if(vis[i]){
c[i]='1';
for(int j=pr+1;j<i;j++)c[j]=((j-pr)&1)?'0':'1';
pr=i;
}
for(int j=pr+1;j<n;j++)c[j]=((j-pr)&1)?'0':'1';
for(int i=0;i<n;i++)c[i+n]=c[i];
for(int i=2,j=0;i<=n*2;i++){
while(j&&c[j]!=c[i-1])j=kmp[j];
if(c[j]==c[i-1])j++;kmp[i]=j;
if(kmp[i]==n){T+=i-n;break;}
}
}
printf("%d\n",T);
}
int main(){
int T;scanf("%d",&T);while(T--)sol();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 0ms
memory: 16184kb
input:
3 1 001001 0001111
output:
1 3 9
result:
ok 3 number(s): "1 3 9"
Test #2:
score: -100
Wrong Answer
time: 131ms
memory: 16184kb
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 28794th numbers differ - expected: '12', found: '21'