zoukankan      html  css  js  c++  java
  • {每日一题}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?

  • 相关阅读:
    zbb20171108 一台电脑启动多个 tomcat
    zbb20171101 oracle 启动 linux
    zbb20171017 svn Cleanup failed to process the following paths错误的解决
    zbb20171013 mysql服务重启 重启服务 重启mysql服务
    zbb20171013 mysql 远程连接 报错 1130-host ... is not allowed to connect to this MySql server
    zbb20171013 svnserver 修改默认端口
    zbb20171013 tomcat 设置访问ip地址直接访问项目
    zbb20171013 Windows 下端口占用 查询 以及结束进程的方法
    20171012 nginx 超时时间配置
    20171012 tomcat 超时时间配置
  • 原文地址:https://www.cnblogs.com/practice/p/2450034.html
Copyright © 2011-2022 走看看