๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ

Algorithm/BOJ

(36)
[BOJ] ๊ตญํšŒ์˜์› ์„ ๊ฑฐ(#1417) ์ฝ”๋“œ n = int(input()) dasom = int(input()) arr = [int(input()) for _ in range(n-1)] cnt = 0 if n == 1: pass else: while dasom ์ฒ˜์Œ์— ๋‚˜๋Š” ์ˆœ์„œ๋Œ€๋กœ ๋ชจ๋“  ํ›„๋ณด์ž์˜ ๋“ํ‘œ์ˆ˜์—์„œ ํ•˜๋‚˜์”ฉ ๋นผ์„œ ๋‹ค์†œ์ด์˜ ๋“ํ‘œ์ˆ˜์— ๋”ํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์„ ํƒํ•˜์˜€๋Š”๋ฐ, ์ƒ๊ฐํ•ด๋ณด๋‹ˆ ๋ฌธ์ œ์—์„œ ์ˆœ์„œ๋Œ€๋กœ ์‚ฌ๋žŒ์„ ๋งค์ˆ˜ํ•˜๋ผ๊ณ  ์ ํ˜€ ์žˆ์ง€ ์•Š์•˜๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค. ๊ทธ๋Ÿฌ๋‹ˆ ๊ทธ๋ƒฅ ์ตœ๋‹ค๋“ํ‘œ์ž์˜ ๋“ํ‘œ์ˆ˜๋ฅผ ๊ณ„์† ๋นผ์„œ ์ตœ๋‹ค ๋“ํ‘œ์ž๊ฐ€ ๋‹ค์†œ์ด ๋  ๋•Œ๊นŒ์ง€ ์‚ฌ๋žŒ์„ ๋งค์ˆ˜ํ•˜๋ฉด ํ•ด๊ฒฐ๋˜๋Š” ๋ฌธ์ œ์˜€๋‹ค. 1417๋ฒˆ: ๊ตญํšŒ์˜์› ์„ ๊ฑฐ ์ฒซ์งธ ์ค„์— ํ›„๋ณด์˜ ์ˆ˜ N์ด ์ฃผ์–ด์ง„๋‹ค. ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ ์ฐจ๋ก€๋Œ€๋กœ ๊ธฐํ˜ธ 1๋ฒˆ์„ ์ฐ์œผ๋ ค๊ณ  ํ•˜๋Š” ์‚ฌ๋žŒ์˜ ์ˆ˜, ๊ธฐํ˜ธ 2๋ฒˆ์„ ์ฐ์œผ๋ ค๊ณ  ํ•˜๋Š” ์ˆ˜, ์ด๋ ‡๊ฒŒ ์ด N๊ฐœ์˜ ์ค„์— ๊ฑธ์ณ ..
[BOJ] ํ•œ๊ตญ์ด ๊ทธ๋ฆฌ์šธ ๋• ์„œ๋ฒ„์— ์ ‘์†ํ•˜์ง€ ์ฝ”๋“œ t = int(input()) imp = input().split('*') for _ in range(t): word = input() if len(word) >= len(imp[0]) + len(imp[1]): if word[0:len(imp[0])] == imp[0] and word[-1 * len(imp[1]):] == imp[1]: print("DA") else: print('NE') else: print('NE') ๋ถ„์„ > ๊ฑฐ์˜ ๋‹ค ํ’€์—ˆ๋Š”๋ฐ, imp[0]๊ณผ imp[1]์˜ ๊ธธ์ด๋ฅผ ๋”ํ–ˆ์„ ๋•Œ word์˜ ๊ธธ์ด๋ณด๋‹ค ์ž‘์€ ๊ฒฝ์šฐ๋ฅผ ๋†“์ณ์„œ ๊ณ„์† ๋ฌธ์ œ๋ฅผ ํ’€์ง€ ๋ชปํ•˜์˜€๋‹ค. ์˜ˆ๋ฅผ๋“ค์–ด, ab*bc ์ด๊ณ  word๊ฐ€ abc์ธ ๊ฒฝ์šฐ ์ด๋Š” ์˜ณ์ง€ ๋ชปํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์˜ˆ์™ธ์ฒ˜๋ฆฌ๋ฅผ ํ•ด์ฃผ์–ด์•ผ ํ•œ๋‹ค. ๋‚˜๋Š” ์ด ์ค‘๋ณต๋œ ๋ฌธ์ž๋ฅผ ์–ด๋–ป๊ฒŒ ์ฒ˜..
[BOJ] ๋“ฃ๋ณด์žก ์ฝ”๋“œ a,b = map(int,input().split()) arr = [input() for _ in range(a)] imp = [input() for _ in range(b)] arr = set(arr) imp = set(imp) result = sorted(list(arr&imp)) print(len(result)) for k in result: print(k) ๋ถ„์„ > ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ์˜€์ง€๋งŒ set ํ•จ์ˆ˜๋ฅผ ์“ฐ์ง€ ์•Š์œผ๋ฉด ์‹œ๊ฐ„์ดˆ๊ณผ๊ฐ€ ๋‚˜๋Š” ๋ฌธ์ œ์˜€๋‹ค. ๋ฌธ์ œ๋ฅผ ํ’€๋ฉด์„œ setํ•จ์ˆ˜๋ผ๋ฆฌ ๊ฒน์น˜๋Š” ์›์†Œ๋งŒ ์ถ”์ถœํ•  ๋•Œ์—๋Š” sorted(list(a&b)) ๊ฐ™์ด ์ฝ”๋“œ๋ฅผ ์ ์œผ๋ฉด ๋œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ฒŒ ๋˜์—ˆ๋‹ค. 1764๋ฒˆ: ๋“ฃ๋ณด์žก ์ฒซ์งธ ์ค„์— ๋“ฃ๋„ ๋ชปํ•œ ์‚ฌ๋žŒ์˜ ์ˆ˜ N, ๋ณด๋„ ๋ชปํ•œ ์‚ฌ๋žŒ์˜ ์ˆ˜ M์ด ์ฃผ์–ด์ง„๋‹ค. ์ด์–ด์„œ ๋‘˜์งธ ์ค„๋ถ€ํ„ฐ N๊ฐœ์˜ ์ค„..
[BOJ] ๋น„์Šทํ•œ ๋‹จ์–ด ํ•ด์„ค ์ฝ”๋“œ N = int(input()) compare = list(input()) ans = 0 for _ in range(N-1): compare_word = compare[:] check_word = list(input()) for _ in range(len(check_word)): a = check_word.pop(0) if a in compare_word: compare_word.remove(a) else: check_word.append(a) A = len(compare_word) B = len(check_word) if (A==0 and B==0) or (A==1 and B==0) or (A==0 and B==1) or (A==1 and B==1): ans += 1 print(ans) ๋ถ„์„ ..
[BOJ] ๋žœ์„  ์ž๋ฅด๊ธฐ ๋‚˜์˜ ์ฝ”๋“œ a,b = map(int,input().split()) arr = [int(input()) for _ in range(a)] k = 1 ans = [] while True: imp = [] for num in arr: t = num // k imp.append(t) k += 1 if sum(imp) == b: break while True: ans = [] k += 1 for num in arr: ans.append(num//k) if ans != imp: break print(k-1) ๋ถ„์„ > ์‹œ๊ฐ„์ดˆ๊ณผ ๋‚  ๊ฒƒ์„ ์•Œ๊ณ  ์žˆ์—ˆ์ง€๋งŒ, ํšจ์œจ์„ฑ์„ ์ œ์™ธํ•˜๊ณ  ์ผ๋‹จ ๋ฌธ์ œ๋ฅผ ํ’€์–ด๋ณด๊ธฐ ์œ„ํ•ด์„œ ํ’€์–ด๋ณด์•˜๋‹ค. ๋‹น์—ฐํžˆ ๊ฒฐ๊ณผ๋Š” ์‹œ๊ฐ„์ดˆ๊ณผ. ํ•ด์„ค ์ฝ”๋“œ a,b = map(int,input().split()) arr = [in..
[BOJ] ๊ฐ€์žฅ ๊ธด ์ฆ๊ฐ€ํ•˜๋Š” ๋ถ€๋ถ„ ์ˆ˜์—ด ์ฝ”๋“œ t = int(input()) num = list(map(int,input().split())) dp = [1] * t for i in range(t): for j in range(i): if num[i] > num[j]: dp[i] = max(dp[i],dp[j]+1) # ํ˜„์žฌ์˜ dp๊ฐ’๊ณผ ์ด์ „ ์ˆ˜์˜ dp๊ฐ’์— 1์„ ๋”ํ•œ ๊ฐ’์„ ๋น„๊ต ํ›„ ๋” ํฌ๋ฉด ๊ฐฑ์‹  print(max(dp)) ๋ถ„์„ > LIS(Longest Increasing Subsequence) ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ ์›๋ฆฌ๋Š” ๊ฐ„๋‹จํ•˜๋‹ค. ํ˜„์žฌ์˜ ์ˆ˜ (num[i]) ๊ฐ€ ๋น„๊ต๋Œ€์ƒ์ด ๋˜๋Š” ์ด์ „์˜ ์ˆ˜ (num[j]) ๋ณด๋‹ค ํด ๋•Œ๋งŒ, ๋น„๊ต ๋Œ€์ƒ์ด ๋˜๋Š” ์ˆ˜์˜ dp๊ฐ’์— 1์„ ๋”ํ•œ ๊ฐ’์„ ๊ฐฑ์‹ ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๊ทธ๋ ‡๊ฒŒ ์ด์ „์˜ ์ˆ˜๋“ค์„ ๋ชจ๋‘ ํƒ์ƒ‰ํ•˜๋ฉด, ์ตœ์ข…์ ์œผ๋กœ ํ˜„์žฌ ์ˆ˜์˜ dp๊ฐ’์€ ์ด..
[BOJ] ์˜ฌ๋ฆผํ”ฝ ์ฝ”๋“œ a,b = map(int,input().split()) world = [] for _ in range(a): num = list(map(int,input().split())) world.append(num) world.sort(key = lambda x: (x[1],x[2],x[3]),reverse=True) for i in range(len(world)): if world[i][0] == b: index = i for i in range(len(world)): if world[index][1:] == world[i][1:]: print(i+1) break ๋ถ„์„ > ์ƒˆ๋กญ๊ฒŒ ์•Œ๊ฒŒ ๋œ ๊ฐœ๋… ์ค‘ ํ•˜๋‚˜๋Š”, ๋ฐฐ์—ด์„ ์ˆœ์„œ๋Œ€๋กœ ๊ธฐ์ค€์„ ์ •ํ•ด์„œ ์ •๋ ฌ์„ ํ•  ๋•Œ (x[1],x[2],x[3]) ์™€ ๊ฐ™์ด ๋žŒ๋‹คํ•จ์ˆ˜๋ฅผ ์ด์šฉํ•ด..
[BOJ] ๊ณตํ†ต ๋ถ€๋ถ„ ๋ฌธ์ž์—ด ๋ฌธ์ œ ๋‘ ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์กŒ์„ ๋•Œ, ๋‘ ๋ฌธ์ž์—ด์— ๋ชจ๋‘ ํฌํ•จ๋œ ๊ฐ€์žฅ ๊ธด ๊ณตํ†ต ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์„ ์ฐพ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค. ์–ด๋–ค ๋ฌธ์ž์—ด s์˜ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด t๋ž€, s์— t๊ฐ€ ์—ฐ์†์œผ๋กœ ๋‚˜ํƒ€๋‚˜๋Š” ๊ฒƒ์„ ๋งํ•œ๋‹ค. ์˜ˆ๋ฅผ ๋“ค์–ด, ๋ฌธ์ž์—ด ABRACADABRA์˜ ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์€ ABRA, RAC, D, ACADABRA, ABRACADABRA, ๋นˆ ๋ฌธ์ž์—ด ๋“ฑ์ด๋‹ค. ํ•˜์ง€๋งŒ, ABRC, RAA, BA, K๋Š” ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์ด ์•„๋‹ˆ๋‹ค. ๋‘ ๋ฌธ์ž์—ด ABRACADABRA์™€ ECADADABRBCRDARA์˜ ๊ณตํ†ต ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์€ CA, CADA, ADABR, ๋นˆ ๋ฌธ์ž์—ด ๋“ฑ์ด ์žˆ๋‹ค. ์ด ์ค‘์—์„œ ๊ฐ€์žฅ ๊ธด ๊ณตํ†ต ๋ถ€๋ถ„ ๋ฌธ์ž์—ด์€ ADABR์ด๋ฉฐ, ๊ธธ์ด๋Š” 5์ด๋‹ค. ๋˜, ๋‘ ๋ฌธ์ž์—ด์ด UPWJCIRUCAXIIRGL์™€ SBQNYBSBZDFNEV์ธ ๊ฒฝ์šฐ์—๋Š” ๊ฐ€์žฅ ๊ธด ๊ณต..