From d5e1b264efdd276896346632179f9065fdda18d1 Mon Sep 17 00:00:00 2001 From: tgyuu-An Date: Wed, 13 Mar 2024 22:45:06 +0900 Subject: [PATCH] 2024-03-13 --- tgyuuAn/README.md | 1 + .../\352\260\234\353\257\270\352\265\264.py" | 48 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 "tgyuuAn/\355\212\270\353\235\274\354\235\264/\352\260\234\353\257\270\352\265\264.py" diff --git a/tgyuuAn/README.md b/tgyuuAn/README.md index 530f4f4a..2877d797 100644 --- a/tgyuuAn/README.md +++ b/tgyuuAn/README.md @@ -42,4 +42,5 @@ | 38차시 | 2023.02.15 | DP | 피보나치 수 3 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/137 | 39차시 | 2023.02.18 | DP | | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/139 | 40차시 | 2023.02.21 | DP | 입대 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/142 +| 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