fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. // Function to delete character at position i in the string
  5. string delpos(int i, string s) {
  6. s.erase(i, 1);
  7. return s;
  8. }
  9.  
  10. int main() {
  11. string s;
  12. int k, count = 0;
  13. cin >> s >> k;
  14.  
  15. // Traverse the string from the beginning to the end
  16. for (int i = 0; i < s.size() - 1; i++) {
  17. if (s[i] < s[i + 1] && count < k) {
  18. s = delpos(i, s);
  19. count++;
  20. i--; // Adjust index after deletion
  21. }
  22. }
  23.  
  24. // Traverse the string from the end to the beginning
  25. for (int i = s.size() - 1; i >= 0 && count < k; i--) {
  26. s = delpos(i, s);
  27. count++;
  28. }
  29.  
  30. cout << s << endl;
  31. return 0;
  32. }
Success #stdin #stdout 0.01s 5284KB
stdin
12468976587 6
stdout
97687