QOJ.ac
QOJ
ID | Problem | Submitter | Result | Time | Memory | Language | File size | Submit time | Judge time |
---|---|---|---|---|---|---|---|---|---|
#485726 | #6299. Binary String | BalintR | TL | 0ms | 0kb | C++20 | 3.3kb | 2024-07-21 02:49:11 | 2024-07-21 02:49:13 |
answer
#include <bits/stdc++.h>
using namespace std;
typedef unsigned uint;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
typedef vector<int> vi;
typedef vector<pii> vpii;
typedef complex<double> cpx;
template <typename T> using minPq = priority_queue<T, vector<T>, greater<T>>;
#define ms(a, x) memset(a, x, sizeof(a))
#define pb push_back
#define fs first
#define sn second
#define ALL(v) begin(v), end(v)
#define SZ(v) ((int) (v).size())
#define lbv(v, x) (lower_bound(ALL(v), x) - (v).begin())
#define ubv(v, x) (upper_bound(ALL(v), x) - (v).begin())
template <typename T> inline void UNIQUE(vector<T> &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}
const int INF = 0x3f3f3f3f;
const ll LLINF = 0x3f3f3f3f3f3f3f3f;
const double PI = acos(-1);
#define FR(i, n) for(int i = 0; i < (n); i++)
#define FOR(i, a, b) for(int i = (a); i < (b); i++)
#define FORR(i, a, b) for(int i = (a); i >= (b); i--)
#define dbg(x) {cerr << #x << ' ' << x << endl;}
#define dbgArr(arr, n) {cerr << #arr; FR(_i, n) cerr << ' ' << (arr)[_i]; cerr << endl;}
template <typename T, typename U>
ostream& operator<<(ostream &os, pair<T, U> p){return os << "(" << p.fs << ", " << p.sn << ")";}
const int MN = 1e6 + 5;
int n;
string str;
int arr[MN], resArr[MN], qu[MN*2], ql, qr;
int getItsToEq(){
ql = qr = 0;
FR(i, n){
qu[qr++] = i;
qr -= min(arr[i], qr);
}
FR(i, qr) qu[i] -= n;
int res = 0;
FR(i, n){
qu[qr++] = i;
if(arr[i]){
qr -= arr[i]-1;
if(qr <= ql) for(;;);
res = max(res, i-qu[qr-1]);
qr--;
}
if(qr > ql) for(;;);
assert(ql <= qr);
while(ql < qr && qu[ql] < (i-n+1)) ql++;
resArr[i] = !(ql < qr && qu[ql] == i-n+1);
}
// dbg(res);
return res;
}
const int MOD = 1e9 + 7;
const int X = 590294819;
int pws[MN], hashes[MN];
int getNumRepeats(){
// dbgArr(resArr, n);
FR(i, n) hashes[i+1] = ((ll) hashes[i]*X + resArr[i]) % MOD;
FOR(k, 1, n) if(n % k == 0){
ll h1 = hashes[k];
for(int l = k; l < n; l += k){
ll h2 = hashes[l+k] - (ll) hashes[l]*pws[k];
if((h1 - h2) % MOD) goto loopEnd;
}
return n/k;
loopEnd:;
}
return 1;
}
int solve(){
cin >> str;
int num1 = count(ALL(str), '1');
if(num1 == 0 || num1 == SZ(str)) return 1;
if(num1 > SZ(str)/2) for(char &ch : str) ch ^= 1;
else reverse(ALL(str));
rotate(str.begin(), find(ALL(str), '0'), str.end());
rotate(str.begin(), find(ALL(str), '1'), str.end());
assert(str.back() == '0');
char lst = -1;
n = 0;
FR(i, SZ(str)){
if(str[i] == '1'){
n += lst != '1';
arr[n-1]++;
}
else {
n += lst == '0';
}
lst = str[i];
}
// dbgArr(arr, n);
int init = getItsToEq();
int numReps = getNumRepeats();
// dbg(numReps);
return init + SZ(str)/numReps;
}
int main(){
cin.sync_with_stdio(0); cin.tie(0);
pws[0] = 1;
FR(i, MN-1) pws[i+1] = (ll) pws[i] * X % MOD;
int t; cin >> t;
while(t--){
cout << solve() << '\n';
fill_n(arr, n, 0);
}
}
Details
Tip: Click on the bar to expand more detailed information
Test #1:
score: 0
Time Limit Exceeded
input:
3 1 001001 0001111