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 |
1 |
Correct |
13 ms |
2900 KB |
Output is correct |
2 |
Correct |
12 ms |
2900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
16 ms |
2956 KB |
Output is correct |
2 |
Correct |
13 ms |
2912 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
14 ms |
2964 KB |
Output is correct |
2 |
Correct |
13 ms |
2848 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 |
17 ms |
3028 KB |
Output is correct |
2 |
Correct |
50 ms |
4348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
70 ms |
5600 KB |
Output is correct |
2 |
Correct |
80 ms |
6340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
212 ms |
13156 KB |
Output is correct |
2 |
Correct |
527 ms |
25396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
654 ms |
29372 KB |
Output is correct |
2 |
Correct |
550 ms |
9380 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1087 ms |
56968 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Execution timed out |
1086 ms |
51940 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |