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?

  • 相关阅读:
    原生Python机器学习分类之一Knn算法
    Java可视化文件(夹)加密解密压缩解压
    基于图搜索技术的八数码问题求解C++
    遗传算法解决TSP问题
    简单dp
    并查集
    KMP算法
    快速迭代
    为什么vs2017在代码右键上没有vs2013(第一个图)上实现抽象类这个选项?
    关于C#面向对象中的查看类图(没有此按键的问题)的解决方法 The solution to view class diagrams in C # object-oriented (without this key)
  • 原文地址:https://www.cnblogs.com/practice/p/2450034.html
Copyright © 2011-2022 走看看