Last active
October 30, 2016 22:25
-
-
Save coderek/f02be5568aab6da788cc09ec61ebe784 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
# https://code.google.com/codejam/contest/dashboard?c=635101#s=p0 | |
def solve(inputs): | |
itr = iter(input) | |
a = int(itr.next()) | |
for i in range(a): | |
m, n = map(lambda c: int(c), itr.next().split(' ')) | |
exists = [itr.next() for j in range(m)] | |
targets = [itr.next() for k in range(n)] | |
print 'Case #{}: {}'.format(i+1, _solve(exists, targets)) | |
class Node: | |
val = None | |
def __init__(self, val): | |
self.val = val | |
self.children = [] | |
def __eq__(self, other): | |
return other.val == self.val | |
def __repr__(self): | |
return self.val | |
def print_tree(tree): | |
e = [tree] | |
while e: | |
p = e.pop() | |
print p | |
e.extend(p.children) | |
def build(exists): | |
tree = Node('/') | |
for e in exists: | |
current = tree | |
parts = filter(lambda l: l!= '', e.split('/')) | |
for p in parts: | |
n = Node(p) | |
try: | |
i = current.children.index(n) | |
current = current.children[i] | |
except: | |
current.children.append(n) | |
current = n | |
return tree | |
def makedir(tree, t): | |
parts = filter(lambda l: l!= '', t.split('/')) | |
current = tree | |
count = 0 | |
for p in parts: | |
n = Node(p) | |
try: | |
i = current.children.index(n) | |
current = current.children[i] | |
except: | |
current.children.append(n) | |
count += 1 | |
current = n | |
return count | |
def _solve(exists, targets): | |
tree = build(exists) | |
total = 0 | |
for t in targets: | |
total += makedir(tree, t) | |
return total | |
solve(input) | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment