| 1 |
import sys |
| 2 |
from collections import OrderedDict |
| 3 |
|
| 4 |
class Trie: |
| 5 |
def __init__(self, name): |
| 6 |
self.name = name |
| 7 |
self.children = OrderedDict() |
| 8 |
self.count = 1 |
| 9 |
|
| 10 |
def add(self, name): |
| 11 |
if name in self.children: |
| 12 |
self.children[name].count += 1 |
| 13 |
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 |
|
| 29 |
Root = Trie("Root") |
| 30 |
|
| 31 |
if __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) |