CF1680C Binary String

You are given a string ss consisting of characters 0 and/or 1.

You have to remove several (possibly zero) characters from the beginning of the string, and then several (possibly zero) characters from the end of the string. The string may become empty after the removals. The cost of the removal is the maximum of the following two values:

  • the number of characters 0 left in the string;
  • the number of characters 1 removed from the string.

What is the minimum cost of removal you can achieve?

Input

The first line contains one integer tt (1≤t≤1041≤t≤104) — the number of test cases.

Each test case consists of one line containing the string ss (1≤|s|≤2⋅1051≤|s|≤2⋅105), consisting of characters 0 and/or 1.

The total length of strings ss in all test cases does not exceed 2⋅1052⋅105.

Output

For each test case, print one integer — the minimum cost of removal you can achieve.

Example

input

5
101110110
1001001001001
0000111111
00000
1111

output

1
3
0
0
0

思路:二分答案+双指针判断当前答案是否符合

#include<iostream>
#include<algorithm>
#include<cstring>
using namespace std;
#define int long long 
const int N=1000010;
int a[N];
int s1[N];
int s0[N];
int n;
string s;
bool check(int x)
{
	for(int i=1,j=0;i<=n;i++)
	{
		while(j<i&&s0[i]-s0[j]>x) j++;
		if(s1[j]+s1[n]-s1[i]<=x) return true;
	}
	return false;
}
signed main()
{
	int t;
	cin>>t;
	while(t--)
	{
		cin>>s;
		s=" "+s;
		n=s.size();
		int l=0,r=n;
		for(int i=1;i<=n;i++)
		{
			if(s[i]=='1')
			{
				s1[i]=s1[i-1]+1;
				s0[i]=s0[i-1];
			}
			else
			{
				s0[i]=s0[i-1]+1;
				s1[i]=s1[i-1];
			}
		}
		while(l<r)
		{
			int mid=l+r>>1;
			if(check(mid)) r=mid;
			else l=mid+1;
		}
		cout<<l<<endl;
	}
	
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值