Ir para o conteúdo

FIXME Este documento precisa de ser traduzido para português, cumprido possíveis normas, como citação da fonte original em Inglês.

Alien Numbers


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.


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 !"#$%&'()*+,-./:;<=>?@[]^_`{|}~


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


Input                        Output
4                            Case #1: Foo
9 0123456789 oF8             Case #2: 9
Foo oF8 0123456789           Case #3: 10011
13 0123456789abcdef 01       Case #4: JAM!



#include <iostream>
#include <string>
#include <vector>
#include <cmath>
#include <algorithm>

using namespace std;

string source,destination;

void get(vector<int>& v,const string& num,const string& source) {
  for (string::const_iterator i=num.begin();i!=num.end();i++)

int to10(const vector<int>& from,int n) {
  int aux=0;
  for (vector<int>::const_iterator i=from.begin();i!=from.end();i++)
  return aux;

vector<int> from10(int num,int n) {
  vector<int> aux;
  while (num) {
  return aux;

string to(vector<int>& from) {
  vector<int> aux=from10(to10(from,source.size()),destination.size());
  string s;
  for (vector<int>::const_iterator i=aux.begin();i!=aux.end();i++) {
  return s;

int main() {
  int t;
  string num;
  vector<int> v;
  cin >> t;
  for (int i=1;i<=t;i++) {
    cin >> num >> source >> destination;
    cout << "Case #" << i << ": " << to(v) << endl;
  return 0;

Solução por Warrior


import Data.Array.Unboxed
import Data.List
import Text.Printf

main = getContents >>= processLines . tail . lines

processLines :: [String] -> IO ()
processLines = zipWithM_ (x l -> printf "Case #%d: %sn" x (translate (words l))) [1..]

translate (num:org:dst:[]) = fromDec lngDst lenDst . toDec lngOrg lenOrg $ num 
  where lngOrg = array ('0', '255') $ zip org   [0..]
        lenOrg = length org
        lngDst = array (0   , 100   ) $ zip [0..] dst
        lenDst = length dst

toDec :: UArray Char Int -> Int -> String -> Int
toDec alfa len = snd . foldl' soma 0 . map (alfa!)
  where soma val n = val * len + n

fromDec :: UArray Int Char-> Int -> Int -> String
fromDec alfa len = map (alfa!) . reverse . unfoldr f
  where f 0 = Nothing
        f x = Just (r, q)
        (q,r) = x `divMod` len

Solução por Betovsky


def to_decimal_list(alien_number, source_language):
    return [source_language.index(x) for x in alien_number]

def to_decimal(decimal_list, source_base):
    d = 0
    for n in decimal_list:
        d = d*source_base+n
    return d

def from_decimal(decimal_number, target_base):
    if target_base == 10:
        o = map(int, str(decimal_number))
        o = []
        while True:
            q = decimal_number/target_base
            o.insert(0, decimal_number%target_base)
            if q == 0:
            decimal_number = q
    return o

def from_decimal_list(decimal_list, target_language):
    return "".join(target_language[x] for x in decimal_list)

def translate_line(alien_number, source_language, target_language):
    return from_decimal_list(from_decimal(to_decimal(to_decimal_list(alien_number, source_language), len(source_language)), len(target_language)), target_language)

def translate_file(file):
    o = open(file+".solv", "wb")
    i = open(file, "rb")
    lines = i.readline()
    for l in range(1, int(lines)+1):
        input = i.readline().split()
        o.write("Case #%d: %sn" % (l, translate_line(input[0], input[1], input[2])))

Solução por fnds


$fn = "A-small.html";

function to_decimal($str)
    $num_chr = strlen($str);
    $chr = str_split($str);
    for($i=0; $i<=$num_chr; $i++)
        $chr[$i] = $i;
    return $chr;

for($i=1; $i<=101; $i++)
    $conteudo = file($fn);
    $linha = $conteudo[$i];

    if($i == 1) $N = $linha;
    if($i > 1)
        $case[$i-1] = $linha;

        $str = split("[r ]+", $linha);
        $alien = $str[0];
        $source = $str[1];
        $target = $str[2];

        $base_source = strlen($source);
        $base_target = strlen($target);

        $source = to_decimal($source);


Solução por skin