Dashboard - Qualification Round 2012 - Google Code Jam
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?
Each line consists of a string G in Googlerese, made up of one or more words containing the letters 'a' - 'z'. There will be exactly one space (' ') character between consecutive words and no spaces at the beginning or at the end of any line.
G contains at most 100 characters.
None of the text is guaranteed to be valid English.
Official Analysis: https://code.google.com/codejam/contest/1460488/dashboard#s=a&a=0
http://samfoust.com/Blog/Post/problem-a-speaking-in-tongues
So the way to go about solving this is fairly easy since it is one to one and never changes so once you figure out a cypher you can just hardcode it and solve everything. Between the instructions and sample input output you are given every mapping except what q maps to and since its one to one it’s not hard to figure out what is missing.
http://google-tale.blogspot.com/2012/04/gcj-2012-speaking-in-tongues.html
Read full article from Dashboard - Qualification Round 2012 - Google Code Jam
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?
Solving this problem
Usually, Google Code Jam problems have 1 Small input and 1 Large input. This problem has only 1 Small input. Once you have solved the Small input, you have finished solving this problem.Input
The first line of the input gives the number of test cases, T. T test cases follow, one per line.Each line consists of a string G in Googlerese, made up of one or more words containing the letters 'a' - 'z'. There will be exactly one space (' ') character between consecutive words and no spaces at the beginning or at the end of any line.
Output
For each test case, output one line containing "Case #X: S" where X is the case number and S is the string that becomes G in Googlerese.Limits
1 ≤ T ≤ 30.G contains at most 100 characters.
None of the text is guaranteed to be valid English.
Official Analysis: https://code.google.com/codejam/contest/1460488/dashboard#s=a&a=0
"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."
There really is just one mapping, and the main challenge here is figuring out what it is. Fortunately, there is a lot we can learn from the sample input and output. For example, by looking at the first word in the first line, we know that "our" becomes "ejp" in Googlerese, so 'o' -> 'e', 'u' -> 'j', and 'r' -> 'p'. If you go through the entire text, you will that there is almost enough information to reconstruct the entire mapping:
'a' -> 'y' 'b' -> 'n' 'c' -> 'f' 'd' -> 'i' 'e' -> 'c' 'f' -> 'w' 'g' -> 'l' 'h' -> 'b' 'i' -> 'k' 'j' -> 'u' 'k' -> 'o' 'l' -> 'm' 'm' -> 'x' 'n' -> 's' 'o' -> 'e' 'p' -> 'v' 'q' -> ??? 'r' -> 'p' 's' -> 'd' 't' -> 'r' 'u' -> 'j' 'v' -> 'g' 'w' -> 't' 'x' -> 'h' 'y' -> 'a' 'z' -> ???
We just need to figure out how to translate 'q' and 'z'. But if you read the problem statement carefully, you will notice there was one more example we gave you! "a zoo" gets translated to "y qee". This means that 'z' gets mapped to 'q'.
So the way to go about solving this is fairly easy since it is one to one and never changes so once you figure out a cypher you can just hardcode it and solve everything. Between the instructions and sample input output you are given every mapping except what q maps to and since its one to one it’s not hard to figure out what is missing.
string input = "ejp mysljylc kd kxveddknmc re jsicpdrysi rbcpc ypc rtcsra dkh wyfrepkym veddknkmkrkcd de kr kd eoya kw aej tysr re ujdr lkgc jv";
string output = "our language is impossible to understand there are twenty six factorial possibilities so it is okay if you want to just give up";
Dictionary<char, char> cipher = new Dictionary<char, char>();
for (int i = 0; i < input.Length; i++)
{
if (cipher.ContainsKey(input[i])) continue;
cipher.Add(input[i], output[i]);
}
cipher.OrderBy(a => a.Key);
This will fill out they cypher with every match except q and z but if you remember in the instructions they gave us the mapping for z and it is q so that only leaves mapping from q and the only char left is z so not the cipher is complete.
string code = " yhesocvxduiglbkrztnwjpfmaq";
string alphabet = " abcdefghijklmnopqrstuvwxyz";
http://google-tale.blogspot.com/2012/04/gcj-2012-speaking-in-tongues.html
public String translate(String input) { | |
HashMap<Character, Character> dict = getDictionary(); | |
StringBuilder sbTranslatedPhrase = new StringBuilder(); | |
for(int i=0;i<input.length();i++) { | |
char c = input.charAt(i); | |
sbTranslatedPhrase.append((c == ' ') ? ' ' : dict.get(c).charValue()); | |
} | |
return sbTranslatedPhrase.toString(); | |
} |