Created
February 9, 2015 00:24
-
-
Save PirosB3/a36d9ef4ac58102439e9 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
from sys import stdin | |
from itertools import count | |
TRIE = {} | |
def insert_to_trie(trie, part): | |
if len(part) < 1: | |
return | |
first, last = part[0], part[1:] | |
if first not in trie.keys(): | |
trie[first] = {} | |
return insert_to_trie(trie[first], last) | |
def traverse(trie, part, count): | |
try: | |
first, last = part[0], part[1:] | |
except IndexError: | |
count[0] += 1 | |
return count | |
for char in first: | |
try: | |
traverse(trie[char], last, count) | |
except KeyError: | |
pass | |
return count | |
def process(line): | |
data = [] | |
inside = False | |
inside_data = [] | |
for ch in line: | |
if ch == '(': | |
inside = True | |
inside_data = [] | |
data.append(inside_data) | |
elif ch == ')': | |
inside = False | |
else: | |
if inside: | |
inside_data.append(ch) | |
else: | |
data.append([ch]) | |
result = [0] | |
traverse(TRIE, data, result) | |
return result[0] | |
def main(): | |
c = count() | |
c.next() | |
_, words, _ = map(int, stdin.readline().split(' ')) | |
for _ in xrange(words): | |
word = stdin.readline().strip() | |
insert_to_trie(TRIE, word) | |
for line in stdin.readlines(): | |
line = line.strip() | |
print "Case #%s: %s" % (c.next(), process(line)) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment