题解 | #编程题2#

编程题2

https://www.nowcoder.com/practice/dcc301bc11a7420b88afdbd272299809?tpId=137&tqId=33880&rp=1&ru=/exam/oj&qru=/exam/oj&sourceUrl=%2Fexam%2Foj%3Fpage%3D1%26pageSize%3D50%26search%3D%26tab%3D%25E5%2590%258D%25E4%25BC%2581%25E7%259C%259F%25E9%25A2%2598%26topicId%3D137&difficulty=undefined&judgeStatus=undefined&tags=&title=

#include <iostream>
#include <cstdio>
#include <algorithm>
using namespace std;
int count_max_len(char f,const string& s,int m,int n);
int main() {
    int m, n;
    string s;
    cin >> n >> m >> s;
    if (m == n)cout << n;
    else cout<< max(count_max_len('a',s,m,n),count_max_len('b',s,m,n));
    return 0;
}
int count_max_len(char f, const string& s,int m,int n) {
    int max_len = m, ca = 0;
    int pl = 0, pr = 0;
    while (pr < n) {
        if (s[pr] == f) {
            ca++;
            if (ca == (m + 1)) {
                ca--;
                max_len = max(max_len, pr - pl);
                while (pl <= pr && s[pl] != f) {
                    pl++;
                }
                pl++;
            }
        }
        pr++;
    }
    return max_len;
}
// 64 位输出请用 printf("%lld")

#算法#
全部评论

相关推荐

totoroyyw:千年老妖😂
投递华为等公司10个岗位
点赞 评论 收藏
分享
点赞 评论 收藏
分享
评论
点赞
收藏
分享
牛客网
牛客企业服务