์ƒˆ์†Œ์‹

๐Ÿงฎ PS

๋ฐฑ์ค€ (boj) ํŒŒ์ด์ฌ - 10816 ๋ฒˆ : ์ˆซ์ž ์นด๋“œ2

  • -

๋ฌธ์ œ๋งํฌ : https://www.acmicpc.net/problem/10816

 

10816๋ฒˆ: ์ˆซ์ž ์นด๋“œ 2

์ฒซ์งธ ์ค„์— ์ƒ๊ทผ์ด๊ฐ€ ๊ฐ€์ง€๊ณ  ์žˆ๋Š” ์ˆซ์ž ์นด๋“œ์˜ ๊ฐœ์ˆ˜ N(1 ≤ N ≤ 500,000)์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„์—๋Š” ์ˆซ์ž ์นด๋“œ์— ์ ํ˜€์žˆ๋Š” ์ •์ˆ˜๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์ˆซ์ž ์นด๋“œ์— ์ ํ˜€์žˆ๋Š” ์ˆ˜๋Š” -10,000,000๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 10,

www.acmicpc.net

 

*ํŒŒ์ด์ฌ ์ฝ”๋“œ
from sys import stdin

N = int(input())
arr_n = list(map(int,stdin.readline().split()))
M = int(input())
arr_m = list(map(int,stdin.readline().split()))

dic = dict()

for i in arr_n:
    try :
        dic[i] += 1
    except:
        dic[i] = 1

for i in arr_m:
    try:
        print(dic[i] , end = " ")
    except:
        print(0, end=" ")
    

 

*์ฒ˜์Œ์—” ์ด์ง„ํƒ์ƒ‰์„ ์ด์šฉํ•˜์—ฌ ๋ฌธ์ œ๋ฅผ ํ•ด๊ฒฐํ•˜๋ ค๊ณ  ํ–ˆ๋‹ค. ์ตœ๋Œ€ํ•œ ๊ณ ๋ฏผํ•˜๋ฉฐ ํšจ์œจ์ ์œผ๋กœ ์ด์ง„ํƒ์ƒ‰์„ ๊ตฌํ˜„ํ•ด ๋ด๋„ ๊ฒŒ์† ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋ฐœ์ƒํ–ˆ๊ณ , ์ด์œ ๋Š” ๋‹จ์ผ target์„ ํƒ์ƒ‰ํ• ๋•Œ๋Š” ๋‹น์—ฐํžˆ ์ด๋ถ„ํƒ์ƒ‰์ด ๋น ๋ฅด์ง€๋งŒ, ์ด ๋ฌธ์ œ์—์„œ๋Š” target์ด ์—ฌ๋Ÿฌ๊ฐœ ์ด๊ณ , ์ค‘๋ณต๋„ ์กด์žฌํ•œ๋‹ค. ๋”ฐ๋ผ์„œ ์ตœ์ดˆ์— counting์„ 1๋ฒˆ ํ•ด๋†“๊ณ , ํƒ์ƒ‰์ด ์•„๋‹Œ ๊ทธ๋ƒฅ key๊ฐ’์œผ๋กœ value๋ฅผ ๋ฝ‘์•„์˜ค๋Š”๊ฒŒ ๋” ํšจ์œจ์ ์ด์—ˆ๋‹ค.

๋”•์…”๋„ˆ๋ฆฌ๋ฅผ ์ด์šฉํ•œ list ์นด์šดํŒ… : https://infinitt.tistory.com/78

 

ํŒŒ์ด์ฌ(python) ๋ฆฌ์ŠคํŠธ ์ค‘๋ณต ์š”์†Œ ๊ฐœ์ˆ˜ ์ฐพ๊ธฐ (์นด์šดํŒ…) or ์ œ๊ฑฐ,์‚ญ์ œํ•˜๊ธฐ (try , except , count)

*์ค‘๋ณต์š”์†Œ ์นด์šดํŒ…ํ•˜๊ธฐ *try, escept ๋ฌธ ์‚ฌ์šฉ count={} lists = ["a","a","b",'apple','w','wf'] for i in lists: try: count[i] += 1 except: count[i]=1 print(count) ๊ฒฐ๊ณผ๊ฐ’ : {'a': 2, 'b': 1, 'apple': 1, 'w'..

infinitt.tistory.com

 

 

Contents

ํฌ์ŠคํŒ… ์ฃผ์†Œ๋ฅผ ๋ณต์‚ฌํ–ˆ์Šต๋‹ˆ๋‹ค

์ด ๊ธ€์ด ๋„์›€์ด ๋˜์—ˆ๋‹ค๋ฉด ๊ณต๊ฐ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.