QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#390809 | #6299. Binary String | OccDreamer | TL | 1ms | 5764kb | C++14 | 3.1kb | 2024-04-15 22:18:47 | 2024-04-15 22:18:48 |
Judging History
answer
//code by Emissary
#include<bits/stdc++.h>
#define vc vector
#define db double
#define fi first
#define se second
#define ll long long
#define mk make_pair
#define pb push_back
#define RI register int
#define PI pair<int,int>
#define ull unsigned long long
#define err cerr << " -_- " << endl
#define debug cerr << " ------------------- " << endl
#define input(x) freopen(#x".in","r",stdin)
#define output(x) freopen(#x".out","w",stdout)
#define NO puts("No")
#define YES puts("Yes")
//#define OccDreamer
//#define int long long
using namespace std;
namespace IO{
inline int read(){
int X=0, W=0; char ch=getchar();
while(!isdigit(ch)) W|=ch=='-', ch=getchar();
while(isdigit(ch)) X=(X<<1)+(X<<3)+(ch^48), ch=getchar();
return W?-X:X;
}
inline void write(int x){
if(x<0) x=-x, putchar('-');
if(x>9) write(x/10);
putchar(x%10+'0');
}
inline void sprint(int x){write(x), putchar(32);}
inline void eprint(int x){write(x), putchar(10);}
}using namespace IO;
const int MAXN = 1e7 + 7;
int topf;
int n, tot;
int nxt[MAXN], stk[MAXN];
bool mark[MAXN<<1];
char s[MAXN<<1];
struct range{
int l, r, num, nowtim;
}Q[MAXN];
inline void Rotate(int len){
for(int i=n+1;i<n+len;++i) s[i]=s[i-n];
for(int i=1;i<=n;++i) s[i]=s[i+len-1];
return ;
}
inline int Nxt(int x){return x==n?1:x+1;}
inline void solve(){
scanf("%s",s+1); n=strlen(s+1);
int fir=0;
for(int i=1;i<=n;++i)
if(s[i]!=s[1]){fir=i; break;}
if(!fir) return eprint(1);
Rotate(fir); int sum0=0, sum1=0;
int nowlen=0, num=s[1]-'0';
for(int i=1;i<=n;++i){
if(s[i]-'0'==num) ++nowlen;
else{
if(nowlen>1) sum0+=(num==0)*(nowlen-1), sum1+=(num==1)*(nowlen-1);
num^=1; nowlen=1;
}
}
if(nowlen>1) sum0+=(num==0)*(nowlen-1), sum1+=(num==1)*(nowlen-1);
if(sum1<sum0){
for(int i=1;i<=n;++i) s[i]^=1;
reverse(s+1,s+1+n);
}
sum0=0, sum1=0;
nowlen=0, num=s[1]-'0'; int pos=1, lim=0;
for(int i=1;i<=n;++i){
if(s[i]-'0'==num) ++nowlen;
else{
if(nowlen>1){
sum0+=(num==0)*(nowlen-1), sum1+=(num==1)*(nowlen-1);
if(sum1-sum0<lim) lim=sum1-sum0, pos=i;
}
num^=1; nowlen=1;
}
}
if(nowlen>1) sum0+=(num==0)*(nowlen-1), sum1+=(num==1)*(nowlen-1);
Rotate(pos); tot=0;
nowlen=0, num=s[1]-'0'; num^=1;
/*
1
1000001010101010111110011100001111110000101
*/
int maxn=0;
for(int i=1;i<=n;++i){
if(s[i]-'0'==num){
if(s[i]=='0') maxn=max(maxn,(i-stk[topf])/2), --topf;
else stk[++topf]=i;
}
else num=s[i]-'0';
}
int ans=maxn;
for(int i=1;i<=n;++i) mark[i]=0; pos=1; mark[0]=1;
for(int i=1;i<=topf;++i){
int j=stk[i]+maxn;
mark[j%n==0?n:j%n]=1, pos=j%n==0?n:j%n;
}
int las=pos;
for(int i=Nxt(pos);i!=pos;i=Nxt(i)){
if(mark[i]) continue;
mark[i]=mark[las]^1; las=i;
}
for(int i=1;i<=n;++i) mark[i+n]=mark[i]; int now=0;
for(int i=2;i<=n<<1;++i){
while(now && mark[now+1]!=mark[i]) now=nxt[now];
if(mark[now+1]==mark[i]) ++now;
nxt[i]=now;
}
eprint(ans+((n<<1)-nxt[n<<1]));
}
signed main(){
int t=read();
while(t--) solve();
return 0;
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 5764kb
input:
3 1 001001 0001111
output:
1 3 9
result:
ok 3 number(s): "1 3 9"
Test #2:
score: -100
Time Limit Exceeded
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...