๊ธฐ๋ก๋ฐฉ

BOJ_12933 : ์˜ค๋ฆฌ ๋ณธ๋ฌธ

CodingTest/Python

BOJ_12933 : ์˜ค๋ฆฌ

Soom_1n 2022. 7. 21. 16:41

๐Ÿ‘‰ ๋ฌธ์ œ๋งํฌ

 

12933๋ฒˆ: ์˜ค๋ฆฌ

์ฒซ์งธ ์ค„์— ์˜์„ ์ด๊ฐ€ ๋…น์Œํ•œ ์†Œ๋ฆฌ๊ฐ€ ์ฃผ์–ด์ง„๋‹ค. ์†Œ๋ฆฌ์˜ ๊ธธ์ด๋Š” 5๋ณด๋‹ค ํฌ๊ฑฐ๋‚˜ ๊ฐ™๊ณ , 2500๋ณด๋‹ค ์ž‘๊ฑฐ๋‚˜ ๊ฐ™์€ ์ž์—ฐ์ˆ˜์ด๊ณ , 'q','u','a','c','k'๋กœ๋งŒ ์ด๋ฃจ์–ด์ ธ ์žˆ๋‹ค.

www.acmicpc.net



๐Ÿ”ธ ๋ฌธ์ œ ๋ถ„์„ ๐Ÿ”ธ

  • ์šธ์Œ์†Œ๋ฆฌ "quack"๋ฅผ ํ™•์ธํ•ด์•ผํ•˜๋Š”๋ฐ, ์—ฌ๋Ÿฌ ์‚ฌ์ดํด์ด ์กด์žฌํ•  ์ˆ˜ ์žˆ๋‹ค.
  • ๋ถˆ๊ฐ€๋Šฅํ•œ ์กฐํ•ฉ์ด๋ผ๋ฉด -1์„ ์ถœ๋ ฅํ•ด์•ผ ํ•œ๋‹ค.

๐Ÿ”ธ ์ฝ”๋“œ ๐Ÿ”ธ

import sys
input = sys.stdin.readline
print = sys.stdout.write

string = input().rstrip()
strs = []       # ์˜ค๋ฆฌ ๋ณ„ ์šธ์Œ์†Œ๋ฆฌ ์ €์žฅ
answer = 0      # ์˜ค๋ฆฌ์˜ ์ˆ˜
done = 0        # ๋๋‚œ ์šธ์Œ ์†Œ๋ฆฌ ์ˆ˜

duck = "quack"  # ์šธ์Œ์†Œ๋ฆฌ ํ•œ ์‚ฌ์ดํด
find = 0        # ์‚ฌ์ดํด์—์„œ ์ฐพ๋Š” ๋ฌธ์ž

for c in string:
    if c == 'q': find = 4   # ์ด์ „ ๋ฌธ์ž๋ฅผ ์ฐพ์•„์•ผํ•จ
    elif c == 'u': find = 0
    elif c == 'a': find = 1
    elif c == 'c': find = 2
    elif c == 'k': find = 3
    
    isin = False            # ์—ฐ๊ฒฐ๋˜๋Š” ์šธ์Œ ์ฐพ๊ธฐ
    for i in range(len(strs)):          # ์šธ์Œ ๊ฒ€์‚ฌ
        if strs[i][-1] == duck[find]:   
            strs[i] += duck[(find+1)%5]
            isin = True     # ์—ฐ๊ฒฐ๋˜๋Š” ์šธ์Œ ์ฐพ์Œ
            if find == 3:
                done += 1
            elif find == 4:
                done -= 1
            break
    if not isin:            # ์—ฐ๊ฒฐ๋˜๋Š” ์šธ์Œ ๋ชป์ฐพ์Œ
        strs.append("q")
        answer += 1

if answer == done:
    print(str(answer))
else:
    print(str(-1))

๐Ÿ”ธ ์ฝ”๋“œ ํ•ด์„ ๐Ÿ”ธ

  • ์šธ์Œ์†Œ๋ฆฌ๋ฅผ ๊ตฌ๋ถ„ํ•˜๋Š” ๋ฐฉ๋ฒ•์œผ๋กœ string ๋ฆฌ์ŠคํŠธ๋ฅผ ์„ ํƒํ–ˆ๋‹ค.
  • ๋ถˆ๊ฐ€๋Šฅํ•œ ์‚ฌ์ดํด์„ ์˜ค๋ฆฌ์˜ ์ˆ˜์™€ ์™„์„ฑ๋œ ์šธ์Œ์˜ ์ˆ˜๋ฅผ ๋น„๊ตํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์‚ฌ์šฉํ–ˆ๋‹ค.
  • ์ž…๋ ฅ์—์„œ ๋งˆ์ง€๋ง‰ ์ค„๋ฐ”๊ฟˆ \n์„ ์ œ๊ฑฐํ•ด์ค˜์•ผ ํ–ˆ๋‹ค.

๐Ÿ”ธ end ๐Ÿ”ธ

  • ์šธ์Œ์†Œ๋ฆฌ ๊ตฌ๋ถ„ ๋ฐฉ๋ฒ•์„ ๋งŽ์ด ๊ณ ๋ฏผํ•˜๋‹ค ๋ฆฌ์ŠคํŠธ๋ฅผ ์„ ํƒํ–ˆ๋‹ค.
  • ์‹ค๋ฒ„ 4์น˜๊ณ ๋Š” ์–ด๋ ต๊ฒŒ ํ’€์€ ๊ฒƒ ๊ฐ™์•„ ์ฝ”๋“œ ๋ฆฌ๋ทฐ๋ฅผ ๋ฐ›์„ ์ƒ๊ฐ์ด๋‹ค.

728x90

'CodingTest > Python' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

BOJ_2775 : ๋ถ€๋…€ํšŒ์žฅ์ด ๋ ํ…Œ์•ผ  (0) 2022.08.07
BOJ_2751 : ์ˆ˜ ์ •๋ ฌํ•˜๊ธฐ 2  (0) 2022.08.02
BOJ_10828 : ์Šคํƒ  (0) 2022.07.30
BOJ_20207 : ๋‹ฌ๋ ฅ  (0) 2022.07.21
BOJ_17413 : ๋‹จ์–ด ๋’ค์ง‘๊ธฐ 2  (0) 2022.07.21