QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#112324 | #6299. Binary String | ethening | RE | 1ms | 3480kb | C++17 | 2.0kb | 2023-06-11 10:31:21 | 2023-06-11 10:31:24 |
Judging History
answer
#include "bits/stdc++.h"
#include <algorithm>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
void solve() {
string s;
cin >> s;
int n = s.size();
int cnt0 = count(begin(s), end(s), '0');
int cnt1 = count(begin(s), end(s), '1');
if (cnt0 == 0 || cnt1 == 0) {
cout << 1 << "\n";
return;
}
if (cnt0 > cnt1) {
swap(cnt0, cnt1);
for (auto &ch : s) ch = '0' + '1' - ch;
reverse(begin(s), end(s));
}
// cnt0 <= cnt1 after this
{
int st{};
for (int i = 0; i < n; i++) {
if (s[i] == '0') {
st = i;
break;
}
}
string tmp;
for (int i = st; i < n; i++) tmp += s[i];
for (int i = 0; i < st; i++) tmp += s[i];
s = tmp;
}
// s[0] == '0'
vector<int> prev0, after0;
for (int i = 0; i < n; i++) {
if (s[i] == '0') prev0.push_back(i);
}
after0.push_back(prev0[0]);
for (int i = 1; i < cnt0; i++) {
after0.push_back(max(prev0[i], after0[i - 1] + 2));
}
int ans = 0;
for (int i = 0; i < cnt0; i++) {
ans = max(ans, after0[i] - prev0[i]);
}
string s2(n, '1');
for (int i = 0; i < cnt0; i++) {
s2[after0[i]] = '0';
}
const ll MOD = 998244353ll;
const ll base = 53;
vector<ll> pwr(n + 1);
for (int i = 0; i < n; i++) {
if (i == 0) {
pwr[0] = base;
}
else {
pwr[i] = pwr[i - 1] * base % MOD;
}
}
ll cur_hash = 0;
for (int i = 0; i < n; i++) {
if (s2[i] == '1') {
cur_hash += pwr[n - 1 - i];
if (cur_hash > MOD) cur_hash -= MOD;
}
}
ll first_hash = cur_hash;
int first_rep = n;
for (int i = 0; i < n - 1; i++) {
if (s2[i] == '1') {
cur_hash -= pwr[n - 1];
}
cur_hash = cur_hash * pwr[0];
if (s2[i] == '1') {
cur_hash += pwr[0];
}
cur_hash = (cur_hash % MOD + MOD) % MOD;
if (cur_hash == first_hash) {
first_rep = i + 1;
break;
}
}
ans += first_rep;
cout << ans << "\n";
}
int main() {
cin.tie(0)->sync_with_stdio(0);
int t;
cin >> t;
while (t--) {
solve();
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 1ms
memory: 3480kb
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...