QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#153386 | #6299. Binary String | chen_zexing | RE | 1ms | 11952kb | C++17 | 2.3kb | 2023-08-29 23:49:10 | 2023-08-29 23:49:11 |
Judging History
answer
#pragma GCC optimize("Ofast,unroll-loops")
#include <bits/stdc++.h>
using namespace std;
char s[10000005];
int a[20000005],b[10000005],c[10000005];
long long pre1[10000005],pre2[10000005],fac1[10000005],fac2[10000005];
long long base=29,mod1=998244353,mod2=1e9+7;
pair<long long,long long> query(int l,int r){
return {((pre1[r]-pre1[l-1]*fac1[r-l+1])%mod1+mod1)%mod1,((pre2[r]-pre2[l-1]*fac2[r-l+1])%mod2+mod2)%mod2};
}
int main() {
int T = 1, kase = 0;
cin >> T;
while (T--) {
scanf("%s", s + 1);
int n = strlen(s + 1), cnt = 0;
for (int i = 1; i <= n; i++) s[i + n] = s[i];
for (int i = 1; i <= n; i++) {
if (s[i] == s[i + 1]) {
if (s[i] == '0') a[i] = -1;
else a[i] = 1;
}
else a[i] = 0;
cnt += a[i];
a[i + n] = a[i];
b[i]=b[i+n]=0;
}
if (cnt < 0) {
for (int i = 1; i <= n; i++) a[i] = a[i + n] = -a[i];
for (int i = 1; i <= n; i++) swap(a[i], a[n * 2 - i + 1]);
}
int mn = 0, pos = 1,mx=0;
for (int i = 1; i <= n; i++) {
c[i] = c[i - 1] + a[i];
if (c[i] < mn) mn = c[i], pos = i + 1;
}
vector<int> v;
for (int i = pos; i < pos + n; i++) {
if (a[i] == 1) v.push_back(i);
else if(a[i]==-1) mx=max(mx,(i-v.back()+1)/2),v.pop_back();
}
for(auto t:v){
b[t-pos+1+mx]=1;
assert(t-pos+1+mx<=n);
}
fac1[0] = fac2[0] = 1;
for (int i = 1; i <= n; i++) {
pre1[i] = (pre1[i - 1] * base + (b[i] + 1)) % mod1;
pre2[i] = (pre2[i - 1] * base + (b[i] + 1)) % mod2;
fac1[i] = fac1[i - 1] * base % mod1, fac2[i] = fac2[i - 1] * base % mod2;
}
int ans = 0;
for (int i = 1; i <= n; i++)
if (n % i == 0) {
auto ref = query(1, i);
int f = 1;
for (int j = i + 1; j <= n; j += i) if (query(j, j + i - 1) != ref) f = 0;
if (f) {
ans = i;
break;
}
}
printf("%d\n", ans+mx);
}
return 0;
}
/*
1
10001111000111
*/
详细
Test #1:
score: 100
Accepted
time: 1ms
memory: 11952kb
input:
3 1 001001 0001111
output:
1 3 9
result:
ok 3 number(s): "1 3 9"
Test #2:
score: -100
Dangerous Syscalls
input:
262144 000000000000000000 100000000000000000 010000000000000000 110000000000000000 001000000000000000 101000000000000000 011000000000000000 111000000000000000 000100000000000000 100100000000000000 010100000000000000 110100000000000000 001100000000000000 101100000000000000 011100000000000000 11110000...