Untitled

mail@pastecode.io avatar
unknown
plain_text
a month ago
2.3 kB
1
Indexable
Never
import java.util.Scanner;

public class Main {
    static int[][] arr = new int[11][10005];

    public static int getNumber(char[] str, int size) {
        int res = 0;
        for (int i = 0; i < size; i++) {
            int number = str[i] - '0';
            res = res * 10 + number;
        }
        return res;
    }

    public static void changeNumber(int number, char[] str, int size) {
        for (int i = 0; i < size; i++) {
            char c = (char) ((number % 10) + '0');
            number /= 10;
            str[size - i - 1] = c;
        }
    }

    public static void main(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int T = scanner.nextInt();

        for (int tc = 1; tc <= T; tc++) {
            char[] str = scanner.next().toCharArray();
            int N = scanner.nextInt();
            int size = str.length;
            int defaultValue = getNumber(str, size);
            arr[0][0] = defaultValue;

            int count = 0, count2 = 1, max = 0;
            for (int l = 0; l < N; l++) {
                count = count2;
                count2 = 0;
                for (int k = 0; k < count; k++) {
                    changeNumber(arr[l][k], str, size);

                    for (int i = 0; i < size - 1; i++) {
                        for (int j = i + 1; j < size; j++) {
                            char tmp = str[j];
                            str[j] = str[i];
                            str[i] = tmp;
                            int value = getNumber(str, size);

                            boolean existed = false;
                            for (int m = 0; m < count2; m++) {
                                if (arr[l + 1][m] == value) {
                                    existed = true;
                                    break;
                                }
                            }

                            if (!existed) arr[l + 1][count2++] = value;
                            if (l == N - 1 && max < value) max = value;

                            char tmp2 = str[j];
                            str[j] = str[i];
                            str[i] = tmp2;
                        }
                    }
                }
            }

            System.out.println("Case #" + tc + "\n" + max);
        }
    }
}