Advertisement
Josif_tepe

Untitled

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