ps | |
---|---|
링크 | acmicpc.net/… |
출처 | BOJ |
문제 번호 | 5670 |
문제명 | 휴대폰 자판 |
레벨 | 플래티넘 3 |
분류 |
트라이 |
시간복잡도 | O(l) |
인풋사이즈 | l<=10^6 |
사용한 언어 | Python |
제출기록 | 3676ms |
최고기록 | 1908ms |
해결날짜 | 2021/01/06 |
"""Solution code for "BOJ 5670. 휴대폰 자판".
- Problem link: https://www.acmicpc.net/problem/5670
- Solution link: http://www.teferi.net/ps/problems/boj/5670
"""
import sys
from teflib import string
def main():
while True:
try:
n = int(sys.stdin.readline())
except ValueError:
break
words = [sys.stdin.readline().rstrip() for _ in range(n)]
trie = string.Trie(words)
input_count = 0
for word in words:
for node in trie.prefixes(word):
if len(trie.children[node]) > 1 or trie.word_counts[node] > 0:
input_count += 1
print(f'{input_count / n:.2f}')
if __name__ == '__main__':
main()
"""Solution code for "BOJ 5670. 휴대폰 자판".
- Problem link: https://www.acmicpc.net/problem/5670
- Solution link: http://www.teferi.net/ps/problems/boj/5670
"""
from teflib import string
import sys
def main():
while True:
try:
n = int(sys.stdin.readline())
except ValueError:
break
trie = string.Trie(sys.stdin.readline().rstrip() for _ in range(n))
input_count = n
for node in trie.nodes():
if len(trie.children[node]) > 1 or trie.word_counts[node] > 0:
input_count += trie.get_child_count(node)
print(f'{input_count / n:.2f}')
if __name__ == '__main__':
main()