[๋ฐฑ์ค€] ๋ฌธ์ž์—ด์ง‘ํ•ฉ / 14425๋ฒˆ / ํŒŒ์ด์ฌ / Python / ๋”•์…”๋„ˆ๋ฆฌ
Algorithm/Baekjoon

[๋ฐฑ์ค€] ๋ฌธ์ž์—ด์ง‘ํ•ฉ / 14425๋ฒˆ / ํŒŒ์ด์ฌ / Python / ๋”•์…”๋„ˆ๋ฆฌ

728x90
๋ฐ˜์‘ํ˜•

๐Ÿ’กsolutions )

๐Ÿ’ฌ ์‰ฌ์šด ๋ฌธ์ œ๋ผ๊ณ  ์ƒ๊ฐํ•˜๊ณ  ๋น ๋ฅด๊ฒŒ ํ’€์–ด์„œ ์ œ์ถœํ–ˆ๋”๋‹ˆ ์‹œ๊ฐ„์ดˆ๊ณผ.... pypy๋กœ ์ œ์ถœํ•ด๋„ ์‹œ๊ฐ„์ดˆ๊ณผ..

 

 

๐Ÿ’ฌ ๋‹ค๋ฅธ ๋ฐฉ๋ฒ•์ด ์žˆ์„๊นŒ ํ•˜๊ณ  ์ฐพ์•„๋ณด๋‹ค๊ฐ€ ์ฒซ ํ’€์ด์™€ ๋น„์Šทํ•˜๊ฒŒ ๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ•˜์ง€๋งŒ ์ด์ค‘ for๋ฌธ์„ ์‚ฌ์šฉํ•˜์ง€ ์•Š์•„ ํ›จ์”ฌ ํšจ์œจ์ ์œผ๋กœ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋‹ค.

๐Ÿ’ฌ M๊ฐœ์˜ ๋ฌธ์ž์—ด ๊ฐ๊ฐ์ด N๊ฐœ์˜ ๋ฌธ์ž์—ด๋กœ ์ด๋ฃจ์–ด์ง„ S์ง‘ํ•ฉ์— ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•˜๊ธฐ๋งŒ ํ•˜๋ฉด ๋˜๊ธฐ ๋•Œ๋ฌธ์— -> ๋”•์…”๋„ˆ๋ฆฌ ์ž๋ฃŒํ˜•์— N๊ฐœ์˜ ๋ฌธ์ž์—ด์„ ๊ฐ๊ฐ key๊ฐ’์œผ๋กœ ์ €์žฅํ•œ ํ›„์— -> M๊ฐœ์˜ ๋ฌธ์ž์—ด์„ ๋ฐ˜๋ณต๋ฌธ์„ ํ†ตํ•ด key๊ฐ’์ด ์กด์žฌํ•˜๋Š”์ง€ ํ™•์ธํ•˜๋ฉด ๋œ๋‹ค.

 

๐Ÿ‘จ‍๐Ÿ’ปcode )

 

๐Ÿฅ์ฒซ ๋ฒˆ์งธ ํ’€์ด -> ์‹œ๊ฐ„ ์ดˆ๊ณผ๐Ÿ’ฅ

import sys

input = sys.stdin.readline
n, m = map(int, input().split())
s_dict = {}
for i in range(n):
    string = input().rstrip()
    if string[0] in s_dict:
        s_dict[string[0]].append(string)
    else:
        s_dict[string[0]] = [string]
answer = 0
for j in range(m):
    word = input().rstrip()
    if word[0] in s_dict:
        for k in s_dict[word[0]]:
            if word == k:
                answer += 1
                break
print(answer)

 

๐Ÿฅ๋‘ ๋ฒˆ์งธ ํ’€์ด -> ํ†ต๊ณผ

import sys

input = sys.stdin.readline
n, m = map(int, input().split())
s_dict = {}
for i in range(n):
    string = input().rstrip()
    s_dict[string] = 1
answer = 0
for j in range(m):
    word = input().rstrip()
    if word in s_dict:
        answer += 1
print(answer)

 

๐Ÿ“Œdescription )

 

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

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

www.acmicpc.net


๋ฌธ์ œ

์ด N๊ฐœ์˜ ๋ฌธ์ž์—ด๋กœ ์ด๋ฃจ์–ด์ง„ ์ง‘ํ•ฉ S๊ฐ€ ์ฃผ์–ด์ง„๋‹ค.

์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€๋Š” M๊ฐœ์˜ ๋ฌธ์ž์—ด ์ค‘์—์„œ ์ง‘ํ•ฉ S์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๊ฒƒ์ด ์ด ๋ช‡ ๊ฐœ์ธ์ง€ ๊ตฌํ•˜๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

์ž…๋ ฅ

์ฒซ์งธ ์ค„์— ๋ฌธ์ž์—ด์˜ ๊ฐœ์ˆ˜ N๊ณผ M (1 ≤ N ≤ 10,000, 1 ≤ M ≤ 10,000)์ด ์ฃผ์–ด์ง„๋‹ค. 

๋‹ค์Œ N๊ฐœ์˜ ์ค„์—๋Š” ์ง‘ํ•ฉ S์— ํฌํ•จ๋˜์–ด ์žˆ๋Š” ๋ฌธ์ž์—ด๋“ค์ด ์ฃผ์–ด์ง„๋‹ค.

๋‹ค์Œ M๊ฐœ์˜ ์ค„์—๋Š” ๊ฒ€์‚ฌํ•ด์•ผ ํ•˜๋Š” ๋ฌธ์ž์—ด๋“ค์ด ์ฃผ์–ด์ง„๋‹ค.

์ž…๋ ฅ์œผ๋กœ ์ฃผ์–ด์ง€๋Š” ๋ฌธ์ž์—ด์€ ์•ŒํŒŒ๋ฒณ ์†Œ๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ์œผ๋ฉฐ, ๊ธธ์ด๋Š” 500์„ ๋„˜์ง€ ์•Š๋Š”๋‹ค. ์ง‘ํ•ฉ S์— ๊ฐ™์€ ๋ฌธ์ž์—ด์ด ์—ฌ๋Ÿฌ ๋ฒˆ ์ฃผ์–ด์ง€๋Š” ๊ฒฝ์šฐ๋Š” ์—†๋‹ค.

์ถœ๋ ฅ

์ฒซ์งธ ์ค„์— M๊ฐœ์˜ ๋ฌธ์ž์—ด ์ค‘์— ์ด ๋ช‡ ๊ฐœ๊ฐ€ ์ง‘ํ•ฉ S์— ํฌํ•จ๋˜์–ด ์žˆ๋Š”์ง€ ์ถœ๋ ฅํ•œ๋‹ค.




๋ฐ˜์‘ํ˜•