๋ฌธ์ ๋งํฌ
https://www.acmicpc.net/problem/5430
์ฝ๋ ์ ์ถ ๊ธฐ๋ก (๋ฉ๋ชจ๋ฆฌ ๋ฐ ์๊ฐ)
์ ์ถ ๋ ์ง
@5/20/2021
๋ฉ๋ชจ๋ฆฌ
KB
์๊ฐ
ms
๋ฌธ์ ํ์ด
start, end ๋ณ์๋ก ๋ฐฐ์ด์ ์ฒ์๊ณผ ๋์ ์ ์ฅ!
R ๋ช
๋ น์ด์ผ๋ โ start ์ end๋ฅผ ์๋ก ๋ฐ๊ฟ์ค
D ๋ช
๋ น์ด์ผ๋ โ start < end๋ผ๋ฉด start += 1
start > end ๋ผ๋ฉด start -= 1
๊ฒฐ๊ณผ ์ถ๋ ฅ โ start < end๋ผ๋ฉด arr[start : end + 1] ์ถ๋ ฅ
start > end ๋ผ๋ฉด arr[end : start + 1].reverse() ์ถ๋ ฅ
Code
์๊ฐ์ด๊ณผ..
T = int(input())
for _ in range(T):
cmd = str(input())
arr_len = int(input())
arr_ = str(input())
arr_ = arr_[1:-1].split(',')
arr = []
flag = 0
for i in range(arr_len):
arr.append(int(arr_[i]))
location = 0
for i in range(len(cmd)):
if cmd[i] == 'R':
location = -1 if (location == 0) else (0)
elif cmd[i] == 'D':
if len(arr) == 0:
flag = 1
break
arr = arr[1:] if (location == 0) else (arr[0:-1])
if location == -1:
arr.reverse()
if flag == 1:
print("error")
else:
print(arr)
arr.clear()
arr_.clear()
Python
๋ณต์ฌ
์๊ฐ์ด๊ณผ..
T = int(input())
for _ in range(T):
cmd = str(input())
arr_len = int(input())
arr_ = str(input())
arr_ = arr_[1:-1].split(',')
arr = []
flag = 0
for i in range(arr_len):
arr.append(int(arr_[i]))
start = 0
end = arr_len -1
for i in range(len(cmd)):
if cmd[i] == 'R':
start, end = end, start
elif cmd[i] == 'D':
if len(arr) == 0:
flag = 1
break
if start < end:
start += 1
elif start > end:
start -= 1
else:
flag = 1
break
if flag == 1:
print("error")
else:
if start > end:
arr_tmp = arr[end : start + 1]
arr_tmp.reverse()
print(arr_tmp)
elif start < end:
print(arr[start : end + 1])
else:
print("[]")
arr.clear()
arr_.clear()
C
๋ณต์ฌ