Skip to content

Commit

Permalink
2024-03-13
Browse files Browse the repository at this point in the history
  • Loading branch information
tgyuuAn committed Mar 13, 2024
1 parent 512b3bf commit d5e1b26
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 @@ -42,4 +42,5 @@
| 38차시 | 2023.02.15 | DP | <a href="https://www.acmicpc.net/problem/2749">피보나치 수 3</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/137
| 39차시 | 2023.02.18 | DP | <a href="https://www.acmicpc.net/problem/7579">앱</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/139
| 40차시 | 2023.02.21 | DP | <a href="https://www.acmicpc.net/problem/31413">입대</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/142
| 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 d5e1b26

Please sign in to comment.