백준 14425 - 문자열 집합 (Python)
Updated:
더 쉽게 풀 수 있지만 Trie자료구조를 공부하기 위해 썼습니다.
pypy3로 제출하셔야 합니다.
전체 코드
import sys
input = sys.stdin.readline
class Node():
def __init__(self, key):
self.key = key
self.children = dict()
class Trie():
def __init__(self):
self.head = Node(None)
def insert(self, string):
curr_node = self.head
for char in string:
if char not in curr_node.children:
curr_node.children[char] = Node(char)
curr_node = curr_node.children[char]
def find(self, string):
curr_node = self.head
for char in string:
if char in curr_node.children:
curr_node = curr_node.children[char]
else:
return False
return True
N, M = map(int, input().split())
trie = Trie()
for _ in range(N):
trie.insert(input())
answer = 0
for _ in range(M):
if trie.find(input()):
answer += 1
print(answer)