Chinaunix首页 | 论坛 | 博客
  • 博客访问: 45371
  • 博文数量: 7
  • 博客积分: 1410
  • 博客等级: 上尉
  • 技术积分: 195
  • 用 户 组: 普通用户
  • 注册时间: 2006-12-27 19:37
文章分类
文章存档

2009年(7)

我的朋友
最近访客

分类: Python/Ruby

2009-08-18 22:42:31

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



import os

fileIn=open('A-large-practice.in','r')
fileOut=open("A-large-practive.out","w")
recordlen=fileIn.readline()

print recordlen

i=0

for i in range(int(recordlen)):
    line=fileIn.readline()
    line=line.split()
    c=line[0]
    source=line[1]
    dest=line[2]
   

    valueOfC=0

    for i in range(len(c)):
        valueOfC=valueOfC*len(source)
        valueOfC=valueOfC+source.find(c[i])

    print valueOfC

    result=''

    while valueOfC>0:
        t=valueOfC%len(dest)
        result=dest[t]+result
        valueOfC=valueOfC/len(dest)
    fileOut.write("Case #"+str(i+1)+":"+result+'\n')
fileIn.close()
fileOut.close()

阅读(447) | 评论(0) | 转发(0) |
0

上一篇:没有了

下一篇:Practice-Saving the Universe

给主人留下些什么吧!~~