ํ‹ฐ์Šคํ† ๋ฆฌ ๋ทฐ

728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ

 

14425๋ฒˆ: ๋ฌธ์ž์—ด ์ง‘ํ•ฉ

์ฒซ์งธ ์ค„์— ๋ฌธ์ž์—ด์˜ ๊ฐœ์ˆ˜ N๊ณผ M (1 ≤ N ≤ 10,000, 1 ≤ M ≤ 10,000)์ด ์ฃผ์–ด์ง„๋‹ค.  ๋‹ค์Œ N๊ฐœ์˜ ์ค„์—๋Š” ์ง‘ํ•ฉ S์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๋ฌธ์ž์—ด๋“ค์ด ์ฃผ์–ด์ง„๋‹ค. ๋‹ค์Œ M๊ฐœ์˜ ์ค„์—๋Š” ๊ฒ€์‚ฌํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์ž์—ด๋“ค์ด ์ฃผ์–ด

www.acmicpc.net

 

๋ฌธ์ œ ํ’€์ด

 N๊ฐœ์— ๋ฌธ์ž์—ด๋กœ ์ด๋ฃจ์–ด์ง„ ์ง‘ํ•ฉ S๊ฐ€ ์žˆ์„ ๋•Œ, M๊ฐœ์— ๋ฌธ์ž์—ด ์ค‘ ์ง‘ํ•ฉ S์— ํฌํ•จ๋˜๋Š” ๋ฌธ์ž๊ฐ€ ์ด ๋ช‡ ๊ฐœ์ธ์ง€ ๊ตฌํ•˜๋Š” ๋ฌธ์ œ์ด๋‹ค. ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•ด์„œ๋Š” `๋”•์…”๋„ˆ๋ฆฌ`์™€ `in`์„ ํ™œ์šฉํ•˜๋ฉด ์‰ฝ๊ฒŒ ํ’€ ์ˆ˜ ์žˆ๋‹ค.

 

 M๊ฐœ์˜ ๋ฌธ์ž์—ด ์ค‘, N์— ํฌํ•จ๋˜๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ ์œ„ํ•ด์„œ๋Š” `if pattern in strings`์™€ ๊ฐ™์ด ํ™•์ธํ•˜๋ฉด ๋œ๋‹ค. ํŒŒ์ด์ฌ์˜ `in` ์—ฐ์‚ฐ์€ ๋ฆฌ์ŠคํŠธ์˜ ๊ฒฝ์šฐ `O(N)`์˜ ์‹œ๊ฐ„์ด ์†Œ์š”๋˜์ง€๋งŒ ๋”•์…”๋„ˆ๋ฆฌ์˜ ๊ฒฝ์šฐ `O(1)`์˜ ์‹œ๊ฐ„์ด ์†Œ์š”๋˜๋ฏ€๋กœ ๋ณด๋‹ค ํšจ์œจ์ ์ด๋‹ค.

 

์ฝ”๋“œ

from sys import stdin


if __name__ == '__main__':
    n, m = map(int, stdin.readline().split())
    strings = {stdin.readline().strip() for _ in range(n)}
    cnt = 0
    
    for _ in range(m):
        pattern = stdin.readline().strip()
        if pattern in strings:
            cnt += 1
    print(cnt)

 

728x90
๋ฐ˜์‘ํ˜•
๋Œ“๊ธ€
๊ธ€ ๋ณด๊ด€ํ•จ
์ตœ๊ทผ์— ์˜ฌ๋ผ์˜จ ๊ธ€
์ตœ๊ทผ์— ๋‹ฌ๋ฆฐ ๋Œ“๊ธ€