ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [๋ฐฑ์ค€] 9081 ๋‹จ์–ด ๋งž์ถ”๊ธฐ with Python
    PS 2022. 3. 9. 01:10
    728x90
    ๋ฐ˜์‘ํ˜•

    ๐Ÿ“Œ BOJ 9081 ๋‹จ์–ด ๋งž์ถ”๊ธฐ

    ๐Ÿ’ก ์กฐ๊ฑด

    1. ๋‹จ์–ด๋ฅผ ์ฃผ๋ฉด ๊ทธ ๋‹จ์–ด๋ฅผ ์ด๋ฃจ๋Š” ์•ŒํŒŒ๋ฒณ๋“ค๋กœ ๋งŒ๋“ค ์ˆ˜ ์žˆ๋Š” ๋‹จ์–ด๋“ค์„ ์‚ฌ์ „ ์ˆœ์œผ๋กœ ์ •๋ ฌํ•  ๋•Œ์—
      ์ฃผ์–ด์ง„ ๋‹จ์–ด ๋‹ค์Œ์— ๋‚˜์˜ค๋Š” ๋‹จ์–ด๋ฅผ ์ฐพ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜๋Š” ๋ฌธ์ œ

    2. ์ผ€์ด์Šค์˜ ๊ฐœ์ˆ˜ T (1 โ‰ค T โ‰ค 10)

    3. ๋‹จ์–ด๋Š” ์•ŒํŒŒ๋ฒณ A~Z ๋Œ€๋ฌธ์ž๋กœ๋งŒ ์ด๋ฃจ์–ด์ง€๋ฉฐ ํ•ญ์ƒ ๊ณต๋ฐฑ์ด ์—†๋Š” ์—ฐ์†๋œ ์•ŒํŒŒ๋ฒณ์œผ๋กœ ์ด๋ฃจ์–ด์ง„๋‹ค.
      ๋‹จ์–ด์˜ ๊ธธ์ด๋Š” 100์„ ๋„˜์ง€ ์•Š๋Š”๋‹ค.

    4. ๊ตฌํ˜„ ์œ ํ˜•์˜ ๋ฌธ์ œ

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

    from sys import stdin
    
    for t in range(int(stdin.readline())):
        string = list(stdin.readline().rstrip())
        length = len(string)
        i, j = 0, 1
        for idx in range(1, length):
            if string[idx] > string[idx - 1]:
                if i < idx:
                    i = idx
    
        for idx in range(1, length):
            if string[idx] > string[i - 1]:
                if j < idx:
                    j = idx
    
        if i != 0 and j != 0:
            string[i-1], string[j] = string[j], string[i-1]
    
            string[i:] = list(reversed(string[i:]))
        print(''.join(string))

    ๐Ÿ”– ์˜ˆ์ œ ๋ฐ ์‹คํ–‰๊ฒฐ๊ณผ

    ์˜ˆ์ œ

    4
    HELLO
    DRINK
    SHUTTLE
    ZOO

    ์‹คํ–‰๊ฒฐ๊ณผ

    HELOL
    DRKIN
    SLEHTTU
    ZOO

    โŒจ๏ธ ๋ฌธ์ œ ํ’€์ด

    1. s[i-1] < s[i] ๋ฅผ ๋งŒ์กฑํ•˜๋Š” ์ตœ๋Œ€ i(s์˜ value) ์ฐพ๋Š”๋‹ค.

    2. j >= 1, s[i-1] < s[j] ๋ฅผ ๋งŒ์กฑํ•˜๋Š” ์ตœ๋Œ€ j(index) ์ฐพ๋Š”๋‹ค.

    3. s[i-1] ๊ณผ s[j] ๋ฅผ ๋ฐ”๊ฟ”์ค€ ๋’ค, s[i:]์˜ ๋ฐ์ดํ„ฐ๋ฅผ ๋’ค์ง‘์–ด ์ค€๋‹ค.

    ๐Ÿ’พ ๋Š๋‚€์ 

    1. ๋ฌธ์ œ๋ฅผ ํ’€๊ธฐ ์œ„ํ•ด์„œ ๋‹ค์Œ์— ์˜ค๋Š” ๋ฌธ์ž๋ฅผ ๊ตฌํ•˜๊ธฐ ์œ„ํ•œ ๊ณต์‹์ด ๋”ฐ๋กœ ์žˆ์—ˆ์Šต๋‹ˆ๋‹ค.
    2. cpp ์—์„œ๋Š” next_permutation ์„ ์‚ฌ์šฉํ•ด์„œ ํ’€๋ฉด ๋œ๋‹ค๊ณ  ํ–ˆ๋˜ ๊ฒƒ ๊ฐ™์€๋ฐ, ํŒŒ์ด์ฌ์€ ๊ทธ๊ฒŒ ์—†๋‹ค.
    ๋ฐ˜์‘ํ˜•

    ๋Œ“๊ธ€

Designed by Tistory.