diff --git a/tgyuuAn/README.md b/tgyuuAn/README.md index ff3b4b12..54dec912 100644 --- a/tgyuuAn/README.md +++ b/tgyuuAn/README.md @@ -45,4 +45,5 @@ | 41차시 | 2023.03.04 | DP | 자두나무 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/148 | 42차시 | 2023.03.07 | DFS | 스도쿠 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/152 | 43차시 | 2024.03.10 | 이분 탐색 | 징검다리 건너기 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/157 +| 44차시 | 2023.03.13 | 트라이 | 개미굴 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/159 --- diff --git "a/tgyuuAn/\355\212\270\353\235\274\354\235\264/\352\260\234\353\257\270\352\265\264.py" "b/tgyuuAn/\355\212\270\353\235\274\354\235\264/\352\260\234\353\257\270\352\265\264.py" new file mode 100644 index 00000000..5d33234f --- /dev/null +++ "b/tgyuuAn/\355\212\270\353\235\274\354\235\264/\352\260\234\353\257\270\352\265\264.py" @@ -0,0 +1,48 @@ +import sys + +def input(): return sys.stdin.readline() + +class Node(): + def __init__(self, key): + self.key = key + self.children = {} # 딕셔너리 선언 + +class Tries(): + def __init__(self): + self.head = Node(None) + + def insert(self, informations): + cur_node = self.head + + for info in informations: + # 만약 자식 노드들 중에서 char가 없을 경우 새로운 노드를 만듬 + if info not in cur_node.children: + cur_node.children[info] = Node(info) + + cur_node = cur_node.children[info] + + def search_all(self): + cur_node = self.head + stack = [[cur_node, 0]] + + while stack: + cur_node, depth = stack.pop() + + if cur_node.key != None: + if depth == 1: print(cur_node.key) + else: + for _ in range((depth-1)*2): print("-", end="") + print(cur_node.key) + + for key in sorted(list(cur_node.children.keys()), reverse = True): + stack.append([cur_node.children[key], depth+1]) + + +N = int(input()) +tries = Tries() + +for _ in range(N): + info = input().split()[1:] + tries.insert(info) + +tries.search_all() \ No newline at end of file