2. 小信反转串
题目ID:8093
5 分
时间限制: 1000ms
空间限制: 262144kB
题目描述
时间:1s 空间:256M
题目描述:
小信有一个长为 �n 的只含 00 和 11字符串 �s。 他可以进行最多 �k 次如下操作:
- 选择字符串 �s 的一个子串,将其中的字符反转(00 变成 11,11 变成 00)。
求在操作过程中(操作数可为00)出现过的最长的连续的 11 的长度。
输入格式:
第一行,22 个正整数 �,�n,k; 第二行,字符串 �s。
输出格式:
输出不超过 �k 次操作后,最长的连续的 11 的长度。
样例1输入:
5 1 00010
样例1输出:
4
样例2输入:
5 2 00010
样例2输出:
5
约定:
对于100%的数据,1≤�,�≤1051≤n,k≤105。 字符串 �s 只由 00 和 11 组成,长度为 �n。
我甚至把我的代码和老师的对了三遍,第二个样例就愣是过不了(一直输出4)
我的代码:
#include<bits/stdc++.h>
using namespace std;
int n,k;
string s;
int ans;
int main(){
cin>>n>>k;
cin>>s;
int a=(int)s.size();
int l=0,r=0;
int cnt=0;
while(r<a){
while(cnt<k and r<a){
if(s[r]=='0'){
cnt++;
while(r<n and s[r]=='0'){
r++;
}
}
while(s[r]=='1'){
r++;
}
}
ans=max(ans,r-1);
while(s[l]=='1' and l<r){
l++;
}
if(s[l]=='0'){
cnt--;
while(s[l]=='0' and l<r){
l++;
}
}
}
cout<<ans;
return 0;
}