# CodeJam 2009 Qualification A

This was my first Code Jam problem ever, and I was quite thrilled with the competition at the time! 😀 So I figured it would be the perfect introduction problem. You can access through the following link:

It is Alien Language, and it says:

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.

So, it means there is a translation expression and words that we must match, in order to know how many words belong to the  test case language. Ring any bells? Yes, regular expressions! So, all we need to do is write the text reader for the input rules, read the words and build the regular expression from the test case line, and count the matches!

I/O rules:

Input

The first line of input contains 3 integers, LD 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.

Output

For each test case, output:

`Case #X: K`

where X is the test case number, starting from 1, and K indicates how many words in the alien language match the pattern.

So, the following was produced:

```import re, string, sys

from array import *

ST_WORDS=2
ST_PATTERNS=3
ST_EXIT=4

cases = 0
casen = 1
wordnum = 0
wordlen = 0

words = list()
patterns = list()

while state != ST_EXIT :
line = raw_input()

state = ST_WORDS
elif state == ST_WORDS:
words.append(line)
if len(words) == wordnum:
state = ST_PATTERNS

elif state == ST_PATTERNS:
expr = ""
insor = False
justin = False

for i in range(len(line)):
if line[i] == '(':
expr += '('
insor = True
justin = True
elif line[i] == ')':
expr += ')'
insor = False
justin = False
else:
if insor == True:
if justin != True:
expr += '|'
justin = False

expr += line[i]

p = re.compile(expr)
matches = 0

for i in words:
if p.match(i) != None:
matches+=1

print "Case #%d: %d" % (casen,matches)
casen+=1

if casen-1 == cases:
state = ST_EXIT
else:
state = ST_EXIT

exit()
```