๋‚ด ์ธ์ƒ์—์„œ ๋ฏฟ์„ ๊ฑด ์˜ค์ง ๋‚˜ ์ž์‹ ๋ฟ!

The only one you can truly trust is yourself.

๊ฒŒ์ž„ ํ”„๋กœ๊ทธ๋ž˜๋ฐ/Python ํ”„๋กœ๊ทธ๋ž˜๋ฐ

ํ”„๋กœ๊ทธ๋ž˜๋จธ์Šค [3์ฐจ] ์••์ถ•

๐ŸŽฎinspirer9 2023. 2. 26. 22:56
728x90
๋ฐ˜์‘ํ˜•

๋ฌธ์ œ ์„ค๋ช…

์‹ ์ž…์‚ฌ์› ์–ดํ”ผ์น˜๋Š” ์นด์นด์˜คํ†ก์œผ๋กœ ์ „์†ก๋˜๋Š” ๋ฉ”์‹œ์ง€๋ฅผ ์••์ถ•ํ•˜์—ฌ ์ „์†ก ํšจ์œจ์„ ๋†’์ด๋Š” ์—…๋ฌด๋ฅผ ๋งก๊ฒŒ ๋˜์—ˆ๋‹ค. ๋ฉ”์‹œ์ง€๋ฅผ ์••์ถ•ํ•˜๋”๋ผ๋„ ์ „๋‹ฌ๋˜๋Š” ์ •๋ณด๊ฐ€ ๋ฐ”๋€Œ์–ด์„œ๋Š” ์•ˆ ๋˜๋ฏ€๋กœ, ์••์ถ• ์ „์˜ ์ •๋ณด๋ฅผ ์™„๋ฒฝํ•˜๊ฒŒ ๋ณต์› ๊ฐ€๋Šฅํ•œ ๋ฌด์†์‹ค ์••์ถ• ์•Œ๊ณ ๋ฆฌ์ฆ˜์„ ๊ตฌํ˜„ํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค.

์–ดํ”ผ์น˜๋Š” ์—ฌ๋Ÿฌ ์••์ถ• ์•Œ๊ณ ๋ฆฌ์ฆ˜ ์ค‘์—์„œ ์„ฑ๋Šฅ์ด ์ข‹๊ณ  ๊ตฌํ˜„์ด ๊ฐ„๋‹จํ•œ LZW(Lempel–Ziv–Welch) ์••์ถ•์„ ๊ตฌํ˜„ํ•˜๊ธฐ๋กœ ํ–ˆ๋‹ค. LZW ์••์ถ•์€ 1983๋…„ ๋ฐœํ‘œ๋œ ์•Œ๊ณ ๋ฆฌ์ฆ˜์œผ๋กœ, ์ด๋ฏธ์ง€ ํŒŒ์ผ ํฌ๋งท์ธ GIF ๋“ฑ ๋‹ค์–‘ํ•œ ์‘์šฉ์—์„œ ์‚ฌ์šฉ๋˜์—ˆ๋‹ค.

LZW ์••์ถ•์€ ๋‹ค์Œ ๊ณผ์ •์„ ๊ฑฐ์นœ๋‹ค.

  1. ๊ธธ์ด๊ฐ€ 1์ธ ๋ชจ๋“  ๋‹จ์–ด๋ฅผ ํฌํ•จํ•˜๋„๋ก ์‚ฌ์ „์„ ์ดˆ๊ธฐํ™”ํ•œ๋‹ค.
  2. ์‚ฌ์ „์—์„œ ํ˜„์žฌ ์ž…๋ ฅ๊ณผ ์ผ์น˜ํ•˜๋Š” ๊ฐ€์žฅ ๊ธด ๋ฌธ์ž์—ด w๋ฅผ ์ฐพ๋Š”๋‹ค.
  3. w์— ํ•ด๋‹นํ•˜๋Š” ์‚ฌ์ „์˜ ์ƒ‰์ธ ๋ฒˆํ˜ธ๋ฅผ ์ถœ๋ ฅํ•˜๊ณ , ์ž…๋ ฅ์—์„œ w๋ฅผ ์ œ๊ฑฐํ•œ๋‹ค.
  4. ์ž…๋ ฅ์—์„œ ์ฒ˜๋ฆฌ๋˜์ง€ ์•Š์€ ๋‹ค์Œ ๊ธ€์ž๊ฐ€ ๋‚จ์•„์žˆ๋‹ค๋ฉด(c), w+c์— ํ•ด๋‹นํ•˜๋Š” ๋‹จ์–ด๋ฅผ ์‚ฌ์ „์— ๋“ฑ๋กํ•œ๋‹ค.
  5. ๋‹จ๊ณ„ 2๋กœ ๋Œ์•„๊ฐ„๋‹ค.

์••์ถ• ์•Œ๊ณ ๋ฆฌ์ฆ˜์ด ์˜๋ฌธ ๋Œ€๋ฌธ์ž๋งŒ ์ฒ˜๋ฆฌํ•œ๋‹ค๊ณ  ํ•  ๋•Œ, ์‚ฌ์ „์€ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ดˆ๊ธฐํ™”๋œ๋‹ค. ์‚ฌ์ „์˜ ์ƒ‰์ธ ๋ฒˆํ˜ธ๋Š” ์ •์ˆ˜๊ฐ’์œผ๋กœ ์ฃผ์–ด์ง€๋ฉฐ, 1๋ถ€ํ„ฐ ์‹œ์ž‘ํ•œ๋‹ค๊ณ  ํ•˜์ž.

์ƒ‰์ธ ๋ฒˆํ˜ธ123...242526

๋‹จ์–ด A B C ... X Y Z

์˜ˆ๋ฅผ ๋“ค์–ด ์ž…๋ ฅ์œผ๋กœ KAKAO๊ฐ€ ๋“ค์–ด์˜จ๋‹ค๊ณ  ํ•˜์ž.

  1. ํ˜„์žฌ ์‚ฌ์ „์—๋Š” KAKAO์˜ ์ฒซ ๊ธ€์ž K๋Š” ๋“ฑ๋ก๋˜์–ด ์žˆ์œผ๋‚˜, ๋‘ ๋ฒˆ์งธ ๊ธ€์ž๊นŒ์ง€์ธ KA๋Š” ์—†์œผ๋ฏ€๋กœ, ์ฒซ ๊ธ€์ž K์— ํ•ด๋‹นํ•˜๋Š” ์ƒ‰์ธ ๋ฒˆํ˜ธ 11์„ ์ถœ๋ ฅํ•˜๊ณ , ๋‹ค์Œ ๊ธ€์ž์ธ A๋ฅผ ํฌํ•จํ•œ KA๋ฅผ ์‚ฌ์ „์— 27 ๋ฒˆ์งธ๋กœ ๋“ฑ๋กํ•œ๋‹ค.
  2. ๋‘ ๋ฒˆ์งธ ๊ธ€์ž A๋Š” ์‚ฌ์ „์— ์žˆ์œผ๋‚˜, ์„ธ ๋ฒˆ์งธ ๊ธ€์ž๊นŒ์ง€์ธ AK๋Š” ์‚ฌ์ „์— ์—†์œผ๋ฏ€๋กœ, A์˜ ์ƒ‰์ธ ๋ฒˆํ˜ธ 1์„ ์ถœ๋ ฅํ•˜๊ณ , AK๋ฅผ ์‚ฌ์ „์— 28 ๋ฒˆ์งธ๋กœ ๋“ฑ๋กํ•œ๋‹ค.
  3. ์„ธ ๋ฒˆ์งธ ๊ธ€์ž์—์„œ ์‹œ์ž‘ํ•˜๋Š” KA๊ฐ€ ์‚ฌ์ „์— ์žˆ์œผ๋ฏ€๋กœ, KA์— ํ•ด๋‹นํ•˜๋Š” ์ƒ‰์ธ ๋ฒˆํ˜ธ 27์„ ์ถœ๋ ฅํ•˜๊ณ , ๋‹ค์Œ ๊ธ€์ž O๋ฅผ ํฌํ•จํ•œ KAO๋ฅผ 29 ๋ฒˆ์งธ๋กœ ๋“ฑ๋กํ•œ๋‹ค.
  4. ๋งˆ์ง€๋ง‰์œผ๋กœ ์ฒ˜๋ฆฌ๋˜์ง€ ์•Š์€ ๊ธ€์ž O์— ํ•ด๋‹นํ•˜๋Š” ์ƒ‰์ธ ๋ฒˆํ˜ธ 15๋ฅผ ์ถœ๋ ฅํ•œ๋‹ค.

ํ˜„์žฌ ์ž…๋ ฅ(w)๋‹ค์Œ ๊ธ€์ž(c)์ถœ๋ ฅ์‚ฌ์ „ ์ถ”๊ฐ€(w+c)

K A 11 27: KA
A K 1 28: AK
KA O 27 29: KAO
O   15  

์ด ๊ณผ์ •์„ ๊ฑฐ์ณ ๋‹ค์„ฏ ๊ธ€์ž์˜ ๋ฌธ์žฅ KAKAO๊ฐ€ 4๊ฐœ์˜ ์ƒ‰์ธ ๋ฒˆํ˜ธ [11, 1, 27, 15]๋กœ ์••์ถ•๋œ๋‹ค.

์ž…๋ ฅ์œผ๋กœ TOBEORNOTTOBEORTOBEORNOT๊ฐ€ ๋“ค์–ด์˜ค๋ฉด ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์••์ถ•์ด ์ง„ํ–‰๋œ๋‹ค.

ํ˜„์žฌ ์ž…๋ ฅ(w)๋‹ค์Œ ๊ธ€์ž(c)์ถœ๋ ฅ์‚ฌ์ „ ์ถ”๊ฐ€(w+c)

T O 20 27: TO
O B 15 28: OB
B E 2 29: BE
E O 5 30: EO
O R 15 31: OR
R N 18 32: RN
N O 14 33: NO
O T 15 34: OT
T T 20 35: TT
TO B 27 36: TOB
BE O 29 37: BEO
OR T 31 38: ORT
TOB E 36 39: TOBE
EO R 30 40: EOR
RN O 32 41: RNO
OT   34

์ œํ•œ ์‚ฌํ•ญ

์ž…๋ ฅ ํ˜•์‹

์ž…๋ ฅ์œผ๋กœ ์˜๋ฌธ ๋Œ€๋ฌธ์ž๋กœ๋งŒ ์ด๋ค„์ง„ ๋ฌธ์ž์—ด msg๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. msg์˜ ๊ธธ์ด๋Š” 1 ๊ธ€์ž ์ด์ƒ, 1000 ๊ธ€์ž ์ดํ•˜์ด๋‹ค.

์ถœ๋ ฅ ํ˜•์‹

์ฃผ์–ด์ง„ ๋ฌธ์ž์—ด์„ ์••์ถ•ํ•œ ํ›„์˜ ์‚ฌ์ „ ์ƒ‰์ธ ๋ฒˆํ˜ธ๋ฅผ ๋ฐฐ์—ด๋กœ ์ถœ๋ ฅํ•˜๋ผ.

์ž…์ถœ๋ ฅ ์˜ˆ

 

msg answer
KAKAO [11, 1, 27, 15]
TOBEORNOTTOBEORTOBEORNOT [20, 15, 2, 5, 15, 18, 14, 15, 20, 27, 29, 31, 36, 30, 32, 34]
ABABABABABABABAB [1, 2, 27, 29, 28, 31, 30]

์ž…์ถœ๋ ฅ ์˜ˆ ์„ค๋ช…

ํ•ด์„ค ๋ณด๋Ÿฌ๊ฐ€๊ธฐ

ํ’€์ด

  • ๋ฐฐ์—ด๋กœ ํ–ˆ๋‹ค๊ฐ€ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ํ–ˆ๋‹ค๊ฐ€ ๋‹ค์‹œ ๋ฐฐ์—ด๋กœ ํ–ˆ๋‹ค๊ฐ€ ๋”•์…”๋„ˆ๋ฆฌ๋กœ ํ–ˆ๋‹ค๊ฐ€ ใ…‹ใ…‹ใ…‹
  • ๊ฐˆํŒก์งˆํŒก ๋ฌด์ง€์„ฑ ์ฝ”๋”ฉ ใ…‹ใ…‹ใ…‹
    • ๋ฐฐ์—ด๋กœ ํ–ˆ๋˜ ๊ฑด A-B-C-D-E ์ด๋ ‡๊ฒŒ ๋งŒ๋“ค๊ณ 
      • A-B-C-D-E
    •  AND์™€ DO, BAT๊ฐ€ ๋“ค์–ด์˜ค๋ฉด, ์ด๋Ÿฐ ์‹์œผ๋กœ ํ•ด๋ณผ๊นŒ ํ–ˆ๋‹ค๊ฐ€ ์–ด์šฐ์•ผ ๋„˜ ๋ณต์žกํ•ด...ใ…‹ใ…‹ใ…‹
      • A      - B      - C - D - E
      • ใ„ดN     ใ„ดA          ใ„ดO
      •     ใ„ดD    ใ„ดB
def solution(msg):
    answer = []
    
    ์ƒ‰์ธ๋ฒˆํ˜ธ = 26
    ์‚ฌ์ „ = {}
    for i in range(26):
        ์‚ฌ์ „[chr(65+i)] = i+1
    
    index = 0
    while index < len(msg):
        for i in range(index, len(msg)):
            ์ฐพ์„๋‹จ์–ด = msg[index:i+1]
            if ์ฐพ์„๋‹จ์–ด not in ์‚ฌ์ „:
                ์‚ฌ์ „[์ฐพ์„๋‹จ์–ด] = len(์‚ฌ์ „)+1
                answer.append(์‚ฌ์ „[msg[index:i]])
                index += len(msg[index:i])
        else:
            answer.append(์‚ฌ์ „[msg[index:]])
            index += len(msg[index:])

    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.16ms, 10.4MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.24ms, 10.3MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.38ms, 10.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.22ms, 10.4MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.4MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.31ms, 10.3MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.25ms, 10.2MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.24ms, 10.3MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.36ms, 10.4MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (0.34ms, 10.3MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (0.64ms, 10.3MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (0.27ms, 10.4MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (0.22ms, 10.3MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (0.08ms, 10.4MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (0.14ms, 10.4MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (0.21ms, 10.3MB)
  • ๊น”๋”... for else ๋ฌธ์„ ์“ฐ์ง€ ์•Š๊ณ  ์ด๋ ‡๊ฒŒ ์“ฐ๋Š”๊ฒŒ ๋” ์ข‹์•˜์„ ๋“ฏ ์‹ถ๋‹ค.
def solution(msg):
    alphabet = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
    d = {k:v for (k,v) in zip(alphabet, list(range(1,27)))}
    answer = []

    while True:
        if msg in d:
            answer.append(d[msg])
            break
        for i in range(1, len(msg)+1):
            if msg[0:i] not in d:
                answer.append(d[msg[0:i-1]])
                d[msg[0:i]] = len(d)+1
                msg = msg[i-1:]
                break

    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.1MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.1MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.1MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.28ms, 10.1MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.03ms, 10MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.75ms, 10.4MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.30ms, 10.1MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.28ms, 10.1MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.3MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.45ms, 10.1MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.46ms, 10.1MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.36ms, 10.1MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.72ms, 10.3MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (0.70ms, 10.2MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (1.20ms, 10.1MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (0.79ms, 10.2MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (0.40ms, 10.2MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (0.18ms, 10.2MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (0.27ms, 10.1MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (0.60ms, 10.2MB)
  • ์š”๋ฒˆ์— ์•Œ๊ฒŒ ๋œ ๊ฑฐ
    • for ... else๋ฌธ
      • for๋ฌธ์„ ๋‹ค ์‹คํ–‰ํ•˜๊ณ  ๋‚˜๋ฉด else ๋ฌธ์ด ์‹คํ–‰๋œ๋‹ค.
    • ๋”•์…”๋„ˆ๋ฆฌ๋„ for๋ฌธ์œผ๋กœ ์ƒ์„ฑํ•  ์ˆ˜ ์žˆ๋‹ค.
      • ์ด๋ ‡๊ฒŒ ์•ˆ์งœ๋„ ๋จ
์‚ฌ์ „ = {}
    for i in range(26):
        ์‚ฌ์ „[chr(65+i)] = i+1
  • ๋Œ€์‹  ์ด๋ ‡๊ฒŒ ์งค ์ˆ˜ ์žˆ๋‹ค.
    • ํ•˜๋Š” ๊น€์— ๋” ํŠœ๋‹...
def solution(msg):
    answer = []
    
    ์‚ฌ์ „ = {chr(65+i):i+1 for i in range(26)}
    
    index = 0
    while index < len(msg):
        for i in range(index, len(msg)):
            ์ฐพ์„๋‹จ์–ด = msg[index:i+1]
            if ์ฐพ์„๋‹จ์–ด not in ์‚ฌ์ „:
                ์‚ฌ์ „[์ฐพ์„๋‹จ์–ด] = len(์‚ฌ์ „)+1
                answer.append(์‚ฌ์ „[msg[index:i]])
                index += len(msg[index:i])
        else:
            answer.append(์‚ฌ์ „[msg[index:]])
            index += len(msg[index:])

    return answer
    
์ •ํ™•์„ฑ  ํ…Œ์ŠคํŠธ
ํ…Œ์ŠคํŠธ 1 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.4MB)
ํ…Œ์ŠคํŠธ 2 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.2MB)
ํ…Œ์ŠคํŠธ 3 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.4MB)
ํ…Œ์ŠคํŠธ 4 ใ€‰	ํ†ต๊ณผ (0.16ms, 10.2MB)
ํ…Œ์ŠคํŠธ 5 ใ€‰	ํ†ต๊ณผ (0.02ms, 10.3MB)
ํ…Œ์ŠคํŠธ 6 ใ€‰	ํ†ต๊ณผ (0.24ms, 10.4MB)
ํ…Œ์ŠคํŠธ 7 ใ€‰	ํ†ต๊ณผ (0.19ms, 10.3MB)
ํ…Œ์ŠคํŠธ 8 ใ€‰	ํ†ต๊ณผ (0.40ms, 10.5MB)
ํ…Œ์ŠคํŠธ 9 ใ€‰	ํ†ต๊ณผ (0.01ms, 10.2MB)
ํ…Œ์ŠคํŠธ 10 ใ€‰	ํ†ต๊ณผ (0.39ms, 10.4MB)
ํ…Œ์ŠคํŠธ 11 ใ€‰	ํ†ต๊ณผ (0.16ms, 10.3MB)
ํ…Œ์ŠคํŠธ 12 ใ€‰	ํ†ต๊ณผ (0.25ms, 10.3MB)
ํ…Œ์ŠคํŠธ 13 ใ€‰	ํ†ต๊ณผ (0.65ms, 10.4MB)
ํ…Œ์ŠคํŠธ 14 ใ€‰	ํ†ต๊ณผ (0.55ms, 10.3MB)
ํ…Œ์ŠคํŠธ 15 ใ€‰	ํ†ต๊ณผ (0.37ms, 10.4MB)
ํ…Œ์ŠคํŠธ 16 ใ€‰	ํ†ต๊ณผ (0.27ms, 10.4MB)
ํ…Œ์ŠคํŠธ 17 ใ€‰	ํ†ต๊ณผ (0.22ms, 10.3MB)
ํ…Œ์ŠคํŠธ 18 ใ€‰	ํ†ต๊ณผ (0.13ms, 10.2MB)
ํ…Œ์ŠคํŠธ 19 ใ€‰	ํ†ต๊ณผ (0.20ms, 10.4MB)
ํ…Œ์ŠคํŠธ 20 ใ€‰	ํ†ต๊ณผ (0.21ms, 10.2MB)

 

728x90
๋ฐ˜์‘ํ˜•