sm 기술 블로그

91. 14425 (문자열 집합) 본문

문제/백준_파이썬

91. 14425 (문자열 집합)

sm_hope 2022. 6. 19. 12:29
import sys
input = sys.stdin.readline

N, M = map(int, input().split())

repository = set(input().strip() for _ in range(N))
compare = [input().strip() for _ in range(M)]

cnt = 0

for val in compare:
    if val in repository:
        cnt += 1

print(cnt)

문제요약

문자열 집합을 입력 받음. 비교 문자열들을 입력 받음.  비교 문자열에 집합의 문자열이 몇개 포함되어 있는가

설명

먼저 문자열 집합이라하여 집합을 사용하였다.

특별한 문법을 사용한 것이 없어 솔직히 어렵지 않다.

 

집합을 사용하지 않고 데이터사전을 이용한다면

import sys
input = sys.stdin.readline

N, M = map(int, input().split())

repository = [input().strip() for _ in range(N)]
compare = [input().strip() for _ in range(M)]

stringDic = {repository[i]: 1 for i in range(len(repository))}

cnt = 0

for val in compare:
    try:
        stringDic[val]
        cnt += 1
    except:
        continue

print(cnt)

다음과 같이 풀 수 있다.

'문제 > 백준_파이썬' 카테고리의 다른 글

93. 10816(숫자 카드2)  (0) 2022.06.19
92. 1620(나는야 포켓몬 마스터 이다솜)  (0) 2022.06.19
90. 10815(숫자카드)  (0) 2022.06.19
89. 18870 (좌표압축)  (0) 2022.06.18
88. 10814(나이순 정렬)  (0) 2022.06.17
Comments