QOJ.ac
QOJ
ID | 题目 | 提交者 | 结果 | 用时 | 内存 | 语言 | 文件大小 | 提交时间 | 测评时间 |
---|---|---|---|---|---|---|---|---|---|
#411354 | #6299. Binary String | SamponYW | RE | 0ms | 0kb | C++14 | 2.3kb | 2024-05-15 11:59:45 | 2024-05-15 12:10:28 |
Judging History
answer
#include <bits/stdc++.h>
#define db double
#define il inline
#define re register
#define ll long long
#define ui unsigned
#define ull ui ll
#define i128 __int128
#define pii pair<int, int>
#define fi first
#define se second
#define eb emplace_back
#define SZ(v) (int)v.size()
#define ALL(v) v.begin(), v.end()
#define mems(v, x) memset(v, x, sizeof(v))
#define memc(a, b) memcpy(a, b, sizeof(a))
#define FOR(i, L, R) for(re int i = (L); i <= (R); ++i)
#define ROF(i, R, L) for(re int i = (R); i >= (L); --i)
#define LS i << 1, l, mid
#define RS i << 1 | 1, mid + 1, r
#define popc(x) __builtin_popcount(x)
using namespace std;
#define N 10000005
#define P 998244353
il int add(int x, int y) {return x + y < P ? x + y : x + y - P;}
il void addr(int &x, int y) {(x += y) >= P && (x -= P);}
il int qpow(int p, int n = P - 2) {
int s = 1;
while(n) {
if(n & 1) s = 1ll * s * p % P;
p = 1ll * p * p % P, n >>= 1;
}
return s;
}
string str; int n, v[N];
int f[N];
int u[N];
il void WORK() {
cin >> str, n = SZ(str);
int cnt = 0; for(auto c : str) cnt += c & 1;
if(!cnt || cnt == n) {cout << "1\n"; return ;}
if(cnt > n - cnt) {for(auto &c : str) c ^= 1; reverse(ALL(str));}
int num = 0;
FOR(i, 0, n - 1) if(str[i] == '0') {
rotate(str.begin(), str.begin() + i, str.end()); break;
}
str = " " + str;
int m = 0;
FOR(i, 1, n) if(str[i] == '0') {
int j = i + 1; while(j <= n && str[j] == '1') ++j;
v[++m] = j - i - 1, i = j - 1;
}
int mn = 0, id = 1, sum = 0;
FOR(i, 1, m) {
sum += v[i] - 1;
if(sum < mn) mn = sum, id = i;
}
rotate(v + 1, v + id, v + 1 + m);
int ans = 0;
FOR(i, 1, m) if(v[i] > 1) {
int now = 0, j = i;
for(; ; ) {
now += v[j] - 1; if(now <= 1) break; ++j;
}
ans = max(ans, j - i);
FOR(k, i, j) v[k] = 1;
}
int L = 0;
FOR(i, 1, m) {
str[++L] = '0';
if(v[i]) str[++L] = '1';
}
assert(L == n);
int j = 0;
FOR(i, 2, n) {
while(j && str[j + 1] != str[i]) j = f[j];
if(str[j + 1] == str[i]) ++j; f[i] = j;
}
int k = n - f[n];
if(n % k == 0) ans += k;
else ans += n;
cout << ans << "\n";
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0), cout.tie(0);
int T; cin >> T;
while(T--) WORK();
cerr << 1.0 * clock() / CLOCKS_PER_SEC << "\n";
return 0;
}
詳細信息
Test #1:
score: 0
Runtime Error
input:
3 1 001001 0001111