40 lines · python
1import sys2from collections import OrderedDict3 4class Trie:5 def __init__(self, name):6 self.name = name7 self.children = OrderedDict()8 self.count = 19 10 def add(self, name):11 if name in self.children:12 self.children[name].count += 113 else:14 self.children[name] = Trie(name)15 return self.children[name]16 17 def print(self, depth):18 if depth > 0:19 print('|', end="")20 for i in range(depth):21 print('-', end="")22 if depth > 0:23 print(end=" ")24 print(self.name, '#', self.count)25 for key, child in self.children.items():26 child.print(depth + 1)27 28 29Root = Trie("Root")30 31if __name__ == "__main__":32 for line in sys.stdin:33 words = line.split('==>')34 words = [word.strip() for word in words]35 MyTrie = Root;36 for word in words:37 MyTrie = MyTrie.add(word)38 39 Root.print(0)40