QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#95457 | #6299. Binary String | wyzwyz | WA | 176ms | 5628kb | C++14 | 2.3kb | 2023-04-09 19:26:18 | 2023-04-09 19:26:19 |
Judging History
answer
#include<cstdio>
#include<cctype>
#include<cstring>
#include<algorithm>
#define maxn 10011001
template<class T>
inline T read(){
T r=0,f=0;
char c;
while(!isdigit(c=getchar()))f|=(c=='-');
while(isdigit(c))r=(r*10)+(c^48),c=getchar();
return f?-r:r;
}
template<class T>
inline T min(T a,T b){
return a<b?a:b;
}
template<class T>
inline T max(T a,T b){
return a>b?a:b;
}
const int base=19491001;
int n,top,sta[maxn][2];
char StR[maxn],str[maxn];
inline void work(){
scanf("%s",StR+1);
n=strlen(StR+1);
int st=0;
for(int i=1;i<n&&!st;i++)
if(StR[i]!=StR[n])st=i;
if(!st)return puts("1"),void();
for(int i=0;i<n;i++)
str[i+1]=StR[(st+i-1)%n+1];
int cnt[2]={0,0};
for(int i=1,lst=-1;i<=n;i++){
if(str[i]-'0'!=lst)lst=str[i]-'0';
else cnt[str[i]-'0']++;
}
if(cnt[0]>cnt[1]){
for(int i=1;i<=n;i++)
str[i]=str[i]^1;
std::reverse(str+1,str+1+n);
}
st=1,cnt[0]=cnt[1]=0;
for(int i=1,lst=-1;i<=n;i++){
if(str[i]-'0'!=lst){
lst=str[i]-'0';
if(cnt[0]>cnt[1])
st=i,cnt[0]=cnt[1]=0;
}
else cnt[str[i]-'0']++;
}
int ans=0;
top=0,str[n+1]='2';
for(int i=0;i<n;i++)
StR[i+1]=str[(st+i-1)%n+1];
for(int i=1;i<=n;i++)str[i]=StR[i];
for(int i=1,lst=0,cnt=0;i<=n+1;i++){
if(str[i]==str[lst]){
cnt++;
continue;
}
if(!cnt){
lst=i;
continue;
}
if(str[lst]=='1'){
sta[++top][0]=cnt;
sta[top][1]=i-1;
}
else {
int T=0,pos=lst;
while(cnt){
int c=min(sta[top][0],cnt);
sta[top][0]-=c,cnt-=c;
if(cnt)pos=pos-(pos-sta[top][1]-T-1)/2;
T+=c+(pos-sta[top][1]-T-1)/2;
sta[top][1]-=c;
if(!sta[top][0])top--;
}
ans=max(ans,T);
}
lst=i,cnt=0;
}
top=max(top,1);
for(int i=1;i<=n;i++)str[i]=0;
do{
int r=sta[top][1],l=r-sta[top][0];
for(int i=l;i<=r;i++)str[i]='1';
}while(--top);
for(int l=1,r;l<=n;l=r+1){
if(str[r=l])continue;
while(r<n&&!str[r+1])r++;
str[r]=str[r%n+1]^1;
for(int i=r-1;i>=l;i--)
str[i]=str[i+1]^1;
}
unsigned __int128 h=0,H=0,P=1;
for(int i=0;i<n;i++)
h+=str[n-i]*P,P*=base;
H=h;
int x=1;
do{
ans++,h*=base;
h-=P*str[x],h+=str[x++];
}while(h^H);
printf("%d\n",ans);
}
int main(){
int t=read<int>();
while(t--)work();
return 0;
}
詳細信息
Test #1:
score: 100
Accepted
time: 2ms
memory: 5628kb
input:
3 1 001001 0001111
output:
1 3 9
result:
ok 3 number(s): "1 3 9"
Test #2:
score: -100
Wrong Answer
time: 176ms
memory: 5588kb
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 152nd numbers differ - expected: '22', found: '21'