Trie๋ฅผ ์ฐ์ตํด๋ณผ ์ ์๋ ๋ํ์ ์ธ ๋ฌธ์ ์ด๋ค. ์ฌ์ค python์ผ๋ก ํธ๋ ๊ฒฝ์ฐ Trie๋ฅผ ์ด์ฉํ๋ ๊ฒ๋ณด๋ค set์ ์ด์ฉํ๋๊ฒ ๋ ๋น ๋ฅด๋ค (์ด์ ๋ set์ HashTable๋ก ๊ตฌํ๋์ด ์์ด, ์์ ํ์์ ์ ํ์๊ฐ์ด ๊ฑธ๋ฆฌ๊ธฐ ๋๋ฌธ์ด๋ผ๊ณ ํจ). ์ฌ์ง์ด python์ผ๋ก ํ๋ฉด ํต๊ณผ๋ฅผ ๋ชปํ๊ณ , Pypy3์ผ๋ก ์ ์ถํด์ผ ํ๋ค. ๋๋ trie ์ฐ์ต์ ์ํด trie๋ก ํ์ด ๋ณด์๋ค.
Node์ children์ dictionary๋ก ๊ตฌํํ๋ ๊ฒ๋ณด๋ค๋ defaultdict๋ก ๊ตฌํํ๋ ๊ฒ search์์ ๋ ์ ๋ฆฌํ ๊ฒ ๊ฐ๋ค๊ณ ์๊ฐํ๋๋ฐ,์คํ๋ ค ์๊ฐ์ด ๋ ๊ฑธ๋ ค์ ๊ทธ ์ด์ ๋ฅผ ์์๋ณด์๋ค.
๐ผ ๋ฌธ์ ๋งํฌ
https://www.acmicpc.net/problem/14425
14425๋ฒ: ๋ฌธ์์ด ์งํฉ
์ฒซ์งธ ์ค์ ๋ฌธ์์ด์ ๊ฐ์ N๊ณผ M (1 ≤ N ≤ 10,000, 1 ≤ M ≤ 10,000)์ด ์ฃผ์ด์ง๋ค. ๋ค์ N๊ฐ์ ์ค์๋ ์งํฉ S์ ํฌํจ๋์ด ์๋ ๋ฌธ์์ด๋ค์ด ์ฃผ์ด์ง๋ค. ๋ค์ M๊ฐ์ ์ค์๋ ๊ฒ์ฌํด์ผ ํ๋ ๋ฌธ์์ด๋ค์ด ์ฃผ์ด
www.acmicpc.net
- ํฐ์ด: ์ค๋ฒ III
- ๋ถ๋ฅ: ์๋ฃ๊ตฌ์กฐ, ๋ฌธ์์ด, ํด์, ํธ๋ฆฌ
โ ์ฝ๋ 1. Defaultdict๋ฅผ ์ด์ฉํ ๊ตฌํ (์คํจ)
import sys
input = sys.stdin.readline
from collections import defaultdict
class Node:
def __init__(self, data=False):
self.data = data
self.children = defaultdict(lambda: None)
def insert(head, string):
ptr = head
for s in string:
if ptr.children[s] is not None:
ptr.children[s] = Node()
ptr = ptr.children[s]
ptr.data = True
def search(head, string):
ptr = head
for s in string:
if ptr.children[s] is not None:
return False
ptr = ptr.children[s]
if ptr.data:
return True
else:
return False
head = Node()
N, M = map(int, input().split())
for _ in range(N):
insert(head, input().rstrip())
ctr = 0
for _ in range(M):
if search(head, input().rstrip()):
ctr += 1
print(ctr)
๐ก ์ฝ๋ 2. dictionary๋ฅผ ์ด์ฉํ ๊ตฌํ (์ฑ๊ณต)
import sys
input = sys.stdin.readline
class Node:
def __init__(self, data=False):
self.data = data
self.children = {}
def insert(head, string):
ptr = head
for s in string:
if s not in ptr.children:
ptr.children[s] = Node()
ptr = ptr.children[s]
ptr.data = True
def search(head, string):
ptr = head
for s in string:
if s not in ptr.children:
return False
ptr = ptr.children[s]
if ptr.data:
return True
else:
return False
head = Node()
N, M = map(int, input().split())
for _ in range(N):
insert(head, input().rstrip())
ctr = 0
for _ in range(M):
if search(head, input().rstrip()):
ctr += 1
print(ctr)
์ฝ๋ ์ค๋ช
- Trie๋ฅผ ์ด์ฉํ ๊ตฌํ์ด๋ค.
- ์ฝ๋ 1๊ณผ 2์ ์ฐจ์ด์ ์ Node์ children์ ์ ์ฅํ๋ ๋ฐ์
defaultdict
๋ฅผ ์ฌ์ฉํ๋, ์ผ๋ฐdictionary
๋ฅผ ์ฌ์ฉํ๋์ ์ฐจ์ด๋ฐ์ ์๋ค. - ๋๋ฌธ์ search์ insert์์, children์ ํน์ ๋ฌธ์๊ฐ ์๋์ง ๊ฒ์ฌํ๋ ๋ถ๋ถ์์ ์ฐจ์ด๊ฐ ๋ฐ์ํ๋ค.
- ์ฝ๋ 1:
if ptr.children[s] is not None
- ์ฝ๋ 2:
if s in ptr.children
- ์ฝ๋ 1:
- ๊ฒฐ๊ณผ: ์ฝ๋ 1 - ์คํจ (์๊ฐ์ด๊ณผ) / ์ฝ๋ 2 - ์ฑ๊ณต
์ฝ๋ 2์ ๊ฒฝ์ฐ๋, ํ์์ ptr.children์ ๊ธธ์ด๋งํผ์ ์๊ฐ์ด ์์๋ ๊ฒ์ด๋ผ ์๊ฐํ๊ณ ,
์ฝ๋ 1์ ๊ฒฝ์ฐ๋, ํ์์ O(1) ์๊ฐ์ด ์์๋ ๊ฒ์ด๋ผ ์๊ฐํ๋ค.
ํ์ง๋ง ๊ฒฐ๊ณผ๋ ๋ฐ๋๋ก, ์ฝ๋ 1์ ์๊ฐ์ด๊ณผ๊ฐ ๋ฐ์ํ๊ณ ์ฝ๋ 2๋ ํต๊ณผํ๋ค.
๋ฐฑ์ค ์ง๋ฌธ๊ฒ์ํ์ ๊ธ์ ์ฌ๋ ค์ ๋ค์๊ณผ ๊ฐ์ ๋ต์ ์ป์ ์ ์์๋ค.
๊ฐ node์ key๊ฐ ์ํ๋ฒณ 26์๋ก ์ ํ๋์ด ์๊ธฐ ๋๋ฌธ์, children์ ์ต๋๊ฐฏ์ ์ญ์ 26๊ฐ๋ก ์ ํ๋๋ค.
๋ฐ๋ผ์ ์ฝ๋ 2์์ ptr.children์ ํ์์ด O(n)์ด๋ผ๋ n์ด ์ต๋ 26์ผ๋ก ๋งค์ฐ ์๊ณ ,
์ฝ๋ 1์ ๊ฒฝ์ฐ ํ์์ O(1)์ด๋ผ๋ ๊ทธ ์์๊ฐ ์ปค์ ์ฝ๋ 2์ ์ ํํ์๋ณด๋ค ๋๋ ค์ง ์๋ ์๋ค๋ ๊ฒ์ด๋ค.
์ฝ๋๋ฅผ ์งค ๋ ์๊ฐ๋ณต์ก๋ ์ธ์๋ n์ ํฌ๊ธฐ๋ ํจ๊ป ๊ณ ๋ คํด์ผ ํ๋ค๋ ๊ฒ์ ๋๋ ํ๋ฃจ..
'๐ CS > ์ฝ๋ฉํ ์คํธ' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[๋ฐฑ์ค 16235๋ฒ] ๋๋ฌด ์ ํ ํฌ (์ ์ ์ํ defaultdict) (1) | 2022.10.15 |
---|---|
[๋ฐฑ์ค 9084๋ฒ] ๋์ (python/ํ์ด์ฌ) + ์ค๋ฒ II ๋ฌ์ฑ (1) | 2022.10.08 |
[๋ฐฑ์ค 2579๋ฒ] ๊ณ๋จ ์ค๋ฅด๊ธฐ (python/ํ์ด์ฌ) (0) | 2022.09.15 |
[๋ฐฑ์ค 14500๋ฒ] ํ ํธ๋ก๋ฏธ๋ ธ (python/ํ์ด์ฌ) (2) | 2022.09.14 |
[๋ฐฑ์ค 7569๋ฒ] ํ ๋งํ (python/ํ์ด์ฌ) (0) | 2022.09.09 |