동캄의 코딩도장

백준 5430 [AC] 파이썬 본문

코테/BOJ

백준 5430 [AC] 파이썬

동 캄 2022. 4. 12. 20:12
# 백준 5430 AC
import sys
from collections import deque
input = sys.stdin.readline

for _ in range(int(input().rstrip())):
    p = list(map(str, input()))
    n = int(input().rstrip())
    lst = input().rstrip()[1:-1]
    if lst:
        lst = list(map(str, lst.split(',')))
    if p.count('D') > n:
        print('error')
    elif p.count('D') == n:
        print([])
    else:
        i = 0
        front = 0
        back = 0
        check = 0
        while i < len(p)-1:
            if p[i] == 'R':
                j = 1
                while i+j < len(p):
                    if p[i+j] == 'R':
                        j += 1
                    else:
                        break
                i = i+j
                if j % 2 == 1:
                    if check == 0:
                        check = 1
                    else:
                        check = 0
            else:
                j = 1
                while i+j < len(p)-1:
                    if p[i+j] == 'D':
                        j += 1
                    else:
                        break
                i = i+j
                if check == 0:
                    front += j
                else:
                    back += j
        lst = lst[front:len(lst)-back]
        if check == 1:
            lst.reverse()
        print('['+','.join(lst)+']')

먼저 lst에 앞뒤 문자 '[' 와 ']'를 떼어준 뒤, ,를 기준으로 string으로 분리하여 리스트에 저장한다.

 

만약 'D'의 숫자 > n 이면 --> error 를 리턴

만약 'D'의 숫자 == n 이면 --> [] 을 리턴

만약 'D'의 숫자 < n 이면, 

R이 몇번 반복되는지

혹은

D가 몇번 반복되는지 를 체크하고 갯수를 파악한다.

'코테 > BOJ' 카테고리의 다른 글

백준 14500 [테트로미노] 파이썬  (0) 2022.04.14
백준 10026 [적록색약] 파이썬  (0) 2022.04.14
백준 1107 [리모컨] 파이썬  (0) 2022.04.09
백준 1987 [알파벳] 파이썬  (0) 2022.03.24
백준 7576 [토마토] 파이썬  (0) 2022.03.21