Skip to content

Commit

Permalink
Merge pull request #159 from AlgoLeadMe/44-tgyuuAn
Browse files Browse the repository at this point in the history
44-tgyuuAn
  • Loading branch information
tgyuuAn authored Mar 24, 2024
2 parents 7491076 + d8db55b commit 88a639e
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 0 deletions.
1 change: 1 addition & 0 deletions tgyuuAn/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -45,4 +45,5 @@
| 41차시 | 2023.03.04 | DP | <a href="https://www.acmicpc.net/problem/2240">자두나무</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/148
| 42차시 | 2023.03.07 | DFS | <a href="https://www.acmicpc.net/problem/2239">스도쿠</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/152
| 43차시 | 2024.03.10 | 이분 탐색 | <a href="https://school.programmers.co.kr/learn/courses/30/lessons/64062">징검다리 건너기</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/157
| 44차시 | 2023.03.13 | 트라이 | <a href="https://www.acmicpc.net/problem/14725">개미굴</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/159
---
48 changes: 48 additions & 0 deletions tgyuuAn/트라이/개미굴.py
Original file line number Diff line number Diff line change
@@ -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()

0 comments on commit 88a639e

Please sign in to comment.