Untitled

mail@pastecode.io avatar
unknown
c_cpp
18 days ago
1.2 kB
1
Indexable
Never
#include <iostream>
#include <cstring>
#include <algorithm>
using namespace std;

int n, k;
char a[10];
int price;
bool visited[1000000][11];

int toInt(char *x) {
    int num = 0;
    for (int i = 0; x[i] != '\0'; i++) {
        num = num * 10 + (x[i] - '0');
    }
    return num;
}

void solve(int exchange) {
    if (exchange == 0) {
        int currentPrice = toInt(a);
        if (currentPrice > price) {
            price = currentPrice;
        }
        return;
    }

    int currentState = toInt(a);
    if (visited[currentState][exchange]) {
        return;
    }
    visited[currentState][exchange] = true;

    for (int i = 0; i < n; i++) {
        for (int j = i + 1; j < n; j++) {
            if (a[i] != a[j]) {
                swap(a[i], a[j]);
                solve(exchange - 1);
                swap(a[i], a[j]);
            }
        }
    }
}

int main() {
    freopen("input.txt", "r", stdin);
    int t;
    cin >> t;
    for (int tc = 1; tc <= t; tc++) {
        cin >> a;
        cin >> k;
        n = strlen(a);
        price = 0;
        memset(visited, false, sizeof(visited));

        solve(k);
        cout << "Case #" << tc << endl << price << endl;
    }
    return 0;
}
Leave a Comment