Skip to content

Commit

Permalink
Merge pull request #165 from AlgoLeadMe/46-tgyuuAn
Browse files Browse the repository at this point in the history
46-tgyuuAn
  • Loading branch information
tgyuuAn authored Apr 22, 2024
2 parents 318bf96 + e9416a4 commit 92f8388
Show file tree
Hide file tree
Showing 2 changed files with 81 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 @@ -47,4 +47,5 @@
| 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
| 45์ฐจ์‹œ | 2023.03.16 | ํŠธ๋ผ์ด | <a href="https://www.acmicpc.net/problem/31413">ํŠธ๋ผ์ด</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/162
| 46์ฐจ์‹œ | 2023.03.20 | ํŠธ๋ผ์ด | <a href="https://www.acmicpc.net/problem/27652">AB</a> | https://github.com/AlgoLeadMe/AlgoLeadMe-1/pull/165
---
80 changes: 80 additions & 0 deletions tgyuuAn/ํŠธ๋ผ์ด/AB.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
import sys
from collections import defaultdict

class Node():
def __init__(self, key = None):
self.key = key
self.count = 0
self.children = {}

class Tries():
def __init__(self):
self.head = Node(None)

def add(self, element):
now = self.head

for char in element:
if char not in now.children:
now.children[char] = Node(char)

now = now.children[char]
now.count += 1

def delete(self, element):
now = self.head

for char in element:
child = now.children[char]
child.count -= 1
if child.count == 0: del now.children[char]
now = child

def find(self, element):
now = self.head
dic = defaultdict(int)

string = ""
for char in element:
if char not in now.children: return dic

string = string + char
now = now.children[char]
dic[string] = now.count

return dic

def input(): return sys.stdin.readline().rstrip()

def query(method, target, element):
if method == "add": target.add(element)

if method == "delete": target.delete(element)

if method == "find":
n = len(element)
a_result = A.find(element)
b_result = B.find(element[::-1])

answer = 0
for a_len in range(1,n):
answer += a_result[element[:a_len]] * b_result[element[:a_len-1:-1]]

print(answer)

N = int(input())
A = Tries()
B = Tries()
method, target, element = "", "", ""

for _ in range(N):
_input = input()
if _input[:4] == "find":
method,element = _input.split()
query(method, A, element)

else:
method, target, element = _input.split()

if target == "A": query(method, A, element)
else: query(method, B, element[::-1])

0 comments on commit 92f8388

Please sign in to comment.