QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#741715 | #6299. Binary String | luobotianle | WA | 85ms | 11744kb | C++14 | 1.1kb | 2024-11-13 15:04:15 | 2024-11-13 15:04:18 |
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=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: 0ms
memory: 11728kb
input:
3 1 001001 0001111
output:
1 3 9
result:
ok 3 number(s): "1 3 9"
Test #2:
score: -100
Wrong Answer
time: 85ms
memory: 11744kb
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 16 18 17 17 16 17 16 16 15 18 17 17 16 17 16 16 15 17 16 16 15 16 15 15 14 18 17 17 16 17 16 16 15 17 16 16 15 16 15 15 14 17 16 16 15 16 15 15 14 16 15 15 14 15 14 14 13 18 17 17 16 17 16 16 15 17 16 16 15 16 15 15 14 17 16 16 15 16 15 15 14 16 15 15 14 15 14 14 13 17 16 16 15 1...
result:
wrong answer 8th numbers differ - expected: '20', found: '16'