問在有限的操作數範圍內,能夠得到最大連續的相同字符的子串的長度是多少。

#include<iostream>
#include<string>
#include<vector>
using namespace std;

int n,m;
string str;//全局變量;
 int change(char ch){
    vector<int>loc;
    for(int i=0;i<n;i++){if(str[i]==ch) loc.push_back(i);}//將與字符相同的下標存入vector;
    loc.push_back(n);
    int maxlen=loc[m];//loc中存的索引其實是操作m次時的最大長度;
    for(int j=m+1;j<loc.size();j++){
        maxlen=max(maxlen, loc[j]-loc[j-m-1]-1);
    }
    return maxlen;
}

int main(){
    cin>>n>>m;
    cin>>str;
   cout<<max(change('a'),change('b'))<<endl;   
}

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