Google Code Jam 2012: Qualification Problem 1


This weekend the qualification round of Google Code Jam 2012 took place. There were 4 problems, and you need to score at least 20 points to pass to the next round. This first problem was worth 15 points, and it was pretty trivial, so pretty much 15 free points for anyone who tried.

The Problem

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?

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.

Sample 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

Sample 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

My Solution

You simply had to create a mapping for the letters by studying the sample input and output. Below my C solution:

#include <stdio.h>

char translate(char x){
  switch(x){
    case 'a':
      return 'y';
    case 'b':
      return 'h';
    case 'c':
      return 'e';
    case 'd':
      return 's';
    case 'e':
      return 'o';
    case 'f':
      return 'c';
    case 'g':
      return 'v';
    case 'h':
      return 'x';
    case 'i':
      return 'd';
    case 'j':
      return 'u';
    case 'k':
      return 'i';
    case 'l':
      return 'g';
    case 'm':
      return 'l';
    case 'n':
      return 'b';
    case 'o':
      return 'k';
    case 'p':
      return 'r';
    case 'q':
      return 'z';
    case 'r':
      return 't';
    case 's':
      return 'n';
    case 't':
      return 'w';
    case 'u':
      return 'j';
    case 'v':
      return 'p';
    case 'w':
      return 'f';  
    case 'x':
      return 'm';  
    case 'y':
      return 'a';
    case 'z':
      return 'q';    
    default:
      return ' ';    
  }
}

int main(){
  int i,cases;
  char letter;

  scanf("%d",&cases);
  scanf("%c",&letter); /*discard first n*/

  for (i=0;i<cases;i++){
    printf("Case #%d: ",i+1);
    scanf("%c",&letter);
    while (letter!=10){
      printf("%c",translate(letter));
      scanf("%c",&letter);
    }
    if (i<cases-1)
      printf("n");
  }

return 0;
}

2 thoughts on “Google Code Jam 2012: Qualification Problem 1

  1. DataHero

    Those texts can easily be cracked with statistical analysis. It’s a simple monoalphabetic substitution. If the text is large enough, the frequency of letters in the ciphertext should correspond to the frequency of letters in English, given that the Enciphered language was english.

    So you can solve such things rather elegantly by counting letters and then correct the list afterwards.

    You wouldn’t want to know how powerful heuristical attacks are.

    Reply
  2. amazzal


    please take a look at this one that I wrote:-->a=z A=Z b=y y=b z=a Z=A<--
    #include
    #include
    int translate(char text[])
    {
    int h=0;
    while(text[h]!='')
    {
    if(text[h]>=110 && text[h]=97 && text[h]=90 && text[h]=65 && text[h]<78)
    {
    text[h]=('A'-text[h])+'Z';
    }
    h++;
    }
    return 0;
    }
    int main()
    {
    int t=0;
    char text[100];
    scanf("%d",&t);
    while(t!=0)
    {
    scanf("%s",&text);
    puts("the translate is:");
    translate(text);
    printf("%sn",text);
    puts("again will be:");
    translate(text);
    printf("%sn",text);
    t--;
    puts("------------------");
    }
    getche();
    return 0;
    }

    Reply

Leave a Reply

Your email address will not be published. Required fields are marked *