Google Code Jam : Alien Numbers
April 13, 2010 Leave a comment
// practice problems, April 12, 2008
// http://code.google.com/codejam/contest/dashboard?c=32003#s=p0
// @BEGIN_OF_SOURCE_CODE
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <string>
#include <cctype>
#include <stack>
#include <queue>
#include <vector>
#include <map>
#include <sstream>
#include <set>
#include <math.h>
#define For(i, a) for ( i = 0; i < a; i++ )
#define Rep(i, a, b) for ( i = a; i <= b; i++ )
#define N 1000000
using namespace std;
int returnBase (char *a, char *s)
{
string baseFormat;
char *pch;
for ( int i = 0; a [i]; i++ ) {
pch = strchr (s, a [i]);
//printf ("%d\n", pch - s + 1);
baseFormat += ((pch - s) + '0');
}
//cout << baseFormat;
int output = 0;
int sLength = strlen (s);
int baseLength = baseFormat.length () - 1;
int power = 0;
while ( baseLength > -1 ) {
//int p = ceil (pow (sLength, power));
//int t = (baseFormat.at (baseLength) - '0');
//output += (p * t);
output += ((baseFormat.at (baseLength) - '0') * ceil (pow (sLength, power)));
baseLength--;
power++;
}
return output;
}
void printOutput (char *a, int n)
{
int length = strlen (a);
string convertBase;
while ( n ) {
convertBase += (( n % length) + '0');
//cout << convertBase;
n /= length;
}
reverse (convertBase.begin (), convertBase.end ());
//cout << convertBase;
for ( unsigned int i = 0; i < convertBase.size (); i++ ) {
cout << a [convertBase [i] - '0'];
}
cout << endl;
}
int main ()
{
freopen ("A-large.in", "r", stdin);
freopen ("out-large.out", "w", stdout);
int n;
scanf ("%d", &n);
int cases = 0;
while ( n-- ) {
char alienNumber [1000];
char sourceLanguage [1000];
char targetLanguage [1000];
scanf ("%s %s %s", alienNumber, sourceLanguage, targetLanguage);
printf ("Case #%d: ", ++cases);
int nthNumber = returnBase (alienNumber, sourceLanguage);
//printf ("%d\n", nthNumber);
printOutput (targetLanguage, nthNumber);
}
return 0;
}
// @END_OF_SOURCE_CODE

