목차

휴대폰 자판

ps
링크acmicpc.net/…
출처BOJ
문제 번호5670
문제명휴대폰 자판
레벨플래티넘 3
분류

트라이

시간복잡도O(l)
인풋사이즈l<=10^6
사용한 언어Python
제출기록3676ms
최고기록1908ms
해결날짜2021/01/06

풀이

코드

코드 1

"""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()

코드 2

"""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()