Advertisement
Josif_tepe

Untitled

Mar 13th, 2023
826
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.47 KB | None | 0 0
  1. #include <iostream>
  2. #include <map>
  3. #include <cmath>
  4. #include <set>
  5. #include <vector>
  6. #include <queue>
  7. #include <algorithm>
  8. using namespace std;
  9. typedef long long ll;
  10. int main() {
  11.     ios_base::sync_with_stdio(false);
  12.     int n, k;
  13.     cin >> n >> k;
  14.     string s;
  15.     cin >> s;
  16.    
  17.     multiset<int> ms;
  18.     for(int i = 0; i < 26; i++) {
  19.         ms.insert(0);
  20.     }
  21.     vector<int> cnt(26, 0);
  22.     int i = 0, j = 0;
  23.     int question_marks = 0;
  24.     int chars = 0;
  25.     while(j < n) {
  26.         if(*ms.begin() <= k and *ms.rbegin() <= k) {
  27.             if(s[j] == '?') {
  28.                 question_marks++;
  29.             }
  30.             else {
  31.                 chars++;
  32.                 ms.erase(ms.find(cnt[s[j] - 'a']));
  33.                 cnt[s[j] - 'a']++;
  34.                 ms.insert(cnt[s[j] - 'a']);
  35.             }
  36.             j++;
  37.             if(chars + question_marks == 26 * k) {
  38.                 cout << i + 1 << endl;
  39.                 return 0;
  40.             }
  41.         }
  42.         else {
  43.             if(s[i] == '?') {
  44.                 question_marks--;
  45.             }
  46.             else {
  47.                 chars--;
  48.                 ms.erase(ms.find(cnt[s[i] - 'a']));
  49.                 cnt[s[i] - 'a']--;
  50.                 ms.insert(cnt[s[i] - 'a']);
  51.             }
  52.             i++;
  53.             if(chars + question_marks == 26 * k) {
  54.                 cout << i << endl;
  55.                 return 0;
  56.             }
  57.         }
  58.     }
  59.     cout << -1 << endl;
  60.    
  61.     return 0;
  62. }
  63.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement