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 #whereX:K

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

Sample Input

3 5 4 abc bca dac dbc cba (ab)(bc)(ca) abc (abc)(abc)(abc) (zyx)bc

Sample Output

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.

- From each cell, water flows down to at most one of its 4 neighboring cells.
- For each cell, if none of its 4 neighboring cells has a lower altitude than the current cell's, then the water does not flow, and the current cell is called a
*sink*. - Otherwise, water flows from the current cell to the neighbor with the lowest altitude.
- In case of a tie, water will choose the first direction with the lowest altitude from this list: North, West, East, South.

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 #whereX:

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.

Sample Input

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

Sample Output

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.

Sample Input

3 elcomew elcome to code jam wweellccoommee to code qps jam welcome to codejam

Sample Output

Case #1: 0001 Case #2: 0256 Case #3: 0000

First we store all the words in a 2 dimensional array.

After that, we read each pattern, parse it, and count how many words match.

One possible way of storing a pattern is a 2 dimensional array P[15][26]. P[i][j] is True only if the**i**-th token contains the **j**-th letter of the alphabet, otherwise False. In other words, P[i] is a bitmap of the letters contained by the **i**-th token.

Parsing can be done like this:

- read one character**c**

- if**c** is '(', read characters until we hit ')'. The characters read are the token.

else the token is the character**c**

- populate P[i] for the characters in the token

To count how many words match, we make sure that each letter**i** from the word is contained in the bitmap P[i].

Total complexity is O(N * L * D).

In some programming languages this can solved by transforming the pattern into a regular expression. For instance in python replace '(' and ')' with '[' and ']'.

After that, we read each pattern, parse it, and count how many words match.

One possible way of storing a pattern is a 2 dimensional array P[15][26]. P[i][j] is True only if the

Parsing can be done like this:

- read one character

- if

else the token is the character

- populate P[i] for the characters in the token

To count how many words match, we make sure that each letter

Total complexity is O(N * L * D).

In some programming languages this can solved by transforming the pattern into a regular expression. For instance in python replace '(' and ')' with '[' and ']'.

Test Data

We recommend that you practice debugging solutions without looking at the test data.

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])

Test Data

We recommend that you practice debugging solutions without looking at the test data.

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

- Dp[i,1]: How many times we can find "w" in
**T**?^{(i)} - Dp[i,2]: How many times we can find "we" in
**T**?^{(i)} - Dp[i,3]: How many times we can find "wel" in
**T**?^{(i)} - Dp[i,4]: How many times we can find "welc" in
**T**?^{(i)} - ...
- Dp[i,18]: How many times we can find "welcome to code ja" in
**T**?^{(i)} - Dp[i,19]: How many times we can find "welcome to code jam" in
**T**?^{(i)}

- If the (i+1)-th character of
**T**is not 'c', then Dp[i+1,4] = Dp[i,4]. - If the (i+1)-th character of
**T**is 'c', then we can include all the "welc"s found in**T**, as well as those "welc"s ends exactly on the (i+1)-th character, so Dp[i+1,4] = Dp[i,4] + Dp[i,3].^{(i)}

That's it. Welcome to Code Jam; and we hoped you enjoyed this round.

Test Data

We recommend that you practice debugging solutions without looking at the test data.