Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <map>
- #include <cmath>
- #include <set>
- #include <vector>
- using namespace std;
- int main() {
- ios_base::sync_with_stdio(false);
- int n, k;
- cin >> n >> k;
- string s;
- cin >> s;
- multiset<int> ms;
- for(int i = 0; i < 26; i++) {
- ms.insert(0);
- }
- int i = 0, j = 0;
- int question_marks = 0;
- int sum = 0;
- vector<int> cnt(26, 0);
- while(j < n) {
- if(*ms.begin() <= k and *ms.rbegin() <= k) {
- if(s[j] != '?') {
- sum++;
- ms.erase(ms.find(cnt[s[j] - 'a']));
- cnt[s[j] - 'a']++;
- ms.insert(cnt[s[j] - 'a']);
- }
- else {
- question_marks++;
- }
- j++;
- if(sum + question_marks == 26 * k) {
- cout << i + 1 << endl;
- return 0;
- }
- }
- else {
- if(s[i] != '?') {
- sum--;
- ms.erase(ms.find(cnt[s[i] - 'a']));
- cnt[s[i] - 'a']--;
- ms.insert(cnt[s[i] - 'a']);
- }
- else {
- question_marks--;
- }
- i++;
- if(sum + question_marks == 26 * k) {
- cout << i << endl;
- return 0;
- }
- }
- }
- cout << -1 << endl;
- return 0;
- }
- // a?cdefghijklmnopqrstuvwxyza?
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement