Click Here to go back to the homepage.

Alien Numbers Solution:


#include <bits/stdc++.h>
using namespace std;

int convTo10(string num, string base){
    int fromBase[200];
    int baseLength = base.length();
    for(int i = 0; i < baseLength; i++){
        fromBase[base[i]] = i;
    }
    int result = 0;
    for(char c:num){
        result *= baseLength;
        result += fromBase[c];
    }
    return result;
}

string solve(string num, string from, string to){
    char toBase[200] = {};
    int toBaseLength = to.length();
    strcpy(toBase, to.c_str());
    int b10 = convTo10(num, from);
    string result;
    while(b10){
        result.push_back(toBase[b10 % toBaseLength]);
        b10 /= toBaseLength;
    }
    reverse(result.begin(), result.end());
    return result;
}

int main(){
    // #ifndef TESTING
    // freopen("input.txt", "r", stdin);
    // freopen("output.txt", "w", stdout);
    // #endif

    int cases;
    cin >> cases;
    for(int i = 1; i <= cases; i++){
        string num, from, to;
        cin >> num >> from >> to;
        
        cout << "Case #" << i << ": " << solve(num, from, to) << endl;
    }
    
    return 0;
}