http://code.google.com/codejam/contest/dashboard?c=agdjb2RlamFtcg4LEghjb250ZXN0cxh5DA
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 | Output |
4 | Case #1: Foo |
import java.io.BufferedInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;
public class AlienNumbers {
public String convert(String input){
String[] sp = input.split(" ");
String sourceLang = sp[1];
String destLang = sp[2];
String number =sp[0];
long exactVal =0;
for (int i=0;i<number.length();i++){
exactVal+=sourceLang.indexOf(number.substring(i, i+1))*Math.pow(sourceLang.length(), number.length()-i-1);
}
StringBuffer sb = new StringBuffer();
for (int i=0;exactVal!=0;i++){
sb.append(destLang.substring((int)(exactVal%destLang.length()),(int)((exactVal%destLang.length())+1)));
exactVal = (long)(exactVal/destLang.length());
}
return sb.reverse().toString();
}
public static void main(String[] args) throws FileNotFoundException {
AlienNumbers an = new AlienNumbers();
System.out.println(an.convert("9 0123456789 oF8"));
System.out.println(an.convert("Foo oF8 0123456789"));
System.out.println(an.convert("13 0123456789abcdef 01"));
System.out.println(an.convert("CODE O!CDE? A?JM!."));
InputStream is = new BufferedInputStream(new FileInputStream("Input.txt"));
Scanner scan = new Scanner(new File("Input.txt"));
PrintWriter pw = new PrintWriter("Output.txt");
Integer count =new Integer(scan.nextLine());
for (int i=0;i<count;i++){
pw.write("Case #"+(i+1)+": "+an.convert(scan.nextLine())+"\n");
}
pw.close();
scan.close();
}
}
No comments:
Post a Comment