说好的每日一题时间又到了~
昨天是今年的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 |
Output |
Case #1: our language is impossible to understand |
解答:根据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?