Advertisement
ann8497

Untitled

Feb 2nd, 2023
1,234
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.70 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. string stackToString(stack<char> &s){
  5.     string res = "";
  6.  
  7.     while (!s.empty()) {
  8.         res.push_back(s.top());
  9.         s.pop();
  10.     }
  11.     reverse(res.begin(), res.end());
  12.     return res;
  13. }
  14.  
  15. string solve(string str, int k, int n){
  16.     stack<char> s;
  17.     for(int i = 0; i<n; i++){
  18.         int cnt = n - 1 - i;  
  19.         while((!s.empty()) && (cnt + s.size() >= k) && (s.top() > str[i]))s.pop();
  20.         if(s.size() < k)s.push(str[i]);
  21.     }
  22.     return stackToString(s);
  23. }
  24.  
  25.  
  26. int main() {
  27.    
  28.    
  29.     string s;
  30.     cin>>s;
  31.     int k;
  32.     cin>>k;
  33.     int n = s.length();
  34.    
  35.     cout<<solve(s, k, n);
  36.    
  37.    
  38.     return 0;
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement