Problem: 2050D - Digital string maximization
Language: c++17 

Solution 1 (runtime: 61 ms):
#include <bits/stdc++.h>

using namespace std;



bool podeTrocar(const string &texto, int i) {

return (i < (int)texto.size() && texto[i] > '0' && (texto[i] - 1) > texto[i - 1]);

}



int main() {

ios::sync_with_stdio(false);

cin.tie(nullptr);

int t, k;

char valor, aux;

cin >> t;

for(int i=0; i<t; i++) {

string s;

cin >> s;

int tamanho = s.size();

deque<int> fila;

for (int j = 1; j < tamanho; j++) {

if (podeTrocar(s, j))

fila.push_back(j);

}

while (!fila.empty()) {

k = fila.front();

fila.pop_front();

if (k < tamanho && podeTrocar(s, k)) {

valor = s[k] - 1;

aux = s[k - 1];

s[k - 1] = valor;

s[k] = aux;

if (k - 1 > 0 && podeTrocar(s, k - 1))

fila.push_back(k - 1);

if (k < tamanho && podeTrocar(s, k))

fila.push_back(k);

if (k + 1 < tamanho && podeTrocar(s, k + 1))

fila.push_back(k + 1);

}

}



cout << s << "\n";

}

}



Solution 2 (runtime: 61 ms):
#include <bits/stdc++.h>

using namespace std;

using ll = long long ; using ld = long double ;

constexpr ll mod = 1e9 + 7 ;

#define reverse(res) reverse(res.begin(),res.end());

#define all(x) (x).begin(), (x).end()

#define rall(x) (x).rbegin(), (x).rend()

#define umap unordered_map

#define uset unordered_set

#define pb push_back

#define eb emplace_back

#define pob pop_back

#define pii pair<int,int>

#define ff first

#define ss second

#define lb lower_bound

#define vi vector<int>

#define int long long

#define ub upper_bound

#define contain(map, i) (map.find(i) == map.end())

#define maxele max_element

#define minele min_element

#define len(s) (s).length()

#define nl cout<<endl

#define rep(i, a, b) for(int i = a; i < b; i++)

#define trav(it , strt , end) for(auto it = strt; it != end ; it++ )

/*

**************************************************************************************************

**************************************************************************************************

***        **************

***        **************

***  ***   ***   ***

***  ***   ***   ***

***        ***

*************************

*************************

***        ***

***   ***   ***  ***

***   ***   ***  ***

**************        ***

**************        ***

*************************************************************************************************

**************************************************************************************************

*/

/*ll gcd(ll a ,ll b){

if(b==0)return a;

return gcd(b,a%b);

}

inttobin(int n){

string ans='';

while(n!=0){

if(n%2==1)ans+='1';

else ans+='0';

n=n/2;

}

reverse(ans);

cout<<ans<<endl;

}

int bintodec(string s){

int n=s.size();

for(int i=n-1;i>=0;i--){

if(s[i]=='1')num+=pow(2,i);

}

return num;

}

long long fastExpo(int a, int b,int m ) {

long long  res = 1;

while(b > 0) {

if(b&1) {

res = (res)%m*(a)%m;

}

b = b >> 1;

a = ((a)%m *( a)%m)%m;

}

return res;

}

bool checkithsetbit(int n,int i){

return (n&(1<<i)!=0);

}

int setithbit(int n,int i){

return (n|(1<<i));

}

int clearithbit(int n,int i){

return(n&(~(1<<i)));

}

int toggleithbit(int n,int i){

return (n^(1<<i));

}

int addm(int a, int b)

{ return ((a % mod) + (b % mod)) % mod;}

int subtm(int a, int b)

{ return ((a % mod) - (b % mod) + mod) % mod;}

int mul(int a, int b)

{ return ((a % mod) * (b % mod)) % mod; }

int modInverse(int a)

{return fastpow(a, mod - 2);}

int divm(int a, int b)

{    return mul(a, modInverse(b));}*/

/************************************************/

void solve(){

string s;

cin>>s;

int n=s.size();

for(int i=0;i<n-1;i++){

// cout<<i<<" ";

int j=i;

int maxind=i;

int maxi=s[i]-'0';

// cout<<maxi<<" "<<maxind<<" ";

while(j<min(i+10,n)){

// cout<<(s[j]-'0'-(j-i))<<" ";

if((s[j]-'0'-(j-i))>maxi){

maxi=s[j]-'0'-(j-i);

maxind=j;

}





j++;



}

// cout<<maxind<<" ";

if (maxind != i) {

char temp = s[maxind]-(maxind-i);





for (int j = maxind; j > i; j--) {

s[j] = s[j - 1];

}



s[i] = temp;

}

// cout<<s<<"\n";



}

cout<<s<<"\n";



return;

}



signed main() {

ios_base::sync_with_stdio(false);

cin.tie(); cout.tie(0);

#ifndef ONLINE_JUDGE

freopen("input.txt", "r", stdin);

freopen("output.txt", "w", stdout);

#endif

int t;

cin>>t;

while(t--){

solve();

}

return 0;

}