Advertisement
Alex_tz307

IOIT passphrase

Dec 15th, 2020
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.81 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2.  
  3. using namespace std;
  4.  
  5. int main() {
  6.     ios_base::sync_with_stdio(false);
  7.     cin.tie(nullptr);
  8.     cout.tie(nullptr);
  9.     int N, K;
  10.     string s;
  11.     cin >> N >> K >> s;
  12.     s = '0' + s;
  13.     stack<char> S;
  14.     int cnt = 0, poz = N + 1;
  15.     for(int i = 1; i <= N; ++i) {
  16.         while(!S.empty() && S.top() < s[i] && cnt < K) {
  17.             ++cnt;
  18.             S.pop();
  19.         }
  20.         if(cnt == K) {
  21.             poz = i;
  22.             break;
  23.         }
  24.         S.emplace(s[i]);
  25.     }
  26.     string sol;
  27.     while(!S.empty()) {
  28.         sol.push_back(S.top());
  29.         S.pop();
  30.     }
  31.     reverse(sol.begin(), sol.end());
  32.     for(int i = poz; i <= N; ++i)
  33.         sol.push_back(s[i]);
  34.     while(cnt < K) {
  35.         sol.pop_back();
  36.         ++cnt;
  37.     }
  38.     cout << sol;
  39. }
  40.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement