This was the first round of Google Code Jam 2009, and what an exciting round it was! 8600 people solved at least one problem, and about 7830 of those people solved a problem fully and got to advance to Round 1. 2420 people solved all three problems, and 3080 were properly welcomed to Code Jam.
The round was marred by an outage early on: a nearly two-hour period in which contestants would often get error messages when they tried to download an input. To make up for that we extended the round by two hours, and offer our sincerest apologies to everyone affected.
Over 10,000 contestants, representing 111 countries, used 40 languages to solve the problems. We hope you enjoyed the problems, and we look forward to seeing 7830 of you in Round 1!
Credits
Problem A. Alien Language Written by Junbin Teng. Prepared by Marius Andrei.
Problem B. Watersheds Written by Andrew Gove. Prepared by Xiaomin Chen and Igor Naverniouk.
Problem C. Welcome to Code Jam Written by Xiaomin Chen. Prepared by Xiaomin Chen and Bartholomew Furrow.
Contest analysis presented by Bartholomew Furrow, Marius Andrei, Igor Naverniouk, and Xiaomin Chen.
After years of study, scientists at Google Labs have discovered an alien language transmitted from a faraway planet. The alien language is very unique in that every word consists of exactly L lowercase letters. Also, there are exactly D words in this language.
Once the dictionary of all the words in the alien language was built, the next breakthrough was to discover that the aliens have been transmitting messages to Earth for the past decade. Unfortunately, these signals are weakened due to the distance between our two planets and some of the words may be misinterpreted. In order to help them decipher these messages, the scientists have asked you to devise an algorithm that will determine the number of possible interpretations for a given pattern.
A pattern consists of exactly L tokens. Each token is either a single lowercase letter (the scientists are very sure that this is the letter) or a group of unique lowercase letters surrounded by parenthesis ( and ). For example: (ab)d(dc) means the first letter is either a or b, the second letter is definitely d and the last letter is either d or c. Therefore, the pattern (ab)d(dc) can stand for either one of these 4 possibilities: add, adc, bdd, bdc.
The first line of input contains 3 integers, L, D and N separated by a space. D lines follow, each containing one word of length L. These are the words that are known to exist in the alien language. N test cases then follow, each on its own line and each consisting of a pattern as described above. You may assume that all known words provided are unique.
For each test case, output
Case #X: Kwhere X is the test case number, starting from 1, and K indicates how many words in the alien language match the pattern.
Time limit: 20 seconds per test set.
Memory limit: 1 GB.
1 ≤ L ≤ 10
1 ≤ D ≤ 25
1 ≤ N ≤ 10
1 ≤ L ≤ 15
1 ≤ D ≤ 5000
1 ≤ N ≤ 500
3 5 4 abc bca dac dbc cba (ab)(bc)(ca) abc (abc)(abc)(abc) (zyx)bc
Case #1: 2 Case #2: 1 Case #3: 3 Case #4: 0
Geologists sometimes divide an area of land into different regions based on where rainfall flows down to. These regions are called drainage basins.
Given an elevation map (a 2-dimensional array of altitudes), label the map such that locations in the same drainage basin have the same label, subject to the following rules.
The first line of the input file will contain the number of maps, T. T maps will follow, each starting with two integers on a line -- H and W -- the height and width of the map, in cells. The next H lines will each contain a row of the map, from north to south, each containing W integers, from west to east, specifying the altitudes of the cells.
For each test case, output 1+H lines. The first line must be of the form
Case #X:where X is the test case number, starting from 1. The next H lines must list the basin labels for each of the cells, in the same order as they appear in the input.
Memory limit: 1 GB.
T ≤ 100;
Time limit: 25 seconds.
1 ≤ H, W ≤ 10;
0 ≤ altitudes < 10.
There will be at most two basins.
Time limit: 30 seconds.
1 ≤ H, W ≤ 100;
0 ≤ altitudes < 10,000.
There will be at most 26 basins.
4 3 3 9 6 3 5 9 6 3 5 9 1 10 0 1 2 3 4 5 6 7 8 7 2 3 7 6 7 7 6 7 5 5 1 2 3 4 5 2 9 3 9 6 3 3 0 8 7 4 9 8 9 8 5 6 7 8 9
Case #1: a b b a a b a a a Case #2: a a a a a a a a a b Case #3: a a a b b b Case #4: a a a a a a a b b a a b b b a a b b b a a a a a a
In Case #1, the upper-right and lower-left corners are sinks. Water from the diagonal flows towards the lower-left because of the lower altitude (5 versus 6).
So you've registered. We sent you a welcoming email, to welcome you to code jam. But it's possible that you still don't feel welcomed to code jam. That's why we decided to name a problem "welcome to code jam." After solving this problem, we hope that you'll feel very welcome. Very welcome, that is, to code jam.
If you read the previous paragraph, you're probably wondering why it's there. But if you read it very carefully, you might notice that we have written the words "welcome to code jam" several times: 400263727 times in total. After all, it's easy to look through the paragraph and find a 'w'; then find an 'e' later in the paragraph; then find an 'l' after that, and so on. Your task is to write a program that can take any text and print out how many times that text contains the phrase "welcome to code jam".
To be more precise, given a text string, you are to determine how many times the string "welcome to code jam" appears as a sub-sequence of that string. In other words, find a sequence s of increasing indices into the input string such that the concatenation of input[s[0]], input[s[1]], ..., input[s[18]] is the string "welcome to code jam".
The result of your calculation might be huge, so for convenience we would only like you to find the last 4 digits.
The first line of input gives the number of test cases, N. The next N lines of input contain one test case each. Each test case is a single line of text, containing only lower-case letters and spaces. No line will start with a space, and no line will end with a space.
For each test case, "Case #x: dddd", where x is the case number, and dddd is the last four digits of the answer. If the answer has fewer than 4 digits, please add zeroes at the front of your answer to make it exactly 4 digits long.
Time limit: 20 seconds per test set.
Memory limit: 1 GB.
3 elcomew elcome to code jam wweellccoommee to code qps jam welcome to codejam
Case #1: 0001 Case #2: 0256 Case #3: 0000
For each cell, we need to determine its eventual sink. Then, to each group of cells that share the same sink, we need to assign a unique label.
The inputs to this problem are small enough for a simple brute-force simulation algorithm. Start with a cell and trace the path that water would take by applying the water flow rules repeatedly. Here is one possible solution in Python.
import sys def ReadInts(): return list(map(int, sys.stdin.readline().strip().split(" "))) def Cross(a, b): for i in a: for j in b: yield (i, j) def Neighbours(ui, uj, m, n): if ui - 1 >= 0: yield (ui - 1, uj) if uj - 1 >= 0: yield (ui, uj - 1) if uj + 1 < n: yield (ui, uj + 1) if ui + 1 < m: yield (ui + 1, uj) N = ReadInts()[0] for prob in xrange(1, N + 1): # Read the map (m, n) = ReadInts() maze = [ReadInts() for _ in xrange(m)] answer = [["" for _ in xrange(n)] for _ in xrange(m)] # The map from sinks to labels. label = {} next_label = 'a' # Brute force each cell. for (ui, uj) in Cross(xrange(m), xrange(n)): (i, j) = (-1, -1) (nexti, nextj) = (ui, uj) while (i, j) != (nexti, nextj): (i, j) = (nexti, nextj) for (vi, vj) in Neighbours(i, j, m, n): if maze[vi][vj] < maze[nexti][nextj]: (nexti, nextj) = (vi, vj) # Cell (ui, uj) drains to (i, j). if (i, j) not in label: label[(i, j)] = next_label next_label = chr(ord(next_label) + 1) answer[ui][uj] = label[(i, j)] # Output the labels. print "Case #%d:" % prob for i in xrange(m): print " ".join(answer[i])
In this problem, every one is welcomed by a (somehow) standard dynamic programming problem.
The word we want to find is S = "welcome to code jam", in a long string T. In fact the solution is not very different when we want to find any S. It is actually illustrative to picture the cases for short words.
In case S is just a single character, you just need to count how many times this character appears in T. If S = "xy" is a string of length 2, instead of brute force all the possible positions, one can do it in linear time, start from left to the right. For each occurrence of 'y', one needs to know how many 'x's appeared before that 'y'.
The general solution follows this pattern. Let us again use S = "welcome to code jam" as an example. The formal solution will be clear from the example; and you can always download the good solutions (with nice programming techniques) from the scoreboard.
So, let us define, for each position i in T, T(i) to be the string consists of the first i characters of T. And write
That's it. Welcome to Code Jam; and we hoped you enjoyed this round.