BOJ - 14725 - 개미굴

Updated:

import sys

class Node():
    def __init__(self, key):
        self.key = key
        self.children = dict()

class Trie():

    def __init__(self):
        self.head = Node(None)

    def insert(self, string):
        curr_node = self.head

        for s in string:
            if s not in curr_node.children:
                curr_node.children[s] = Node(s)
            curr_node = curr_node.children[s]

    def printTrie(self, L, curr_node):
        if L == 0:
            curr_node = self.head

        for child in sorted(curr_node.children.keys()):
            print("--" * L, child, sep="")
            self.printTrie(L + 1, curr_node.children[child])

def solution():
    trie = Trie()
    N = int(sys.stdin.readline())
    for _ in range(N):
        arr = list(input().split())
        trie.insert(arr[1:])

    trie.printTrie(0, None)

solution()

https://www.acmicpc.net/problem/14725

Categories:

Updated:

Leave a comment