๐Ÿ”‘ CS/์ฝ”๋”ฉํ…Œ์ŠคํŠธ

[๋ฐฑ์ค€ 14425๋ฒˆ] ๋ฌธ์ž์—ด ์ง‘ํ•ฉ (python/ํŒŒ์ด์ฌ) - Trie ๊ตฌํ˜„ํ•˜๊ธฐ/defaultdict ์“ฐ๋ฉด ์•ˆ๋˜๋Š” ์ด์œ 

๋ณต๋งŒ 2022. 10. 10. 14:37

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 - ์‹คํŒจ (์‹œ๊ฐ„์ดˆ๊ณผ) / ์ฝ”๋“œ 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์˜ ํฌ๊ธฐ๋„ ํ•จ๊ป˜ ๊ณ ๋ คํ•ด์•ผ ํ•œ๋‹ค๋Š” ๊ฒƒ์„ ๋Š๋‚€ ํ•˜๋ฃจ..

๋ฐ˜์‘ํ˜•