Black & White (思維題+模擬)

//https://ac.nowcoder.com/acm/contest/893/F
#include<bits/stdc++.h>

using namespace std;

const int N=100005;

string str;
int t,n,m;

int jojo(){
	int l,r,cnt,ans;
	l=r=cnt=ans=0;
	while(r<n){
		if(str[r]=='1') cnt++;
		while(cnt>m){
			if(str[l]=='1') cnt--;
			l++;
		}
		r++;
		ans=max(ans,r-l);
	}
	
	return ans;
}

int main(){
	cin>>t;
	while(t--){
		str.clear();
		cin>>n>>m>>str;
		int ans=jojo();
		for(int i=0;i<n;i++){
			if(str[i]=='0') str[i]='1';
			else str[i]='0';
		}
		ans=max(ans,jojo());
		cout<<ans<<endl;
	}
	
	return 0;
}

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章