fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3.  
  4. void swap_chars(string &s, int i, int j) {
  5. char temp = s[i];
  6. s[i] = s[j];
  7. s[j] = temp;
  8. }
  9.  
  10. int main() {
  11. int t;
  12. cin >> t;
  13. while (t--) {
  14. string s;
  15. cin >> s;
  16. int pos;
  17. for (int i = 0; i < s.size(); i++) {
  18. int k=0;pos=i;
  19. for(int j=i;j<min(i+10,(int)s.size());j++)
  20. { if (s[j] - s[i] > j-i+k) {
  21. k=s[j]-s[i]-j+i;pos=j; }
  22. }while(pos>i){swap_chars(s,pos,pos-1);s[pos-1]--;pos--;}
  23. }
  24. cout << s << endl;
  25. }
  26. return 0;
  27. }
  28.  
Success #stdin #stdout 0s 5272KB
stdin
6
19
1709
11555
51476
9876543210
5891917899
stdout
81
6710
33311
55431
9876543210
7875567711