Jul 29, 2008

Google Code Jam 2008 - Alien Numbers

As many of your may know Google Code Jam 2008 has been recently launched.
I found some free time and decided to have a look at the tasks of this competition. Just out of curiosity... :)

Today's morning I solved the first task - "Alien Numbers".
The task is just for practice and has almost nothing to do with the real competition. I mean, it is a simple problem, but it was very interesting to solve it as fast as possible :)
Problem

The decimal numeral system is composed of ten digits, which we represent as "0123456789" (the digits in a system are written from lowest to highest). Imagine you have discovered an alien numeral system composed of some number of digits, which may or may not be the same as those used in decimal. For example, if the alien numeral system were represented as "oF8", then the numbers one through ten would be (F, 8, Fo, FF, F8, 8o, 8F, 88, Foo, FoF). We would like to be able to work with numbers in arbitrary alien systems. More generally, we want to be able to convert an arbitrary number that's written in one alien system into a second alien system.

Input

The first line of input gives the number of cases, N. N test cases follow. Each case is a line formatted as

alien_number source_language target_language

Each language will be represented by a list of its digits, ordered from lowest to highest value. No digit will be repeated in any representation, all digits in the alien number will be present in the source language, and the first digit of the alien number will not be the lowest valued digit of the source language (in other words, the alien numbers have no leading zeroes). Each digit will either be a number 0-9, an uppercase or lowercase letter, or one of the following symbols !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~

Output

For each test case, output one line containing "Case #x: " followed by the alien number translated from the source language to the target language.

Limits

1 ≤ N ≤ 100.

Small dataset

1 ≤ num digits in alien_number ≤ 4,
2 ≤ num digits in source_language ≤ 16,
2 ≤ num digits in target_language ≤ 16.

Large dataset

1 ≤ alien_number (in decimal) ≤ 1000000000,
2 ≤ num digits in source_language ≤ 94,
2 ≤ num digits in target_language ≤ 94.

Sample

Input
4
9 0123456789 oF8
Foo oF8 0123456789
13 0123456789abcdef 01
CODE O!CDE? A?JM!.

Output
Case #1: Foo
Case #2: 9
Case #3: 10011
Case #4: JAM!


The following is my solution in C++, be advised and don't blame me, there are many missing checks, I know that, and safety procedures. That is because I tried to solve this problems and get result as fast as possible. So, here you go with the solution I came up first.

#include <fstream>
#include <iostream>
#include <vector>
#include <string>

using namespace std;

typedef vector<int> IntVect_t;

size_t to_num( const string &_base_set, const string &_val)
{
size_t NumRes(0);
size_t PositionPower(1);
string::const_reverse_iterator iter = _val.rbegin();
string::const_reverse_iterator iter_end = _val.rend();
for(; iter != iter_end; ++iter)
{
NumRes += _base_set.find(*iter) * PositionPower;
PositionPower *= _base_set.size();
}
return NumRes;
}

int main(int argc, const char* argv[])
{
ifstream f(argv[1]);
if( !f.is_open() )
return 1;

string outfile(argv[1]);
ofstream f_out( (outfile+".out").c_str() );

size_t nCase;
f >> nCase;
for(size_t i = 1; i <= nCase; ++i)
{
string _src;
string _dest;
string _to_convert;
f >> _to_convert >> _src >> _dest;

IntVect_t pos_dest;
size_t NumRes = to_num( _src, _to_convert );
for(; NumRes > _dest.size()-1;)
{
pos_dest.push_back( NumRes % _dest.size());
NumRes /= _dest.size();
}
pos_dest.push_back( NumRes );

f_out << "Case #"<< i <<": ";
IntVect_t::const_reverse_iterator iter = pos_dest.rbegin();
IntVect_t::const_reverse_iterator iter_end = pos_dest.rend();
for(; iter != iter_end; ++iter)
{
f_out << _dest[*iter];
}
f_out << endl;
}
return 0;
}