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?

  • 相关阅读:
    ResGen.exe 生成resources文件方法 [转]
    C#【Winform】带参启动外部EXE
    SBO的5个开发原则机遇只给有准备的人[转]
    在SQL中插入临时表时使用自动增长的数据字段
    c# 强制退出
    C#实现SQL全库检索数据比较使用DataReader与DataAdapter+Datatable效率,差距惊人!
    推荐一个C#代码混淆器 .NET Reactor
    面向对象软件设计——设计模式学习
    AbstarctFactory模式——设计模式学习
    插入排序算法(直接,折半,希尔)
  • 原文地址:https://www.cnblogs.com/practice/p/2450034.html
Copyright © 2011-2022 走看看