牛客寒假基础集训营 | Day1 G题—eli和字符串

在这里插入图片描述

一. 题目描述

二. 测试用例

三. 代码

#include <iostream>
using namespace std;
#include <cmath>
#include <vector>
int main(){
    int n,k;
    cin >> n >> k;
    string ss;
    cin >> ss;
    vector<int> count[26];
    for(int i=0;i<ss.size();i++){
        count[ss[i]-'a'].push_back(i);
    }
    bool flag = false;
    int ans = n;
    for(int i=0;i<26;i++){
        if(count[i].size() >= k){
            for(int j=0;j<count[i].size()-k+1;j++){
                int foo = count[i][j+k-1]-count[i][j]+1;
                if(foo <= ans){
                    flag = true;
                    ans = foo;
                }  
            }
        }
    }
    if(flag){
        cout << ans << endl;
    }else{
        cout << -1 << endl;
    }
    return 0;
}
发布了137 篇原创文章 · 获赞 13 · 访问量 1万+

猜你喜欢

转载自blog.csdn.net/qq_34170700/article/details/104171917
今日推荐