class trie:
def __init__(self, parent):
self.data = {}
self.parent = parent
self.ln = 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]
curr.ln = max(ln, curr.ln)
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].ln):
result.append(c)
dfs_trie(curr.data[c], result)
if n>0:
result.append("-")
result = []
dfs_trie(tries, result)
print(len(result))
print(*result, sep="\n")
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
2880 KB |
Output is correct |
2 |
Correct |
17 ms |
2848 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
2900 KB |
Output is correct |
2 |
Correct |
13 ms |
2960 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
13 ms |
2900 KB |
Output is correct |
2 |
Correct |
15 ms |
2900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
15 ms |
2872 KB |
Output is correct |
2 |
Correct |
17 ms |
2908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
3156 KB |
Output is correct |
2 |
Correct |
29 ms |
4568 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
46 ms |
5808 KB |
Output is correct |
2 |
Correct |
55 ms |
6652 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
145 ms |
13896 KB |
Output is correct |
2 |
Correct |
334 ms |
26716 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
486 ms |
30924 KB |
Output is correct |
2 |
Correct |
168 ms |
9780 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1097 ms |
73920 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1092 ms |
57548 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |