fork download
  1. #include <bits/stdc++.h>
  2. using namespace std;
  3. typedef long long ll;
  4. const int Mod=998244353;
  5.  
  6. void solve() {
  7.  
  8. string s;
  9. cin >> s;
  10.  
  11. for(int i=0;i<s.size();++i){
  12. int best =s[i]-'0';
  13. int k=i;
  14. for(int j=i;j<min(i+10,(int)s.size());j++){
  15. if(s[j]-'0'-j+i>best){
  16. best=s[j]-'0'-j+i;
  17. k=j;
  18. }
  19. }
  20. while(k>i){
  21. swap(s[k],s[k--]);
  22. }
  23. s[i]=char(best+'0');
  24. }
  25. cout << s << '\n';
  26. }
  27.  
  28. int main(){
  29. ios::sync_with_stdio(false);
  30. cin.tie(nullptr);
  31.  
  32. int t;
  33. cin >> t;
  34. while (t--) solve();
  35.  
  36.  
  37. return 0;
  38. }
  39.  
Success #stdin #stdout 0.01s 5292KB
stdin
6
19
1709
11555
51476
9876543210
5891917899
stdout
81
6710
33311
55431
9876543210
7875567711