Skip to content

Instantly share code, notes, and snippets.

@PirosB3
Created February 10, 2015 16:00
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save PirosB3/29d9f5ab03587ba5c04f to your computer and use it in GitHub Desktop.
Save PirosB3/29d9f5ab03587ba5c04f to your computer and use it in GitHub Desktop.
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