Post

๐Ÿน ZOAC

1. ๋ฌธ์ œ ๋งํฌ

16719๋ฒˆ: ZOAC


2. ์ฝ”๋“œ

Python3 31256KB 40ms

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# ์ฒ˜์Œ ์ž…๋ ฅ๋ฐ›๋Š” ๋‹จ์–ด ๋ฆฌ์ŠคํŠธ
start_word = list(input())
# ์ถœ๋ ฅ์„ ์œ„ํ•œ ๋‹จ์–ด ๋ฆฌ์ŠคํŠธ(๋นˆ์นธ์—์„œ ์ฑ„์›Œ๋‚˜๊ฐ€๊ธฐ)
final_word = [' '] * len(start_word)
# ์ฒ˜์Œ ์ž…๋ ฅ๋ฐ›์€ [๋‹จ์–ด, ์œ„์น˜] ๋ฆฌ์ŠคํŠธ
word = [[start_word[i], i] for i in range(len(start_word))]

# ์žฌ๊ท€๋ฅผ ์‚ฌ์šฉํ•œ ๋‹จ์–ด ์ถœ๋ ฅ
def print_word(word):
    # ๋นˆ๋ฐฐ์—ด์ผ ๋•Œ ์ข…๋ฃŒ
    if not word:
        return
    # ๋‹จ์–ด ๋ฐฐ์—ด ์•ŒํŒŒ๋ฒณ ์ˆœ์œผ๋กœ ์ •๋ ฌ
    sorted_word = sorted(word, key=lambda x:x[0])
    # ์•ŒํŒŒ๋ฒณ ์ค‘ ๊ฐ€์žฅ ์‚ฌ์ „์ˆœ์œผ๋กœ ๋น ๋ฅธ ์•ŒํŒŒ๋ฒณ ์ฒ˜์Œ ์œ„์น˜
    now_idx = sorted_word[0][1]
    # ์ถœ๋ ฅ์„ ์œ„ํ•œ ์•ŒํŒŒ๋ฒณ ์ฑ„์›Œ๋„ฃ๊ธฐ 
    final_word[now_idx] = start_word[now_idx]   # ' ' -> ์•ŒํŒŒ๋ฒณ
    # ์‚ฌ์šฉํ•œ ์•ŒํŒŒ๋ฒณ ๋‹จ์–ด ๋ฆฌ์ŠคํŠธ์—์„œ ์‚ญ์ œ
    start_word[now_idx] = ''
    # ์ฑ„์›Œ๋„ฃ์€ ์•ŒํŒŒ๋ฒณ ํ•ฉ์ณ์„œ ์ถœ๋ ฅ (๋นˆ์นธ์€ ์—†์• ๊ณ  ์ถœ๋ ฅ)
    print(''.join(final_word).replace(' ', ''))
    # ์‚ฌ์šฉํ•œ ์•ŒํŒŒ๋ฒณ ๋ถ€๋ถ„ ๋ฐฐ์—ด word์—์„œ์˜ ์ธ๋ฑ์Šค ์ฐพ๊ธฐ 
    for i in range(len(word)):
        if word[i][1] == now_idx:
            word_idx = i
    # ํ•ด๋‹น ์ธ๋ฑ์Šค๋ฅผ ๊ธฐ์ค€์œผ๋กœ ์ขŒ์šฐ๋กœ ๋‚˜๋ˆ„๊ธฐ
    print_word(word[word_idx+1:])   # ์˜ค๋ฅธ์ชฝ ๋จผ์ € ํƒ์ƒ‰
    print_word(word[:word_idx])     # ์™ผ์ชฝ ๋‚˜์ค‘์— ํƒ์ƒ‰

print_word(word)


3. ํ•ด์„ค

์žฌ๊ท€๋ฅผ ์ด์šฉํ•˜์—ฌ word ๋ฐฐ์—ด์„ ๋ถ„ํ• ํ•˜์—ฌ ํ•ด๊ฒฐํ•œ๋‹ค.

word ๋ฐฐ์—ด ์˜ˆ์‹œ : [[โ€™Zโ€™, 0], [โ€™Oโ€™, 1], [โ€™Aโ€™, 2], [โ€™Cโ€™, 3]]

word๋ฅผ ์•ŒํŒŒ๋ฒณ ์ˆœ์„œ๋กœ ์ •๋ ฌํ•˜๊ณ  ๊ฐ€์žฅ ์•ž ์ˆœ์„œ์ธ ์•ŒํŒŒ๋ฒณ์˜ ์œ„์น˜(word[0][1])์„ now_idx์— ์ €์žฅํ•œ๋‹ค.

ํ•ด๋‹น ์•ŒํŒŒ๋ฒณ์„ ์ถœ๋ ฅ ๋ฆฌ์ŠคํŠธ(final_word)์˜ ๊ฐ’์œผ๋กœ ์ดˆ๊ธฐํ™”ํ•˜๊ณ  ์ฒซ ๋‹จ์–ด ๋ฆฌ์ŠคํŠธ(start_word)์—์„œ๋Š” ์ œ๊ฑฐํ•ด์ค€๋‹ค.

๊ทธ ํ›„ ํ•ด๋‹น ์•ŒํŒŒ๋ฒณ์„ Pivot์œผ๋กœ ๋‘” ํ›„ ๋ฐฐ์—ด์„ ์ขŒ์šฐ๋กœ ๋‚˜๋ˆ„์–ด ์žฌ๊ท€๋กœ ์œ„์˜ ๋‹จ๊ณ„๋“ค์„ ๋‹ค์‹œ ์ง„ํ–‰ํ•œ๋‹ค. ์ด๋•Œ ์˜ค๋ฅธ์ชฝ ๋ฐฐ์—ด๋ถ€ํ„ฐ ํƒ์ƒ‰ํ•ด์ค€๋‹ค.(๋‹จ์–ด๊ฐ€ ์‚ฌ์ „์ˆœ์ด๋ ค๋ฉด ์˜ค๋ฅธ์ชฝ๋ถ€ํ„ฐ ์ฐพ์•„์ฃผ์–ด์•ผํ•œ๋‹ค.)

๋ฐฐ์—ด์„ ์ž‘๊ฒŒ ์ž๋ฅด๋‹ค๊ฐ€ ๋นˆ๋ฐฐ์—ด์ด ๋˜๋ฉด ์žฌ๊ท€์—์„œ ๋น ์ ธ๋‚˜์˜จ๋‹ค.

  • ๋ฐ˜๋ก€
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    
      ABAB
    
      # Output
      A
      AA
      ABA
      ABAB
    
      # Answer
      A
      AA
      AAB
      ABAB
    
This post is licensed under CC BY 4.0 by the author.