#include<bits/stdc++.h> using namespace std; #define INF 0x3f3f3f3f typedef long long ll; const double eps = 1e-8; const double pi = acos(-1.0); ll n, k; string s; int main() { scanf("%lld%lld", &n, &k); cin >> s; ll result = 200005; ll char_map[128] = {0}; map<char, int> word_map; for(ll i=0; i<s.length(); i++){ char_map[s[i]]++; if(char_map[s[i]] == 1){ word_map[s[i]] = i; } else if(char_map[s[i]] == k){ ll tmp = i-word_map[s[i]]+1; if(result > tmp) result=tmp; for(ll j=word_map[s[i]]+1; j<=i; j++){ if(s[j] == s[i]){ word_map[s[i]] = j; char_map[s[i]]--; break; } } } } if(result == 200005) cout << -1 << endl; else cout << result << endl; return 0; }
全部评论
(2) 回帖