`
utensil
  • 浏览: 150465 次
  • 性别: Icon_minigender_1
  • 来自: 深圳
社区版块
存档分类
最新评论

Google Code Jam之Alien Numbers之我的解答

阅读更多
由于时间的限制,程序有些地方的容错性不够,以//!! 标出。
运行成功,经Google Code Jam鉴定为正确。
题目为:
 
Alien Numbers
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 ≤ 10, 2 ≤ num digits in target_language ≤ 10.

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 9 0123456789 oF8 Foo oF8 0123456789 13 0123456789abcdef 01 CODE O!CDE? A?JM!.

Case #1: Foo Case #2: 9 Case #3: 10011 Case #4: JAM!
我的解答的源代码:

#include <iostream>

#include <fstream>

#include <iterator>

 

using namespace std;

 

//A helper function to improve performance of power, basically copied from <stl_numeric.h> of SGI STL

inline long power(long x, long n)

{

    if(n == 0) return 1;

    else

    {

        while((n & 1) == 0)

        {

            n >>= 1;

            x *= x;

        }

 

        long y = x;

        n >>=1;

        while(n != 0)

        {

            x *= x;

            if((n & 1) != 0)

                y *= x;

            n >>=1;

        }

        return y;

    }

}

 

int main(int argc, char* argv[])

{

    //Only for memerizing usage for myself

    if(argc != 3)

    {

        cout << "Usage: nstrans INPUT_FILE OUTPUT_FILE" << endl;

        return 0;

    }

 

    ifstream fin(argv[1]); //!!

    ofstream fout(argv[2]); //!!

 

    //How many cases in total?

    int case_max;

    fin >> case_max;

    fin.ignore(); // ''

 

    string source_number;

    string source_language;

    string target_language;

    string target_number;

 

    int source_digits; //How many digits in source_number?

    int source_base; //How many digits in source_language?

    int target_base; //How many digits in target_language?

 

    long num; //alien number in demical, since it's less than 1000000000, it's in long's range----assert(sizeof(long) >= 4)

 

    char tmp; //for storing a temp char

 

    for(int cur_case = 1; cur_case <= case_max; cur_case++)

    {

        //some cleanup

        num = 0;

        source_number = source_language = target_language = target_number = "";

 

        //tear the case apart

        while((tmp = fin.get()) != ' ') source_number += tmp; //!!

        while((tmp = fin.get()) != ' ') source_language += tmp; //!!

        while((tmp = fin.get()) != '') target_language += tmp; //!!

 

        //

        source_digits = source_number.length();

        source_base = source_language.length();

        target_base = target_language.length();

 

        int e; //exponent

 

        //source number to decimal value

        for(e = 0; e < source_digits; e++)

        {

            num += source_language.find((source_number[source_digits-1 - e])) * power(source_base, e);

        }

 

        #ifdef MY_DEBUG

            cout << source_number << ' ' << num <<' ';

        #endif

 

        //decimal value to target number(reversal order)

        e = 1;

        while(num > 0)

        {

            target_number += target_language[num % target_base];

            num /= target_base;

        }

 

        fout << "Case #" << cur_case << ": ";

 

        //reverse the target number and output

        ostream_iterator<char> foutit(fout);

        copy(target_number.rbegin(), target_number.rend(), foutit);

 

        fout << endl;

 

        #ifdef MY_DEBUG

        ostream_iterator<char> outit(cout);

        copy(target_number.rbegin(), target_number.rend(), outit);

        cout << endl;

        #endif

    }

 

    return 0;

}
 
分享到:
评论

相关推荐

    alien_8.79.tar.gz

    Alien alien.lsm alien.lsm.in alien.pl alien.spec alien.spec.in debian gendiff.txt GPL INSTALL Makefile.PL README TODO [root@yun alien]# ./alien.pl You must specify a file to convert. Usage: alien ...

    Alien

    Alien

    linux Alien 安装方法

    Alien lunbunt 下的 安装apm

    Ubuntu 10.4 Alien Download

    由于Ubuntu系统无法使用yum来安装包,所以需要用alien命令来安装rpm包,详细安装方式如下: sudo alien -i MySQL.rpm

    alien_8.78.tar.gz

    alien_8.78.tar.gz tar -xvzf alien_8.78.tar.gz cd alien make make install

    Alien_Invasion.7z

    本游戏基本上参(zhao)考(ban)了《Python编程从入门到实践》12到13章的...点一下Alien_Invasion.exe就运行啦,Alien_Invasion_code里面有源码~ 只是一个小小白的小小白作品(大部分还是照搬的嘿嘿),只为搏君一笑~

    Alien Product Family Overview

    Alien Product Family Overview,介绍Alien Product 。At Alien® we are proud of the quality of our readers and offer warranty options to back them up. All Alien RFID readers are warranted against ...

    Native Alien

    Native Alien

    Alienware-OC-Controls-Application

    外星人灯光控制插件。Alienware-OC-Controls-Application。

    AlienSkin.Eye.Candy5

    Alien Skin 公司最新的力作 Eye Candy 5 Textures 滤镜, 功能实在强大。制作皮毛(Formerly Fur),砖块(brick wall),花纹板(Diamond Plate),大理石(Marble ),蛇皮(Reptile Skin),石材(Stone Wall),...

    alien_8.93.tar.gz

    alien_8.93.tar.gz from debian

    Alien 源码包,软件包转换器

    Alien 是一个在 rpm、dpkg、stampede slp 和 Slackware tgz 文件格式之间进行转换的程序。 如果你想使用来自另一个发行版的包而不是你系统上安装的包,你可以使用 alien 将它转换为你喜欢的包格式并安装它。

    编译好的lua alien模块,包括5.1版本和5.4版本

    编译好的lua alien模块,包括5.1版本和5.4版本 lua可以通过alien模块访问C动态库, 也可以访问系统api。 适用操作系统: linux 适用lua版本:lua5.1, lua5.4

    alien_invasion.zip

    alien_invasion.zip

    Alienware-Command-Center-Application

    外星人灯光控制插件。Alienware-Command-Center-Application。

    alien

    alien

    Alienware屏保

    Alienware屏保

    Alienware theme.rar

    ALIENWARE电脑主题包内含12张高质量的Alienware壁纸,修改过的光标和自定义桌面图标和色彩方案。Alienware主题包支持Windows 7,Windows 8 / 8.1和Windows 10。 卓越设计和标志的戴尔外星人品牌。拥有数年的原创...

    简单的alien invasion的飞船设置文件

    简单的alien invasion的飞船设置文件,简单的alien invasion的飞船设置文件简单的alien invasion的飞船设置文件简单的alien invasion的飞船设置文件简单的alien invasion的飞船设置文件

Global site tag (gtag.js) - Google Analytics