QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#95886 | #6299. Binary String | psc233 | WA | 225ms | 3656kb | C++14 | 1.8kb | 2023-04-12 11:53:15 | 2023-04-12 11:53:17 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
const int N=1e7+10;
char s[N];
int nxt[N<<1],f[N],q[N<<1];
int T,n;
int kmp(){
for (int i=n;i<n*2;i++) s[i]=s[i-n];
nxt[0]=0;
for (int i=1;i<n*2;i++){
nxt[i]=nxt[i-1];
while (nxt[i]&&s[nxt[i]]!=s[i]) nxt[i]=nxt[nxt[i]-1];
if (s[nxt[i]]==s[i]) nxt[i]++;
}
int s=n*2-nxt[2*n-1];
return s;
}
int main(){
scanf("%d",&T);
while (T--){
scanf("%s",s);
n=strlen(s);
if (n==1){
puts("1"); continue;
}
int sum=0;
for (int i=0;i<n;i++) if (s[i]=='0') sum++;
if (sum*2>n){
for (int i=0;i<n;i++) if (s[i]=='0') s[i]='1'; else s[i]='0';
for (int i=0;i<n-1-i;i++) swap(s[i],s[n-1-i]);
}
bool p=0;
for (int i=0;i<n;i++) if (s[i]=='0'&&s[(i+1)%n]=='0') p=1;
int ans=0;
if (p){
ans++;
for (int i=0;i<n;i++) if (s[i]=='0'&&s[(i+1)%n]=='1'){
swap(s[i],s[(i+1)%n]); i++;
}
}
/*
1111100101000111
1111101010001011
1111110100010101
1111111000101010
0111111001010101
1011111010101010//循环开始
*/
for (int i=n;i<n*2;i++) s[i]=s[i-n];
f[0]=(s[0]=='1'?1:-1);
for (int i=1;i<n*2;i++) f[i]=f[i-1]+(s[i]=='1'?1:-1);
int cnt=0,ws=0;
vector<int>w;
for (int i=n*2-1;i>=0;i--){
q[++cnt]=i;
while (cnt>1&&f[q[cnt]]<=f[q[cnt-1]]) cnt--,q[cnt]=q[cnt+1];
if (i<n&&s[i+1]=='1'){
if (cnt==1) w.push_back((i+1)%n);
else ws=max(ws,(q[cnt-1]-q[cnt])/2);
}
}
ans+=ws;
if (!(int)w.size()) ans+=2;
else{
for (int i=0;i<n;i++) s[i]='?';
for (int i=0;i<w.size();i++){
s[(w[i]+ws)%n]='1';
}
int j=0;
for (int i=0;i<n;i++) if (s[i]=='1'){
int t=0;
for (int k=i-1;k>=j;k--,t^=1) s[k]=(t?'1':'0');
j=i+1;
}
int t=0;
for (int i=j;i<n;i++,t^=1) s[i]=(t?'1':'0');
ans+=kmp();
}
printf("%d\n",ans);
}
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 3656kb
input:
3 1 001001 0001111
output:
1 3 9
result:
ok 3 number(s): "1 3 9"
Test #2:
score: -100
Wrong Answer
time: 225ms
memory: 3592kb
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 1023rd numbers differ - expected: '10', found: '11'