http://images.clipartpanda.com/-acqeKGzoi.jpeg
Gator Engineering
Google Code Jam 2015
Copyright © 2008 W. W. Norton & Company.
All rights reserved.
1
http://images.clipartpanda.com/-acqeKGzoi.jpeg
Gator Engineering
Rules
Copyright © 2008 W. W. Norton & Company.
All rights reserved.
2
http://images.clipartpanda.com/-acqeKGzoi.jpeg
Gator Engineering
Sample questions
P1: Reverse words
Given a list of space separated words, reverse the orderof the words. Each line of text contains L lettersand W words. A line will only consist of letters andspace characters. There will be exactly one spacecharacter between each pair of consecutive words.
Copyright © 2008 W. W. Norton & Company.
All rights reserved.
3
http://images.clipartpanda.com/-acqeKGzoi.jpeg
Gator Engineering
P1: Reverse Words
Input
The first line of input gives the number of cases, N.N test cases follow. For each test case there will a lineof letters and space characters indicating a list of spaceseparated words. Spaces will not appear at the start orend of a line.
Output
For each test case, output one line containing "Case #x:" followed by the list of words in reverse order.
Copyright © 2008 W. W. Norton & Company.
All rights reserved.
4
http://images.clipartpanda.com/-acqeKGzoi.jpeg
Gator Engineering
P1: Reverse Words
Limits
Small dataset
N = 51 ≤ L ≤ 25
Large dataset
N = 1001 ≤ L ≤ 1000
Copyright © 2008 W. W. Norton & Company.
All rights reserved.
5
http://images.clipartpanda.com/-acqeKGzoi.jpeg
Gator Engineering
P1: Reverse Words
Sample
Copyright © 2008 W. W. Norton & Company.
All rights reserved.
6
Input 
Output 
3this is a testfoobarall your base
Case #1: test a is thisCase #2: foobarCase #3: base your all
http://images.clipartpanda.com/-acqeKGzoi.jpeg
Gator Engineering
P2: T9 Spelling
Problem
The Latin alphabet contains 26 characters andtelephones only have ten digits on the keypad. Wewould like to make it easier to write a message toyour friend using a sequence of keypresses toindicate the desired characters. The letters aremapped onto the digits as shown below. To insertthe character B for instance, the program wouldpress 22. In order to insert two characters insequence from the same key, the user must pausebefore pressing the key a second time. The spacecharacter ' ' should be printed to indicate a pause. Forexample, 2 2 indicates AA whereas 22 indicates B.
Copyright © 2008 W. W. Norton & Company.
All rights reserved.
7
https://code.google.com/codejam/contest/images/?image=keypad.png&p=379101&c=351101
http://images.clipartpanda.com/-acqeKGzoi.jpeg
Gator Engineering
P2: T9 Spelling
Input
The first line of input gives the number of cases, NNtest cases follow. Each case is a line of text formattedas desired_message
Each message will consist of only lowercase charactersa-z and space characters ' '. Pressing zero emits a space.
Output
For each test case, output one line containing "Case #x:" followed by the message translated into the sequenceof keypresses.
Copyright © 2008 W. W. Norton & Company.
All rights reserved.
8
http://images.clipartpanda.com/-acqeKGzoi.jpeg
Gator Engineering
P2: T9 Spelling
Limits
1 ≤ N ≤ 100.
Small dataset
1 ≤ length of message in characters ≤ 15.
Large dataset
1 ≤ length of message in characters ≤ 1000.
Copyright © 2008 W. W. Norton & Company.
All rights reserved.
9
http://images.clipartpanda.com/-acqeKGzoi.jpeg
Gator Engineering
P2: T9 Spelling
Sample
Copyright © 2008 W. W. Norton & Company.
All rights reserved.
10
Input 
Output 
4hiyesfoo  barhello world
Case #1: 44 444Case #2: 999337777Case #3: 333666 6660 022 2777Case #4: 4433555555666096667775553
https://code.google.com/codejam/contest/images/?image=keypad.png&p=379101&c=351101
http://images.clipartpanda.com/-acqeKGzoi.jpeg
Gator Engineering
Copyright © 2008 W. W. Norton & Company.
All rights reserved.
11
Chapter 12
Pointers and Arrays (Continued)
http://images.clipartpanda.com/-acqeKGzoi.jpeg
Gator Engineering
Using an Array Name as a Pointer
Pointer arithmetic is one way in which arrays andpointers are related.
Another key relationship:
The name of an array can be used as a pointer tothe first element in the array.
This relationship simplifies pointer arithmetic andmakes both arrays and pointers more versatile.
Copyright © 2008 W. W. Norton & Company.
All rights reserved.
12
http://images.clipartpanda.com/-acqeKGzoi.jpeg
Gator Engineering
Using an Array Name as a Pointer
Suppose that a is declared as follows:
int a[10];
Examples of using a as a pointer:
*a = 7;   /* stores 7 in a[0] */
*(a+1) = 12;   /* stores 12 in a[1] */
In general, a + i is the same as &a[i].
Both represent a pointer to element i of a.
Also, *(a+i) is equivalent to a[i].
Both represent element i itself.
Copyright © 2008 W. W. Norton & Company.
All rights reserved.
13
http://images.clipartpanda.com/-acqeKGzoi.jpeg
Gator Engineering
Using an Array Name as a Pointer
The fact that an array name can serve as a pointermakes it easier to write loops that step through anarray.
Original loop:
for (p = &a[0]; p < &a[N]; p++)
  sum += *p;
Simplified version:
for (p = a; p < a + N; p++)
  sum += *p;
Copyright © 2008 W. W. Norton & Company.
All rights reserved.
14
http://images.clipartpanda.com/-acqeKGzoi.jpeg
Gator Engineering
Using an Array Name as a Pointer
Although an array name can be used as a pointer,it’s not possible to assign it a new value.
Attempting to make it point elsewhere is an error:
while (*a != 0)
  a++;           /*** WRONG ***/
This is no great loss; we can always copy a into apointer variable, then change the pointer variable:
p = a;
while (*p != 0)
  p++;
Copyright © 2008 W. W. Norton & Company.
All rights reserved.
15
http://images.clipartpanda.com/-acqeKGzoi.jpeg
Gator Engineering
Array Arguments (Revisited)
When passed to a function, an array name is treated as a pointer.
Example:
int find_largest(int a[], int n)
{
  int i, max;
 
  max = a[0];
  for (i = 1; i < n; i++)
    if (a[i] > max)
      max = a[i];
  return max;
}
A call of find_largest:
largest = find_largest(b, N);
This call causes a pointer to the first element of b to be assignedto a; the array itself isn’t copied.
Copyright © 2008 W. W. Norton & Company.
All rights reserved.
16
http://images.clipartpanda.com/-acqeKGzoi.jpeg
Gator Engineering
Array Arguments (Revisited)
The fact that an array argument is treated as apointer has some important consequences.
Consequence 1: When an ordinary variable ispassed to a function, its value is copied; anychanges to the corresponding parameter don’taffect the variable.
In contrast, an array used as an argument isn’tprotected against change.
Copyright © 2008 W. W. Norton & Company.
All rights reserved.
17
http://images.clipartpanda.com/-acqeKGzoi.jpeg
Gator Engineering
Array Arguments (Revisited)
For example, the following function modifies anarray by storing zero into each of its elements:
void store_zeros(int a[], int n)
{
  int i;
 
  for (i = 0; i < n; i++)
    a[i] = 0;
}
Copyright © 2008 W. W. Norton & Company.
All rights reserved.
18
http://images.clipartpanda.com/-acqeKGzoi.jpeg
Gator Engineering
Array Arguments (Revisited)
To indicate that an array parameter won’t bechanged, we can include the word const in itsdeclaration:
int find_largest(const int a[], int n)
{
  …
}
If const is present, the compiler will check thatno assignment to an element of a appears in thebody of find_largest.
Copyright © 2008 W. W. Norton & Company.
All rights reserved.
19
http://images.clipartpanda.com/-acqeKGzoi.jpeg
Gator Engineering
Array Arguments (Revisited)
Consequence 2: The time required to pass an arrayto a function doesn’t depend on the size of thearray.
There’s no penalty for passing a large array, sinceno copy of the array is made.
Copyright © 2008 W. W. Norton & Company.
All rights reserved.
20
http://images.clipartpanda.com/-acqeKGzoi.jpeg
Gator Engineering
Array Arguments (Revisited)
Consequence 3: An array parameter can bedeclared as a pointer if desired.
find_largest could be defined as follows:
int find_largest(int *a, int n)
{
  …
}
Declaring a to be a pointer is equivalent todeclaring it to be an array; the compiler treats thedeclarations as though they were identical.
Copyright © 2008 W. W. Norton & Company.
All rights reserved.
21
http://images.clipartpanda.com/-acqeKGzoi.jpeg
Gator Engineering
Array Arguments (Revisited)
Although declaring a parameter to be an array isthe same as declaring it to be a pointer, the sameisn’t true for a variable.
The following declaration causes the compiler toset aside space for 10 integers:
int a[10];
The following declaration causes the compiler toallocate space for a pointer variable:
int *a;
Copyright © 2008 W. W. Norton & Company.
All rights reserved.
22
http://images.clipartpanda.com/-acqeKGzoi.jpeg
Gator Engineering
Array Arguments (Revisited)
In the latter case, a is not an array; attempting touse it as an array can have disastrous results.
For example, the assignment
*a = 0;   /*** WRONG ***/
will store 0 where a is pointing.
Since we don’t know where a is pointing, theeffect on the program is undefined. 
Copyright © 2008 W. W. Norton & Company.
All rights reserved.
23
http://images.clipartpanda.com/-acqeKGzoi.jpeg
Gator Engineering
Array Arguments (Revisited)
Consequence 4: A function with an arrayparameter can be passed an array “slice”—asequence of consecutive elements.
An example that applies find_largest toelements 5 through 14 of an array b:
largest = find_largest(&b[5], 10);
Copyright © 2008 W. W. Norton & Company.
All rights reserved.
24
http://images.clipartpanda.com/-acqeKGzoi.jpeg
Gator Engineering
Using a Pointer as an Array Name
C allows us to subscript a pointer as though itwere an array name:
#define N 10
int a[N], i, sum = 0, *p = a;
for (i = 0; i < N; i++)
  sum += p[i];
The compiler treats p[i] as *(p+i).
Copyright © 2008 W. W. Norton & Company.
All rights reserved.
25