fork download
  1. #include <iostream>
  2. #include <algorithm>
  3.  
  4. using namespace std;
  5.  
  6. class CantSurviveNextAnimalInQueue
  7. {
  8. public:
  9. CantSurviveNextAnimalInQueue()
  10. : last(0)
  11. {}
  12.  
  13. bool operator()(char ch)
  14. {
  15. if (ch >= last)
  16. {
  17. last = ch;
  18. return false;
  19. }
  20. return true;
  21. }
  22.  
  23. private:
  24. char last;
  25. };
  26.  
  27. string JungleFight(const string& queue)
  28. {
  29. string result(queue.size(), '0');
  30.  
  31. result.erase(
  32. std::remove_copy_if(queue.rbegin(), queue.rend(), result.begin(), CantSurviveNextAnimalInQueue()),
  33. result.end());
  34. std::reverse(result.begin(), result.end());
  35. return result;
  36. }
  37.  
  38. int main()
  39. {
  40. std::ios_base::sync_with_stdio(false);
  41. int t;
  42. cin >> t >> ws;
  43.  
  44. string line;
  45. while (t-- && getline(cin, line))
  46. {
  47. cout << JungleFight(line) << '\n';
  48. }
  49. cout << flush;
  50.  
  51. return 0;
  52. }
  53.  
Success #stdin #stdout 0s 3468KB
stdin
5
sdaadd
asldfdaklfjskdvnxcnsdfsdgfksvnkjv
zzyvgf
klkkn
klKKnNLlNL
stdout
sddd
xvv
zzyvgf
n
nlNL