http://code.google.com/codejam/contest/dashboard?c=agxjb2RlamFtLXByb2RyEAsSCGNvbnRlc3RzGIP6AQw#
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.
这个题目实际上是个进制转换的题目, 由于Large dataset最大的数也不超过1000000000, 所以可以以十进制作为中间结果保存, 也就是说,先把字符串转化成10进制,然后输出目标字符串.
是个很简单的题目, 时间和空间复杂度都是O(n)
代码如下:
#include <stdio.h>
#include <string.h>
#define MAX_LENGTH (128)
int main()
{
int nRound;
int i, j;
FILE* fp;
char pSrc[MAX_LENGTH], pDst[MAX_LENGTH], pLanSrc[MAX_LENGTH], pLanDst[MAX_LENGTH];
int pHashSrc[MAX_LENGTH];
fp = fopen("1.txt", "r");
fscanf(fp, "%d", &nRound);
for(i=0; i<nRound; i++)
{
int nDigSrc, nDigDst;
unsigned int nVal = 0;
fscanf(fp, "%s %s %s", pSrc, pLanSrc, pLanDst);
nDigSrc = strlen(pLanSrc);
nDigDst = strlen(pLanDst);
for(j=0; j<nDigSrc; j++)
{
pHashSrc[pLanSrc[j]] = j;
}
for(j=0; j<MAX_LENGTH; j++)
{
if(pSrc[j] == '/0') break;
nVal *= nDigSrc;
nVal += pHashSrc[pSrc[j]];
}
printf("Case #%d: ", i+1);
j = 0;
while(nVal>0)
{
pDst[j] = pLanDst[nVal%nDigDst];
nVal /= nDigDst;
j++;
}
pDst[j] = '/0';
for(--j; j>=0; j--)
printf("%c", pDst[j]);
printf("/n");
}
fclose(fp);
return 0;
}