{每日一题}Google Code Jam 2012 Qualification Round - Problem A解答(python版)

说好的每日一题时间又到了~

昨天是今年的Google Code Jam的Qualification Round,由于网络问题菜鸟康从晚上9点才开始答题,pass了problem A & B, 由于时间太晚头脑混沌,把problem C & D留作practice。

Problem A - Speaking in Tongues (Google提供的官方解答)

题目背景:在Google有一种叫做“Googlerese”的语言,要将Googlerese与正常的英文字符间存在“一一对应”的关系,例如存在以下的对应关系:'a' -> 'y', 'o' -> 'e', 'z' -> 'q',空格和空格对应. 例子:"a zoo" 会被翻译成 "y qee". 任务是要将input file中的字符从googlerese翻译成正常的英文句子。

另外还提供了以下test case

Input
3
ejp mysljylc kd kxveddknmc re jsicpdrysi
rbcpc ypc rtcsra dkh wyfrepkym veddknkmkrkcd
de kr kd eoya kw aej tysr re ujdr lkgc jv


Output
Case #1: our language is impossible to understand
Case #2: there are twenty six factorial possibilities
Case #3: so it is okay if you want to just give up

解答:根据test case找到字母间的对应关系,保存在dict结构中,从input file读出字符串后,遍历该字符串,并查找dict,得到其翻译后的对应字符。

if __name__=="__main__":
    iFile = open("G:/usaco/A-small-attempt1.in", 'r')
    oFile = open("G:/usaco/A-small-attempt1.out", 'w')
    num = (int)(iFile.readline())
    rules = {'q':'z', 'z':'q', ' ': ' ', 'a': 'y', 'c': 'e', 'b': 'h', 'e': 'o', 'd': 's', 'g': 'v', 'f': 'c', 'i': 'd', 'h': 'x', 'k': 'i', 'j': 'u', 'm': 'l', 'l': 'g', 'o': 'k', 'n': 'b', 'p': 'r', 's': 'n', 'r': 't', 'u': 'j', 't': 'w', 'w': 'f', 'v': 'p', 'y': 'a', 'x': 'm'}
    for i in range(0, num):
        G = iFile.readline().strip('\n').strip('\r')
        G = "".join(G)
        N = "Case #"+str((i+1))+": "
        for j in range(0, len(G)):
            N = N+rules[G[j]]
        N = N+'\n'
        oFile.write(N)
        print(N)
    iFile.close()
    oFile.close()

附Problem A原题

We have come up with the best possible language here at Google, called Googlerese. To translate text into Googlerese, we take any message and replace each English letter with another English letter. This mapping is one-to-one and onto, which means that the same input letter always gets replaced with the same output letter, and different input letters always get replaced with different output letters. A letter may be replaced by itself. Spaces are left as-is.

For example (and here is a hint!), our awesome translation algorithm includes the following three mappings: 'a' -> 'y', 'o' -> 'e', and 'z' -> 'q'. This means that "a zoo" will become "y qee".

Googlerese is based on the best possible replacement mapping, and we will never change it. It will always be the same. In every test case. We will not tell you the rest of our mapping because that would make the problem too easy, but there are a few examples below that may help.

Given some text in Googlerese, can you translate it to back to normal text?

转载于:https://www.cnblogs.com/practice/archive/2012/04/15/2450034.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值