fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. #define fastio ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
  5. #define ll long long
  6. void solve() {
  7. //dharmil here
  8. string x; cin>>x;
  9.  
  10. int s[x.size()];
  11. char mx = 'a'-1;;
  12. for(int i =x.size()-1;i>=0;i--)
  13. {
  14. if(x[i] > mx){
  15. mx = x[i];
  16. s[i] = i;
  17. }else if(x[i] == mx){
  18. s[i] = i;
  19. }else{
  20. s[i] = s[i+1];
  21. }
  22. }
  23. string b;
  24. int i=0;
  25. while( i < x.size()){
  26. b += x[s[i]];
  27. i = s[i] + 1;
  28. }
  29.  
  30. cout << b << endl;
  31. }
  32.  
  33. int main() {
  34. fastio;
  35. int t;
  36. cin >> t;
  37. while (t--) {
  38. solve();
  39. }
  40. return 0;
  41. }
Success #stdin #stdout 0.01s 5284KB
stdin
2
abzdczz
abczabcyabcxabck
stdout
zzz
zyxk