#include<iostream>
using namespace std;
const int maxn = 2e5+10;
string s;
int pre[maxn];
bool check(int x) {
int i = 0,j = 0;
int cnt = x;
for(int j = s.length();j >= 1 && cnt > 0;--j) {
if(s[j] == '1') cnt--;
}
int ans = (j-1) - pre[j-1]; // ans = j-1 - pre[j-1]
if(ans <= x) return true;
int l1 = 0,n = s.length();
while(i <= n && j <= n && l1 <= x) {
while(s[i] == '0' && i <= n) i+=1;
while(s[j] == '0' && j <= n) j+=1;
ans = (j-1)-pre[j-1] - (i-pre[i]);
if(ans <= x) return true;
i += 1;j += 1;
}
return false;
}
int main()
{
int t;
cin >> t;
while(t--) {
cin >> s;
s = "0" + s;
pre[0] = 0;
for(int i = 1;i < int(s.length());++i) {
pre[i] = pre[i-1] + s[i]-'0';
}
int l = 0,r = pre[int(s.length())];
while(l < r) {
int mid = (l+r+1) / 2;
if(check(mid)) {
l = mid;
}
else {
r = mid-1;
}
}
cout << l << endl;
}
}
binary string
最新推荐文章于 2022-11-24 14:08:45 发布