Created
February 10, 2015 16:00
-
-
Save PirosB3/29d9f5ab03587ba5c04f 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
import re | |
RE = re.compile('(\w+)') | |
def insert(current_tree, current_path, n=0): | |
if len(current_path) == 0: | |
return n | |
first, last = current_path[0], current_path[1:] | |
try: | |
next = current_tree[first] | |
except KeyError: | |
n += 1 | |
current_tree[first] = {} | |
next = current_tree[first] | |
return insert(next, last, n) | |
def main(): | |
with open('filesdata') as f: | |
total = int(f.readline()) | |
for n in xrange(total): | |
tree = {} | |
existing, to_create = map(int, f.readline().split(' ')) | |
for _ in xrange(existing): | |
paths = RE.findall(f.readline()) | |
insert(tree, paths) | |
total = 0 | |
for _ in xrange(to_create): | |
paths = RE.findall(f.readline()) | |
total += insert(tree, paths) | |
print "Case #%s: %s" % (n+1, total) | |
if __name__ == '__main__': | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment