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 trie:
def __init__(self, parent):
self.data = {}
self.parent = parent
self.len = 0
self.end = False
words = []
tries = trie(None)
n = int(input())
for _ in range(n):
word = input()
words.append(word)
curr = tries
ln = len(word)
for w in word:
if not w in curr.data:
curr.data[w] = trie(curr)
curr = curr.data[w]
p = curr.parent
while p:
p.len=max(p.len, ln)
p = p.parent
else:
curr.end = True
def dfs_trie(curr:trie, result):
global n
if curr.end:
result.append("P")
n -= 1
for c in sorted(curr.data.keys(), key=lambda x: curr.data[x].len):
result.append(c)
dfs_trie(curr.data[c], result)
if n>0:
result.append("-")
result = []
dfs_trie(tries, result)
print(len(result))
print("\n".join(result))
# | 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... |