Skip to content

Instantly share code, notes, and snippets.

@wmantly
Created April 2, 2020 22:01
Show Gist options
  • Save wmantly/aa7e6c6e40be570281fe0b3efb43174e to your computer and use it in GitHub Desktop.
Save wmantly/aa7e6c6e40be570281fe0b3efb43174e to your computer and use it in GitHub Desktop.
__NUM = int(input())
class Trie(dict):
def __call__(self, command, word):
return getattr(self, command)(word)
def add(self, word):
current = self
for letter in word:
if current.get(letter) is not None:
current = current[letter]
else:
current[letter] = {}
current = current[letter]
current['\0'] = None
return ''
def find(self, word):
current = self
count = 0
for letter in word:
if current.get(letter) is not None:
current = current[letter]
else:
print(0)
return 0
nodes = [current]
while nodes:
current = nodes.pop(0)
if current.get('\0', False) == None :
count += 1
if current.keys() is not None:
for letter in current:
if current[letter]:
nodes.append(current[letter])
print(count)
return count
trie = Trie()
for i in range(__NUM):
trie(*input().strip().split())
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment