[Python/ํŒŒ์ด์ฌ] [๐Ÿฅˆ3] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 14425 - ๋ฌธ์ž์—ด ์ง‘ํ•ฉ

keyneneยท2022๋…„ 10์›” 15์ผ
0

Python

๋ชฉ๋ก ๋ณด๊ธฐ
7/26
post-custom-banner

๐Ÿ“–[Python/ํŒŒ์ด์ฌ][๐Ÿฅˆ3] ๋ฐฑ์ค€ ์•Œ๊ณ ๋ฆฌ์ฆ˜ 14425 - ๋ฌธ์ž์—ด ์ง‘ํ•ฉ

๐Ÿ“œ๋ฌธ์ œ



๐Ÿ“•ํ’€์ด๋ฐฉํ–ฅ

S๋Š” ์ง‘ํ•ฉ.set()๋กœ, M๊ฐœ์˜ ๋น„๊ต๋ฌธ์ž์—ด์€ list()๋กœ ์„ ์–ธํ•˜๊ณ ,
M ๋น„๊ต๋ฌธ์ž์—ด ์š”์†Œ๋ฅผ ์ฒ˜์Œ๋ถ€ํ„ฐ ๋๊นŒ์ง€ ์ง‘ํ•ฉ S์— ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ๊ฒ€์‚ฌํ•˜๋ฉด์„œ
ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฉด cnt๋ฅผ ์ฆ๊ฐ€์‹œํ‚ค์ž

๐Ÿคท๐Ÿปโ€โ™€๏ธS๋Š” ์ง‘ํ•ฉ, M๊ฐœ์˜ ๋ฌธ์ž๋กœ ๊ตฌ์„ฑ๋œ ๋ฌธ์ž์—ด์€ ๋ฆฌ์ŠคํŠธ๋กœ ๊ตฌํ˜„ํ•˜๋Š” ์ด์œ ?
    ๋ฌธ์ œ์—์„œ S๋Š” ์ค‘๋ณต๋˜์ง€ ์•Š๋Š”๋‹ค๋ผ๋Š” ์กฐ๊ฑด์ด ์žˆ๊ณ , ๋ฌธ์ž์—ด์€ ์ค‘๋ณต๋  ์ˆ˜๋„ ์žˆ๊ธฐ ๋•Œ๋ฌธ์ž„
    ex)
    M๊ฐœ์˜ ๋ฌธ์ž์—ด comp์— "abc"๋ผ๋Š” ์š”์†Œ๊ฐ€ 2๊ฐœ๊ฐ€ ์žˆ๋Š”๋ฐ, S์— abc๊ฐ€ ์žˆ๋‹ค๋ฉด
    ์ •๋‹ต์€ 2๊ฐœ์ด๊ธฐ ๋•Œ๋ฌธ์— ๋ฌธ์ž์—ด comp๋Š” ์ง‘ํ•ฉ์ด ์•„๋‹Œ ๋ฆฌ์ŠคํŠธ๋กœ ์„ ์–ธ

๐Ÿ“์•Œ๊ณ ๋ฆฌ์ฆ˜ ๊ตฌํ˜„์ˆœ์„œ

  1. N,M์„ ์ž…๋ ฅ๋ฐ›๊ณ , S๋ฅผ .set()์œผ๋กœ, comp(๋ฌธ์ž์—ด)์„ list()๋กœ ์„ ์–ธ
  2. comp์˜ ์š”์†Œ๊ฐ€ S์— ํฌํ•จ๋˜์–ด์žˆ๋Š”์ง€ in์œผ๋กœ ํ™•์ธ ํ›„ ์นด์šดํŒ…

๐Ÿ’ป๊ฒฐ๊ณผ์ฝ”๋“œ

<import sys
input = sys.stdin.readline

n,m = map(int, input().split())
S = set(input().rstrip() for _ in range(n))
comp = list(input().rstrip() for _ in range(m))

cnt = 0
for comp_str in comp:
    if comp_str in S:
        cnt += 1
print(cnt)

โœ๏ธ1. N, M์ €์žฅ, S๋Š” set(), comp๋Š” list()์— ์ €์žฅ

import sys
input = sys.stdin.readline

n,m = map(int, input().split())
#S๋Š” ์ง‘ํ•ฉ์œผ๋กœ ์ž…๋ ฅ๋ฐ›์œผ๋ฉฐ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•จ
S = set(input().rstrip() for _ in range(n))
#๋ฌธ์ž์—ด์„ ๋ฆฌ์ŠคํŠธ์— ์ €์žฅ (์ค‘๋ณตํฌํ•จ)
comp = list(input().rstrip() for _ in range(m))

โœ๏ธ2. comp ๊ฐ ์š”์†Œ S์— ํฌํ•จ(in) ๋˜์–ด์žˆ๋Š”์ง€ ํ™•์ธ

cnt = 0
for comp_str in comp:
	#a "in" b : a๊ฐ€ b์— ํฌํ•จ๋˜์–ด ์žˆ์œผ๋ฉด "True"๋ฅผ ๋ฐ˜ํ™˜
    if comp_str in S:
        cnt += 1
print(cnt)

๐Ÿ“š๋‹ค๋ฅธ ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ์ •๋ฆฌ

import sys
input = sys.stdin.readline

n,m = map(int, input().split())
S = set(input().rstrip() for _ in range(n))

cnt = 0
for _ in range(m):
    comp = input().rstrip()
    if comp in S:
        cnt += 1
print(cnt)
๋ญ ๋‚ด ์•Œ๊ณ ๋ฆฌ์ฆ˜๊ณผ ๊ฑฐ์˜ ๋น„์Šทํ•˜์ง€๋งŒ comp๋ฅผ list๋กœ ์ €์žฅํ•˜์ง€ ์•Š๊ณ , 
for๋ฌธ์œผ๋กœ ๊ทธ๋•Œ๊ทธ๋•Œ ์ž…๋ ฅ๋ฐ›์œผ๋ฉด์„œ ๊ฒ€์‚ฌํ•œ๋‹ค๋Š” ์ ์—์„œ ์ฐจ์ด๊ฐ€ ์žˆ์Œ

โ€ปํ•˜์ง€๋งŒ, ์‹œ๊ฐ„๋ณต์žก๋„๋Š” O(N+M) == O(N+M+comp) ๋กœ ๋˜‘๊ฐ™์Œ!
๐Ÿ‘‰๐Ÿป์–ด๋–ป๊ฒŒ ์ฝ”๋”ฉํ•˜๋“  ๊ทธ์ € ์ทจํ–ฅ์ฐจ์ด..

profile
keynene
post-custom-banner

0๊ฐœ์˜ ๋Œ“๊ธ€