728x90
반응형

https://www.acmicpc.net/problem/14425

 

14425번: 문자열 집합

첫째 줄에 문자열의 개수 N과 M (1 ≤ N ≤ 10,000, 1 ≤ M ≤ 10,000)이 주어진다.  다음 N개의 줄에는 집합 S에 포함되어 있는 문자열들이 주어진다. 다음 M개의 줄에는 검사해야 하는 문자열들이 주어

www.acmicpc.net

VERSION 3.0

import sys

N = list(map(int,sys.stdin.readline().split()))
S = { sys.stdin.readline().rstrip() for i in range(N[0])}

cnt = 0
for j in range(N[1]):
    if sys.stdin.readline().rstrip() in S:
        cnt+=1
print(cnt)

VERSION 2.0

 

 

dict, set의 삽입, 제거, 탐색, 포함여부는 보통 시간복잡도가 O(1)이다.

# 값의 순서, index에 접근할때는 보통 list를 많이 사용하고,
# 값의 탐색이나 확인은 보통 dictionary, set형을 많이 사용한다.

import sys

input = sys.stdin.readline

n, m = map(int, input().split())
cnt = 0

S = {input().rstrip() for _ in range(n)}

for j in range(m):
    if input().rstrip() in S:
        cnt+=1
print(cnt)

 

 

 

VERSION 1.0

 

 list의 삽입, 제거, 탐색, 포함여부는 보통 시간복잡도가 O(N)

import sys

N = list(map(int, sys.stdin.readline().rstrip().split()))
S=[]
M =[]
for i in range(N[0]):
    a = sys.stdin.readline().rstrip()
    S.append(a)
for j in range(N[1]):
    b = sys.stdin.readline().rstrip()
    M.append(b)
cnt = 0
for i in M:
    if i in S:
       cnt+=1
print(cnt)
728x90
반응형

+ Recent posts