Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

44-tgyuuAn #159

Merged
merged 3 commits into from
Mar 24, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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():
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ํŒŒ์ด์ฌ์—์„œ์˜ class... ๊ฑฐ์˜ ์žŠ๊ณ  ์ง€๋ƒˆ๋„ค์š”
๋‹ค์‹œ class ๊ฐœ๋… ์ฐพ์•„๋ณด๋ฉด์„œ ๋ณต์Šตํ•˜๊ณ  ๊ฐ‘๋‹ˆ๋‹ค ๐Ÿ”ฅ๐Ÿ”ฅ

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()