From ed9e5aa0c699c065f709a5d47e7ad247efd44158 Mon Sep 17 00:00:00 2001 From: tgyuu-An Date: Sun, 17 Mar 2024 00:43:50 +0900 Subject: [PATCH] 2024-03-16 --- tgyuuAn/README.md | 1 + ...4\355\201\254 \355\212\270\353\246\254.py" | 48 +++++++++++++++++++ 2 files changed, 49 insertions(+) create mode 100644 "tgyuuAn/\355\212\270\353\235\274\354\235\264/\353\224\224\354\212\244\355\201\254 \355\212\270\353\246\254.py" diff --git a/tgyuuAn/README.md b/tgyuuAn/README.md index f608a203..e27b7112 100644 --- a/tgyuuAn/README.md +++ b/tgyuuAn/README.md @@ -43,4 +43,5 @@ | 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 | 41차시 | 2023.03.04 | DP | 자두나무 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/148 +| 45차시 | 2023.03.16 | 트라이 | 트라이 | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/162 --- diff --git "a/tgyuuAn/\355\212\270\353\235\274\354\235\264/\353\224\224\354\212\244\355\201\254 \355\212\270\353\246\254.py" "b/tgyuuAn/\355\212\270\353\235\274\354\235\264/\353\224\224\354\212\244\355\201\254 \355\212\270\353\246\254.py" new file mode 100644 index 00000000..db7bf90e --- /dev/null +++ "b/tgyuuAn/\355\212\270\353\235\274\354\235\264/\353\224\224\354\212\244\355\201\254 \355\212\270\353\246\254.py" @@ -0,0 +1,48 @@ +import sys + +def input(): return sys.stdin.readline().rstrip() + +N = int(input()) + +class Node(): + def __init__(self, key): + self.key = key + self.children = {} + +class Tries(): + def __init__(self): + self.head = Node(None) + + def insert(self, path): + now = self.head + directories = path.split("\\") + + for directory in directories: + if directory not in now.children: + now.children[directory] = Node(directory) + + now = now.children[directory] + + def dfs(self, now : Node, depth): + if now.key is not None: + for _ in range(depth): + print(" ", end="") + print(now.key) + + now_children = list(now.children.keys()) + now_children.sort() + + for child in now_children: + self.dfs(now.children[child], depth+1) + + def display_all(self): + now = self.head + self.dfs(now, -1) + +tries = Tries() + +for _ in range(N): + path = input() + tries.insert(path) + +tries.display_all() \ No newline at end of file