์•Œ๊ณ ๋ฆฌ์ฆ˜

๋ฐฑ์ค€ 1181๋ฒˆ[๋‹จ์–ด ์ •๋ ฌ] ๋ฌธ์ œํ’€์ด

ghtis1798 2021. 3. 6. 11:53

1181 : ๋‹จ์–ด ์ •๋ ฌ

๐Ÿ“Œ๋ฌธ์ œ ์„ค๋ช…

BOJ ๋‹จ์–ด ์ •๋ ฌ - 1181
์ž…๋ ฅ ์˜ˆ์ œ
์ถœ๋ ฅ ์˜ˆ์ œ

๐Ÿ“Œ๋ฌธ์ œ ์ ‘๊ทผ ๋ฐฉ์‹

1. ์ค‘๋ณต๋œ ๋‹จ์–ด๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค.

2. ๊ธ€์ž ์ˆ˜๋กœ ์ •๋ ฌ ํ›„ ์•ŒํŒŒ๋ฒณ ์‚ฌ์ „์ˆœ์œผ๋กœ ์ •๋ ฌํ•œ๋‹ค.

 

๐Ÿ“Œ๋ชฐ๋ž๋˜ ์ 

sorting ๊ธฐ์ค€์„ lambda์‹์œผ๋กœ ์ •์˜ํ•  ๋•Œ 2๊ฐ€์ง€ ๊ธฐ์ค€์„ ์„ค์ •ํ•  ๊ฒƒ

์ค‘๋ณต๋œ ๋‹จ์–ด๋ฅผ ์ œ๊ฑฐํ•˜๋Š” ๊ฑด set ์ž๋ฃŒ๊ตฌ์กฐ๋ฅผ ์‚ฌ์šฉํ•ด ๊ฐ„๋‹จํžˆ ํ•ด๊ฒฐํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

2๋‹จ๊ณ„์—์„œ ๊ธ€์ž ์ˆ˜๋กœ ์ •๋ ฌํ•˜๋Š” ๊ฒƒ๋„ ๋ฌธ์ œ์—†์ด ์ˆ˜ํ–‰ํ–ˆ์œผ๋‚˜

์•ŒํŒŒ๋ฒณ ์‚ฌ์ „์ˆœ์œผ๋กœ ์ •๋ ฌํ•˜๋Š” ๊ณผ์ •์—์„œ ํ—ท๊ฐˆ๋ ธ๋˜ ๋ถ€๋ถ„์€ ๋ฌธ์ž์—ด ์ •๋ ฌ์„ ์–ด๋–ป๊ฒŒ ํ•  ๊ฒƒ์ธ๊ฐ€์˜€๋‹ค.

lambda ์‹์„ ์ด์šฉํ•ด ์ •๋ ฌํ•  ๋•Œ 2๊ฐœ์˜ ๊ธฐ์ค€์„ ์ฃผ๊ณ  ์‹ถ์—ˆ๋Š”๋ฐ ๋ฌธ๋ฒ•์ด ๊ธฐ์–ต๋‚˜์ง€ ์•Š์•˜๋‹ค.

๊ทธ๋ž˜์„œ ์ง์ ‘ ๋ฌธ์ž์—ด ์ •๋ ฌ์„ ํ•˜๋Š” ํ•จ์ˆ˜๋กœ ๊ตฌํ˜„ํ•˜๋‹ค๋ณด๋‹ˆ ์‹œ๊ฐ„์ด ์˜ค๋ž˜๊ฑธ๋ ธ๋‹ค.

sortํ•จ์ˆ˜์˜ keyํŒŒ๋ผ๋ฏธํ„ฐ๋กœ lambda ์‹์„ ๋„˜๊ฒจ์ค„ ๋•Œ ๊ธ€์ž์ˆ˜์™€ ๋ฌธ์ž์—ด์„ ์„ค์ •ํ•˜๋ฉด ๊ฐ„๋‹จํ•œ ๋ฌธ์ œ์˜€๋‹ค.

๐Ÿ“Œ์†Œ์Šค์ฝ”๋“œ 

import sys

n = int(sys.stdin.readline())
words = [sys.stdin.readline().strip() for i in range(n)]

def solution(words):
    result = list(set(words))
    result.sort(key = lambda word : (len(word), word))
    return result

result = solution(words)
for answer in result:
    print(answer)