This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
class TrieNode:
def __init__(self):
self.children = {}
self.is_end_of_word = False
self.depth = 0
class Trie:
def __init__(self):
self.root = TrieNode()
def insert(self, word: str) -> None:
node = self.root
for char in word:
if char not in node.children:
node.children[char] = TrieNode()
node = node.children[char]
node.is_end_of_word = True
def starts_with(self, prefix: str) -> bool:
node = self.root
for char in prefix:
if char not in node.children:
return False
node = node.children[char]
return True
# import sys
# sys.stdin = open('1.in', 'r')
trie = Trie()
n = int(input())
for _ in range(n):
word = input()
trie.insert(word)
cnt = 0
def dfs1(node):
depth = 0
for child in node.children.values():
depth = max(depth, dfs1(child))
node.depth = depth + 1
return node.depth
def dfs2(node):
if node.is_end_of_word:
print('P')
global cnt
cnt += 1
if cnt == n:
exit()
for char, child in sorted(node.children.items(), key=lambda item: item[1].depth):
print(char)
dfs2(child)
print('-')
dfs1(trie.root)
dfs2(trie.root)
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |