WA on #7 && #21 提交记录
#include<bits/stdc++.h>
using namespace std;
#define int long long
int n,k,l,r,mid,a[100005],b[100005];
string s;
bool check(int x){
int cnt=1,tot=0;
b[1]=a[1];
for(int i=2;i<=n;i++){
b[i]=a[i];
if(b[i]==b[i-1]){
cnt++;
}else{
cnt=1;
}
if(cnt>x){
b[i]^=1;
cnt=1;
tot++;
}
if(tot>k){
return 0;
}
}
return 1;
}
signed main(){
ios::sync_with_stdio(false);
cout.tie(0); cout.tie(0);
cin>>n>>k>>s;
for(int i=0;i<n;i++){
if(s[i]=='N'){
a[i+1]=1;
}
}
r=n;
while(l<r){
mid=(l+r)>>1;
if(check(mid)){
r=mid;
}else{
l=mid+1;
}
}
cout<<l;
return 0;
}