Advertisement
Hamoudi30

Untitled

Jul 6th, 2021
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.23 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. #define ll long long
  4. #define pb push_back
  5. #define SZ(v) ((int)((v).size()))
  6. void run () {
  7.     int n, k;
  8.     cin >> n >> k;
  9.     string s;
  10.     cin >> s;
  11.     int freq[30] = {0};
  12.     for (int i = 0; i < n; ++i) {
  13.         freq[s[i] - 'a']++;
  14.     }
  15.     int pos = 26; // i considered it iterate over 'z'
  16.     for (int i = 0; i < 26; ++i) {
  17.         if (k >= freq[i]) {
  18.             k -= freq[i];
  19.         } else {
  20.             pos = i; // i got the minimum char
  21.             break;
  22.         }
  23.     }
  24.     string res;
  25.     int rem = k;
  26.     for (int i = 0; i < n; ++i) {
  27.         int cur_char = s[i] - 'a';
  28.         if (cur_char > pos || (cur_char == pos && rem == 0)) {
  29.             // so i can add this char in the result string
  30.             res += s[i];
  31.         } else if (cur_char == pos) {
  32.             // here rem or k greater than or equal one so i have to reduce this operations
  33.             --rem;
  34.         }
  35.     }
  36.     cout << res << '\n';
  37. }
  38. int main () {
  39.     ios_base::sync_with_stdio(false);
  40.     cin.tie(nullptr);
  41.     cout.tie(nullptr);
  42. //    freopen("/home/hamoudi/clion/hello.in", "rt", stdin);
  43.     int tt;
  44.     tt = 1;
  45. //    cin >> tt;
  46.     while (tt--)
  47.         run();
  48. }
  49. /*
  50.  
  51.  */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement