QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#257579 | #6299. Binary String | zhaohaikun | WA | 247ms | 3592kb | C++14 | 2.6kb | 2023-11-19 10:31:29 | 2023-11-19 10:31:30 |
Judging History
answer
// MagicDark
#include <bits/stdc++.h>
#define debug cerr << "[" << __LINE__ << "] "
#define SZ(x) (int) x.size() - 1
#define all(x) x.begin(), x.end()
#define ms(x, y) memset(x, y, sizeof x)
#define F(i, x, y) for (int i = (x); i <= (y); i++)
#define DF(i, x, y) for (int i = (x); i >= (y); i--)
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
template <typename T> inline void chkmax(T &x, T y) {x = max(x, y);}
template <typename T> inline void chkmin(T &x, T y) {x = min(x, y);}
template <typename T> inline void read(T &x) {
x = 0; int f = 1; char c = getchar();
for (; !isdigit(c); c = getchar()) if (c == '-') f = -f;
for (; isdigit(c); c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
x *= f;
}
const int N = 1e7 + 10, MOD = 998244353;
string st;
int kmp[N];
int gcd(int x, int y) {return y ? gcd(y, x % y) : x;}
void zhk() {
cin >> st;
int c0 = count(all(st), '0'), c1 = count(all(st), '1');
if (c0 < c1) {
string stt = "";
for (char i: st) stt += i ^ 1;
reverse(all(stt));
swap(st, stt);
}
int n = st.size();
st = ' ' + st;
int pos = 0, mn = 0;
int w = 0;
F(i, 1, n) {
if (st[i] == '1') w--;
else w++;
if (w < mn) {
mn = w;
pos = i;
}
}
string gg = "";
int ss = 0, mx = 0, v = 0;
// F(i, 1, n) {
// if ()
// }
string tt = " ";
F(i, 1, n) {
char cur = st[(pos + i - 1) % n + 1];
tt += cur;
}
// debug << tt << endl;
DF(i, n, 1) {
char cur = st[(pos + i - 1) % n + 1];
// debug << i << " " << ((pos + i - 1) % n + 1) << " " << cur << endl;
if (cur == '0') {
if (ss) {
// ss--;
// v -= i;
if (--ss == 0) {
chkmax(mx, v);
v = 0;
}
} else gg += '0';
// gg += cur, ss = 0;
} else {
if (gg.size() && gg.back() == '1') {
ss++;
v++;
// v += i + 1;
gg += "01";
} else gg += "1";
// chkmax(mx, ++ss);
// if (gg.back() == '1') swap(gg[SZ(gg) - 1], gg[SZ(gg)]);
// gg += cur;
}
// if (cur == '0') s0++;
}
reverse(all(gg));
gg = ' ' + gg;
// debug << gg << endl;
int ans = mx;
// debug << ans << endl;
// for (int i = 1, j; i <= n; )
int j = 0;
F(i, 2, n) {
while (j && gg[i] != gg[j + 1]) j = kmp[j];
if (gg[i] == gg[j + 1]) j++;
kmp[i] = j;
// debug << i << " " << kmp[i] << endl;
}
int t = n - kmp[n];
if (n % t == 0) ans += t;
else ans += n;
// w = n / gcd(n, kmp[n] ? kmp[n] : 1);
// debug << gg << " # " << w << " " << kmp[n] << endl;
// int ans = (mx + w) % MOD;
cout << ans << '\n';
}
signed main() {
int _ = 1;
cin >> _;
while (_--) zhk();
return 0;
}
/* why?
*/
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 100
Accepted
time: 0ms
memory: 3592kb
input:
3 1 001001 0001111
output:
1 3 9
result:
ok 3 number(s): "1 3 9"
Test #2:
score: -100
Wrong Answer
time: 247ms
memory: 3544kb
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 19 19 19 20 21 18 18 18 19 18 18 19 20 19 19 19 20 20 21 21 22 18 18 18 19 18 18 19 20 18 18 18 19 19 19 20 21 19 19 19 19 19 19 20 21 20 20 21 22 21 22 22 23 18 18 18 19 18 18 19 20 18 18 18 19 19 19 20 21 18 18 18 19 18 18 19 20 19 19 19 20 20 21 21 22 19 19 19 19 1...
result:
wrong answer 12th numbers differ - expected: '20', found: '19'