ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [Python] ๋ฐฑ์ค€ 16719 ZOAC (๊ตฌํ˜„)
    ๐Ÿ’ป Algorithm/Python 2022. 7. 8. 23:30

    ๐Ÿ“Œ ๋ฌธ์ œ

    2018๋…„ 12์›”, ์ฒ˜์Œ ์‹œ์ž‘ํ•˜๊ฒŒ ๋œ ZOAC์˜ ์˜คํ”„๋‹์„ ๋งก์€ ์„ฑ์šฐ๋Š” ๋ˆ„๊ตฌ๋ณด๋‹ค ํ™”๋ คํ•˜๊ฒŒ ZOAC๋ฅผ ์•Œ๋ฆฌ๋ ค ํ•œ๋‹ค.

     

    ์•ž ๊ธ€์ž๋ถ€ํ„ฐ ํ•˜๋‚˜์”ฉ ๋ณด์—ฌ์ฃผ๋Š” ๋ฐฉ์‹์€ ๋„ˆ๋ฌด ์‹์ƒํ•˜๋‹ค๊ณ  ์ƒ๊ฐํ•œ ์„ฑ์šฐ๋Š” ๋ฌธ์ž์—ด์„ ๋ณด์—ฌ์ฃผ๋Š” ์ƒˆ๋กœ์šด ๊ทœ์น™์„ ๊ณ ์•ˆํ•ด๋ƒˆ๋‹ค!

    ๊ทœ์น™์€ ์ด๋Ÿฌํ•˜๋‹ค. ์•„์ง ๋ณด์—ฌ์ฃผ์ง€ ์•Š์€ ๋ฌธ์ž ์ค‘ ์ถ”๊ฐ€ํ–ˆ์„ ๋•Œ์˜ ๋ฌธ์ž์—ด์ด ์‚ฌ์ „ ์ˆœ์œผ๋กœ ๊ฐ€์žฅ ์•ž์— ์˜ค๋„๋ก ํ•˜๋Š” ๋ฌธ์ž๋ฅผ ๋ณด์—ฌ์ฃผ๋Š” ๊ฒƒ์ด๋‹ค.

    ์˜ˆ๋ฅผ ๋“ค์–ด ZOAC๋ฅผ ๋ณด์—ฌ์ฃผ๊ณ  ์‹ถ๋‹ค๋ฉด, A → AC → OAC → ZOAC ์ˆœ์œผ๋กœ ๋ณด์—ฌ์ฃผ๋ฉด ๋œ๋‹ค.

    ๋ฐ”์œ ์„ฑ์šฐ๋ฅผ ์œ„ํ•˜์—ฌ ์ด ๊ทœ์น™๋Œ€๋กœ ์ถœ๋ ฅํ•ด์ฃผ๋Š” ํ”„๋กœ๊ทธ๋žจ์„ ์ž‘์„ฑํ•˜์‹œ์˜ค.

    ์ž…๋ ฅ

    ์ฒซ ๋ฒˆ์งธ ์ค„์— ์•ŒํŒŒ๋ฒณ ๋Œ€๋ฌธ์ž๋กœ ๊ตฌ์„ฑ๋œ ๋ฌธ์ž์—ด์ด ์ฃผ์–ด์ง„๋‹ค. ๋ฌธ์ž์—ด์˜ ๊ธธ์ด๋Š” ์ตœ๋Œ€ 100์ž์ด๋‹ค.

    ์ถœ๋ ฅ

    ๊ทœ์น™์— ๋งž๊ฒŒ ์ˆœ์„œ๋Œ€๋กœ ๋ฌธ์ž์—ด์„ ์ถœ๋ ฅํ•œ๋‹ค.

    ์˜ˆ์ œ ์ž…๋ ฅ 1

    ZOAC

    ์˜ˆ์ œ ์ถœ๋ ฅ 1

    A
    AC
    OAC
    ZOAC

    ์˜ˆ์ œ ์ž…๋ ฅ 2

    BAC

    ์˜ˆ์ œ ์ถœ๋ ฅ 2

    A
    AC
    BAC

    ์˜ˆ์ œ ์ž…๋ ฅ 3

    STARTLINK

    ์˜ˆ์ œ ์ถœ๋ ฅ 3

    A
    AI
    AIK
    AINK
    ALINK
    ARLINK
    ARTLINK
    SARTLINK
    STARTLINK

     


    ๐Ÿ“Œ ํ’€์ด

    ๐Ÿ’ฌ Code

    import sys
    input = sys.stdin.readline
    
    n = list(input().rstrip()) # STEPBACK
    
    # ์ž…๋ ฅ์ด ํ•œ ๊ธ€์ž๋ฉด ๊ทธ๋ƒฅ ๊ทธ๋Œ€๋กœ ์ถœ๋ ฅํ•ด์ฃผ๊ณ  ์ข…๋ฃŒ
    if len(n) == 1:
        print(n[0])
        sys.exit(0)
    
    dic = []
    for i in range(len(n)):
        dic.append((n[i], i))
    dic = sorted(dic) # ABCEKPST
    
    ans = [0 for _ in range(len(n))]
    ans[dic[0][1]] = dic[0][0]
    
    while dic:
        print(''.join([k for k in ans if k]))
        i = 1
        idx = 0
        isStart = 1
    
        while i < len(dic):
            add = ans.copy()
            add[dic[i][1]] = dic[i][0]
    
            if isStart:
                minimum = add
                isStart = 0
            elif ''.join([k for k in add if k]) < ''.join([k for k in minimum if k]):
                minimum = add
                idx = i
            i += 1
    
        ans = minimum
        del dic[idx]

    ๐Ÿ’ก Solution

    ๋ณธ๋ž˜ ๋ฌธ์ž์—ด์˜ ์ˆœ์„œ๋ฅผ ์œ ์ง€ํ•œ ์ฑ„ ์ถœ๋ ฅํ•ด์•ผ ํ•˜๊ธฐ ๋•Œ๋ฌธ์— ์ˆœ์„œ๋ฅผ ๊ธฐ์–ตํ•˜๊ธฐ ์œ„ํ•ด (๋ฌธ์ž, ์ธ๋ฑ์Šค)์Œ์ด ๋‹ด๊ธด ๋ฆฌ์ŠคํŠธ dic์„ ์ƒ์„ฑํ–ˆ์Šต๋‹ˆ๋‹ค.

     

    • ans: ์ถœ๋ ฅํ•  ๋ฌธ์ž๊ฐ€ ๋‹ด๊ธด ๋ฆฌ์ŠคํŠธ
    • add: ans์— ํ•œ ๋ฌธ์ž์”ฉ ์ถ”๊ฐ€ํ•ด๋ณธ ๋ฆฌ์ŠคํŠธ
    • minimum: ans์— ํ•œ ๋ฌธ์ž์”ฉ ์ถ”๊ฐ€ํ•ด๋ณด๋ฉด์„œ ์–ป์€ '๊ฐ€์žฅ ์ž‘์€ ๋ฌธ์ž์—ด'์ด ๋‹ด๊ธด ๋ฆฌ์ŠคํŠธ
    • i: ํ•œ ๋ฌธ์ž์”ฉ ์ถ”๊ฐ€ํ•  ๋•Œ i๋ฒˆ์งธ ๋ฌธ์ž๋ฅผ ์ถ”๊ฐ€ํ•˜๋Š” ๊ฒƒ
    • idx: '๊ฐ€์žฅ ์ž‘์€ ๋ฌธ์ž์—ด'์„ ๋งŒ๋“  ๋ฌธ์ž๋ฅผ ๊ฐ€๋ฆฌํ‚ค๋Š” ์ธ๋ฑ์Šค
    • isStart: minimum์€ ๊ณ„์† ๋น„๊ต๋ฅผ ํ†ตํ•ด์„œ ์—…๋ฐ์ดํŠธํ•ด์ค˜์•ผ ํ•˜๋Š”๋ฐ, ์ด๋ฒˆ์ด ์ฒซ ๋ฐ˜๋ณต๋ฌธ์ด๋ผ ์•„์ง ๋น„๊ต ๋Œ€์ƒ์ด ์—†์„ ๋•Œ๋ฅผ ๋‚˜ํƒ€๋‚ด๋Š” Flag

     

    BACK์ด๋ผ๋Š” ์ž…๋ ฅ์ด ๋“ค์–ด์™”๋‹ค๊ณ  ํ•ด ๋ณด๋ฉด.

     

     

    while๋ฌธ(dic์— ์›์†Œ๊ฐ€ ์žˆ๋Š” ๋™์•ˆ ๊ณ„์† ๋ฐ˜๋ณต)

     

    1๋ฒˆ์งธ while๋ฌธ

    • A ์ถœ๋ ฅ
    • BA, AC, AK ์ค‘ ๊ฐ€์žฅ ์ž‘์€ ๋ฌธ์ž์—ด์ธ AC ์ €์žฅ
    • ์ด ๋ฌธ์ž์—ด์„ ๋งŒ๋“œ๋Š” ๋ฐ ์‚ฌ์šฉ๋œ C๋ฅผ dic์—์„œ ์‚ญ์ œ

     

    2๋ฒˆ์งธ while๋ฌธ

    • AC ์ถœ๋ ฅ
    • BAC, ACK ์ค‘ ๊ฐ€์žฅ ์ž‘์€ ๋ฌธ์ž์—ด์ธ ACK ์ €์žฅ
    • ์ด ๋ฌธ์ž์—ด์„ ๋งŒ๋“œ๋Š” ๋ฐ ์‚ฌ์šฉ๋œ K๋ฅผ dic์—์„œ ์‚ญ์ œ

     

    3๋ฒˆ์งธ while๋ฌธ

    • ACK ์ถœ๋ ฅ
    • BACK ์ค‘ ๊ฐ€์žฅ ์ž‘์€ ๋ฌธ์ž์—ด์ธ BACK ์ €์žฅ
    • ์ด ๋ฌธ์ž์—ด์„ ๋งŒ๋“œ๋Š” ๋ฐ ์‚ฌ์šฉ๋œ B๋ฅผ dic์—์„œ ์‚ญ์ œ

     

    4๋ฒˆ์งธ while๋ฌธ

    • BACK ์ถœ๋ ฅ
    • dic์— ๋‚จ์•„์žˆ๋Š” A ์‚ญ์ œ

     

     

     

    ๋Œ“๊ธ€

Designed by Tistory.