Problem: 2050D - Digital string maximization
Language: java

Solution 1 (runtime: 281 ms):
import java.io.*;
import java.util.*;

public class DigitalStringMaximization {
    public static void main(String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        StringTokenizer tokenizer = new StringTokenizer(in.readLine());

        int t = Integer.parseInt(tokenizer.nextToken());

        while (t-- > 0) {
            tokenizer = new StringTokenizer(in.readLine());
            char[] c = tokenizer.nextToken().toCharArray();

            for (int i = c.length - 1; i > 0; i--) {
                if (c[i] > c[i - 1] + 1) {
                    operation(c, i);
                    i += 9;
                    if (i > c.length) {
                        i = c.length;
                    }
                }
            }

            System.out.println(new String(c));
        }
    }

//    public static int findLast(char[] c) {
//        for (int i = c.length - 1; i > 0; i--) {
//            if (c[i] > c[i - 1] + 1) {
//                return i + 1;
//            }
//        }
//        return c.length;
//    }

    public static void operation(char[] c, int i) {
        int cur = c[i];
        int left = c[i - 1];
        c[i - 1] = (char) (cur - 1);
        c[i] = (char) left;
    }
}

Solution 2 (runtime: 312 ms):
import java.io.*;

import java.util.*;



public class Main{

    public static void main(String[] args) throws Exception {

        if (System.getProperty("ONLINE_JUDGE") == null) {

            System.setIn(new FileInputStream("input.txt"));

            System.setOut(new PrintStream(new FileOutputStream("output.txt")));

        }



        FastReader sc = new FastReader();

        PrintWriter out = new PrintWriter(System.out);



        int t = sc.nextInt(); 

        //template

        while (t-- > 0) {

            String num = sc.next();

            int[] nums = new int[num.length()];

            int idx=0;

            while(idx<num.length()){

                nums[idx] = num.charAt(idx)-'0';

                idx++;

            }

            for(int i=1;i<num.length();i++){

                idx=i;

                while(idx>=1&&nums[idx-1]+1<nums[idx]&&nums[idx]>0){

                    int val = nums[idx];

                    nums[idx]=nums[idx-1];

                    nums[idx-1]=val-1;

                    idx--;

                }

                //System.out.println(Arrays.toString(nums));

            }

            StringBuilder sb = new StringBuilder();

            for(int i:nums)sb.append(i);

            System.out.println(sb);

        }



        out.flush(); 

    }



    // Optimized Fast Input Reader

    static class FastReader {

        BufferedReader br;

        StringTokenizer st;



        public FastReader() {

            br = new BufferedReader(new InputStreamReader(System.in));

        }



        String next() {

            while (st == null || !st.hasMoreElements()) {

                try {

                    st = new StringTokenizer(br.readLine());

                } catch (IOException e) {

                    e.printStackTrace();

                }

            }

            return st.nextToken();

        }



        int nextInt() {

            return Integer.parseInt(next());

        }



        long nextLong() {

            return Long.parseLong(next());

        }



        double nextDouble() {

            return Double.parseDouble(next());

        }



        String nextLine() {

            String str = "";

            try {

                str = br.readLine();

            } catch (IOException e) {

                e.printStackTrace();

            }

            return str;

        }

    }

}

class Pair<K,V>{

    K key;

    V value;

    public Pair(K key,V value){

        this.key=key;

        this.value=value;

    }

    public K getKey(){

        return this.key;

    }

    public V getValue(){

        return this.value;

    }

}