๋ฌธ์
์ซ์ ์นด๋๋ ์ ์ ํ๋๊ฐ ์ ํ์ ธ ์๋ ์นด๋์ด๋ค. ์๊ทผ์ด๋ ์ซ์ ์นด๋ N๊ฐ๋ฅผ ๊ฐ์ง๊ณ ์๋ค. ์ ์ M๊ฐ๊ฐ ์ฃผ์ด์ก์ ๋, ์ด ์๊ฐ ์ ํ์๋ ์ซ์ ์นด๋๋ฅผ ์๊ทผ์ด๊ฐ ๋ช ๊ฐ ๊ฐ์ง๊ณ ์๋์ง ๊ตฌํ๋ ํ๋ก๊ทธ๋จ์ ์์ฑํ์์ค.
์ ๋ ฅ
์ฒซ์งธ ์ค์ ์๊ทผ์ด๊ฐ ๊ฐ์ง๊ณ ์๋ ์ซ์ ์นด๋์ ๊ฐ์ N(1 โค N โค 500,000)์ด ์ฃผ์ด์ง๋ค. ๋์งธ ์ค์๋ ์ซ์ ์นด๋์ ์ ํ์๋ ์ ์๊ฐ ์ฃผ์ด์ง๋ค. ์ซ์ ์นด๋์ ์ ํ์๋ ์๋ -10,000,000๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ๊ณ , 10,000,000๋ณด๋ค ์๊ฑฐ๋ ๊ฐ๋ค.
์ ์งธ ์ค์๋ M(1 โค M โค 500,000)์ด ์ฃผ์ด์ง๋ค. ๋ท์งธ ์ค์๋ ์๊ทผ์ด๊ฐ ๋ช ๊ฐ ๊ฐ์ง๊ณ ์๋ ์ซ์ ์นด๋์ธ์ง ๊ตฌํด์ผ ํ M๊ฐ์ ์ ์๊ฐ ์ฃผ์ด์ง๋ฉฐ, ์ด ์๋ ๊ณต๋ฐฑ์ผ๋ก ๊ตฌ๋ถ๋์ด์ ธ ์๋ค. ์ด ์๋ -10,000,000๋ณด๋ค ํฌ๊ฑฐ๋ ๊ฐ๊ณ , 10,000,000๋ณด๋ค ์๊ฑฐ๋ ๊ฐ๋ค.
์ถ๋ ฅ
์ฒซ์งธ ์ค์ ์ ๋ ฅ์ผ๋ก ์ฃผ์ด์ง M๊ฐ์ ์์ ๋ํด์, ๊ฐ ์๊ฐ ์ ํ ์ซ์ ์นด๋๋ฅผ ์๊ทผ์ด๊ฐ ๋ช ๊ฐ ๊ฐ์ง๊ณ ์๋์ง๋ฅผ ๊ณต๋ฐฑ์ผ๋ก ๊ตฌ๋ถํด ์ถ๋ ฅํ๋ค.
์๊ณ ๋ฆฌ์ฆ ๋ถ๋ฅ
ํ์ด
1์ฐจ
import sys ; input = sys.stdin.readline
N = int(input().rstrip())
cards = list(map(int, input().split()))
M = int(input().rstrip())
numbers = list(map(int, input().split()))
counts = [0 for _ in range(M)]
for card in cards :
if card in numbers :
idx = numbers.index(card)
counts[idx] += 1
for count in counts :
print(count, end=" ")
print()- ๋ฌธ์ ๋ง ๋ณด๊ณ , ์๊ฐ๋๋ ๋๋ก ํ์ด ๋ณด์๋ค.
- ์๊ฐ ์ด๊ณผ๊ฐ ๋ ๊ฒ์ด๋ผ๊ณ ์์ํ๊ณ , ์ค์ ๋ก ์๊ฐ ์ด๊ณผ๊ฐ ๋ฌ๋ค.
2์ฐจ - Solved!
import sys ; input = sys.stdin.readline
N = int(input().rstrip())
cards = list(map(int, input().split()))
M = int(input().rstrip())
numbers = list(map(int, input().split()))
counts = dict()
for card in cards :
try :
counts[card] += 1
except KeyError :
counts[card] = 1
for number in numbers :
try :
print(counts[number], end=' ')
except :
print(0, end=' ')
print()- ์๊ณ ๋ฆฌ์ฆ ๋ถ๋ฅ ์ค ํด์๋ฅผ-์ฌ์ฉํ-์งํฉ๊ณผ-๋งต ์์ ํํธ๋ฅผ ์ป์๋ค.
- ๋์
๋๋ฆฌ๋ฅผ ์ฌ์ฉํด์ ํค - ๋ฐธ๋ฅ ํํ๋ก ์ ์ฅํ๋ค๋ฉด?
- ์กด์ฌํ๋ ์ง ํ์ํ๋ ๋ฐ ๊ฑธ๋ฆฌ๋ ์๊ฐ๋ณต์ก๋๊ฐ ์ด๋ฏ๋ก, ์๊ฐ ์ด๊ณผ๊ฐ ๋จ์ง ์์ ๊ฒ์ด๋ผ๊ณ ํ๋จํ์๋ค.