[백준/python] 14725 개미굴

박동현·2024년 12월 6일
0

Algorithm

목록 보기
8/11
post-thumbnail
import sys; input = sys.stdin.readline


def insert(words):
    now = trie
    for word in words:
        now = now.setdefault(word, dict())

def sout(now,depth=0):
    for word in sorted(now):
        print("--"*depth+word)
        sout(now[word], depth+1)

trie = dict()
for _ in range(int(input())):
    K, *words = input().split()
    insert(words)
    
sout(trie)

트라이

간단히 딕셔너리로 구현한 트라이 구조로 단어들을 저장하고,
depth를 저장해 재귀하며 조건에 맞게 출력하는 함수를 구현해 간단하게 출력할 수 있습니다.

profile
동현

0개의 댓글