QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#741728 | #6299. Binary String | luobotianle | WA | 84ms | 12012kb | C++14 | 1.1kb | 2024-11-13 15:06:01 | 2024-11-13 15:06:02 |
Judging History
answer
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
const int N=2e7+5;
const int mod=998244353;
int T;
string s;
int n,cnt;
int a[N],sum[N];
int st[N],top;
int ans;
int nxt[N],h[N],to[N];
int kmp(){
nxt[1]=0;
for(int i=2,j=0;i<=n;i++){
while(j&&h[i]!=h[j+1])j=nxt[j];
if(h[i]==h[j+1])j++;
nxt[i]=j;
}
int tmp=n-nxt[n];
return n%tmp?n:tmp;
}
int main(){
ios::sync_with_stdio(0);
cin.tie(0);cout.tie(0);
cin>>T;
while(T--){
cin>>s;
s=' '+s;
n=s.size()-1;
ans=top=cnt=0;
for(int i=1;i<=n;i++)nxt[i]=to[i]=h[i]=0;
for(int i=1;i<=n;i++)a[i]=(s[i]-'0'),cnt+=a[i];
if(cnt*2>n){
for(int i=1;i<=n;i++)a[i]^=1;
reverse(a+1,a+1+n);
}
for(int i=1;i<=n;i++)sum[i]=sum[i-1]+(a[i]?1:-1);
rotate(a+1,max_element(sum+1,sum+n+1)-sum+a+1,a+n+1);
for(int i=n,lst=-1,tot=0;i>=1;i--){
if(!a[i])continue;
if(lst!=-1){
st[++top]=++tot;
top-=min(top,lst-i-1);
}
if(top){
ans=max(ans,tot-st[1]+1);
to[i]=top;
}
lst=i;
}
for(int i=1;i<=n;i++){
if(!a[i])continue;
h[(i+ans-to[i]-1)%n+1]=1;
}
cout<<(ans+kmp())%mod<<"\n";
}
return 0;
}
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 12012kb
input:
3 1 001001 0001111
output:
1 3 9
result:
ok 3 number(s): "1 3 9"
Test #2:
score: -100
Wrong Answer
time: 84ms
memory: 11724kb
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 20 20 21 18 18 18 19 18 18 19 20 19 19 20 21 20 21 21 22 18 18 18 19 18 18 19 20 18 18 18 19 19 20 20 21 19 19 19 19 20 21 21 22 20 21 21 22 21 22 22 23 18 18 18 19 18 18 19 20 18 18 18 19 19 20 20 21 18 18 18 19 18 18 19 20 19 19 20 21 20 21 21 22 19 19 19 19 1...
result:
wrong answer 12th numbers differ - expected: '20', found: '19'