๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Algorithm/๋ฐฑ์ค€(BOJ)

[ ํŒŒ์ด์ฌ(python) ] ๋ฐฑ์ค€ 1157 - ๋‹จ์–ด ๊ณต๋ถ€

by YWTechIT 2021. 5. 13.
728x90

๐Ÿ“ ๋ฐฑ์ค€ 1157 - ๋‹จ์–ด ๊ณต๋ถ€

๋ฐฑ์ค€ 1157 - ๋‹จ์–ด ๊ณต๋ถ€


๐Ÿ’ก ๋‚˜์˜ ํ’€์ด

์–ธ์  ๊ฐ€ ์ด ๋ฌธ์ œ๋ฅผ ๋ณด๊ณ  ํ’€ ์—„๋‘๊ฐ€ ๋‚˜์ง€ ์•Š์•„์„œ ๋ถ๋งˆํฌ๋กœ ์ถ”๊ฐ€๋งŒ ํ–ˆ์—ˆ๋Š”๋ฐ, ์˜ค๋Š˜ ๋‹ค์‹œ๋ณด๋‹ˆ๊นŒ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋‹ค. ์•ฝ๊ฐ„ ๊นŒ๋‹ค๋กœ์šด ๋ฌธ์ œ์ธ๋ฐ count, index ํ•จ์ˆ˜๋ฅผ ์‚ฌ์šฉํ•˜๋ฉด ๊ธˆ๋ฐฉ ํ’€ ์ˆ˜ ์žˆ์—ˆ๋‹ค. ๋จผ์ € for๋ฌธ์„ ํ’€์–ด์„œ ์ž‘์„ฑํ•˜๊ณ  ์ดํ›„์— list_comprehension์œผ๋กœ ์ฝ”๋“œ๋ฅผ ๋‹จ์ถ•์‹œ์ผฐ๋‹ค. ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๋„๋ก ์˜ˆ์ œ ์ž…๋ ฅ์— ๋‚˜์™€์žˆ๋Š” Mississipi๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์•Œ์•„๋ณด์ž.

 

  • ์ž…๋ ฅ์„ ๋Œ€๋ฌธ์ž or ์†Œ๋ฌธ์ž๋กœ ํ†ต์ผ ์‹œํ‚จ๋‹ค. (์ดํ›„์— set์œผ๋กœ ๋ณ€ํ™˜ํ•˜๊ธฐ ์œ„ํ•จ.)
  • ์ž…๋ ฅ์„ set์œผ๋กœ ํ˜• ๋ณ€ํ™˜์„ ์‹œ์ผœ ์ค‘๋ณต์„ ์ œ๊ฑฐํ•œ๋‹ค.
  • setํ˜•์œผ๋กœ for๋ฌธ์„ ๋Œ๋ ค ์ž…๋ ฅ์— i๊ฐ’์ด ๋ช‡ ๋ฒˆ ์“ฐ์˜€๋Š”์ง€ ํ™•์ธํ•˜๊ณ  ๋นˆ ๋ฆฌ์ŠคํŠธ์— append์‹œํ‚จ๋‹ค.
  • count_list = [1, 4, 1, 4]๊ฐ€ ๋‚˜์˜ค๊ฒŒ ๋˜๋Š”๋ฐ ์ด๋Š” compress_s = ['S', 'P', 'I', 'M']๊ฐ€ ๋ช‡ ๋ฒˆ ์‚ฌ์šฉ๋๋Š”์ง€ countํ•œ ๊ฐ’์ด๋‹ค.
  • ์กฐ๊ฑด๋ฌธ์„ ํ†ตํ•ด ๋งŒ์•ฝ, [1, 4, 1, 4]์˜ count(max_index) ์ฆ‰, max๊ฐ’์ด 2๊ฐœ ์ด์ƒ(max๊ฐ’์ด 2๊ฐœ๋ฉด ๊ฐ€์žฅ ๋งŽ์ด ์‚ฌ์šฉ๋œ ์•ŒํŒŒ๋ฒณ์˜ ๊ฐฏ์ˆ˜๊ฐ€ ๋™์ผํ•˜๋‹ค๋Š” ์–˜๊ธฐ๋‹ค.)์ด๋ฉด ?๋ฅผ ์ถœ๋ ฅํ•˜๊ณ 
  • ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด, count_list.index(max(count_list)) ์ฆ‰, count_list ์ค‘ count๊ฐ€ ๊ฐ€์žฅ ๋งŽ์ด ์„ ์–ธ๋œ index์˜ ์œ„์น˜๋ฅผ ๋ฐ˜ํ™˜ํ•˜๊ณ  ๊ทธ ๊ฐ’์„ count_list[index]์— ๋„ฃ์–ด์ฃผ๋ฉด ์–ด๋–ค ๊ฐ’์ด ๊ฐ€์žฅ ๋งŽ์ด ์“ฐ์˜€๋Š”์ง€ ์•Œ ์ˆ˜ ์žˆ๋‹ค.
    ์ด๋•Œ, compress_s๋Š” list๋กœ ๋ณ€๊ฒฝํ•ด์•ผ index๋ฅผ ๋Œ€์ž… ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • 6๋ฒˆ์˜ ๊ณผ์ •์„ ํ•˜๋‚˜์”ฉ ์‚ดํŽด๋ณด๊ธฐ ์ „์— ๋จผ์ €, set์€ ์ˆœ์„œ๊ฐ€ ์ •ํ•ด์ ธ์žˆ์ง€ ์•Š๊ธฐ๋•Œ๋ฌธ์— ๊ฒฐ๊ณผ๊ฐ’์ด ๋ฐ”๋€” ์ˆ˜๋„ ์žˆ๋‹ค.
  • print(count_list.index(max(count_list))) = 1์ด๊ณ , compress_s[1]๊ณผ ๊ฐ™๋‹ค.
  • compress_s = ['A', 'B'] ๋”ฐ๋ผ์„œ, compress_s[1] = B์ด๋‹ค.
728x90

์กฐ๊ธˆ ๋ณต์žกํ•˜์ง€๋งŒ ๊ตฌํ˜„ ๊ณผ์ •์„ ์ฒœ์ฒœํžˆ ์†์œผ๋กœ ์จ๋‚ด๋ ค๊ฐ€๋ฉด ๊ธˆ๋ฐฉ ์ดํ•ด ํ•  ์ˆ˜ ์žˆ๋‹ค.

s = input().upper()
compress_s = list(set(s))
count_list = [s.count(i) for i in compress_s]

if count_list.count(max(count_list)) > 1:
    print('?')
else:
    print(compress_s[count_list.index(max(count_list))])
๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€