이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
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... |