본문 바로가기

CodingTest/Baekjun Online Judge

[ BOJ / 파이썬 ] 5430 AC

/  제출  1  /

import sys
input = sys.stdin.readline

tc = int(input())

for i in range(tc):
  oper = input().strip()
  n = int(input())
  nums = input()
  if n == 0:
    print('error')
    break

  nums = nums[1:-2]
  if len(nums) > 2:
    nums = list(map(int, nums.split(',')))
  # oper 에서 RR 처리
  while 'RR' in oper:
    oper = oper.replace('RR', '') # ㅇ이고.. replace는 리턴을 주는 녀석이었다.

  for o in oper:
    if len(nums) == 0:
      nums = 'error'
      break
    if o == 'R':
      nums.reverse()
    if o == 'D':
      nums = nums[1:]

  print(nums)

: 40분 걸려 풀고 틀렸다.

: 흐으.. 2번인가 33%에서 막힌다. 뭘까..

: 잘 작성한 코드가 아닌 걸 알아서 실망은 없다.

: replace 부분에서 헤맸다. replace가 oper자체를 변경해주는 줄 알았는데 변경결과를 리턴하는 걸 몰랐다. 기억하자.

 

포기..

골드 5 밖에 안 되는데 너무 한 거 아니냐..

 

https://chancoding.tistory.com/41

 

[Python] 백준 5430번 AC - 효율적인 알고리즘 3720ms에서 132ms

AC 시간 제한 메모리 제한 제출 정답 맞은 사람 정답 비율 1 초 256 MB 18083 3998 2545 19.681% 문제 선영이는 주말에 할 일이 없어서 새로운 언어 AC를 만들었다. AC는 정수 배열에 연산을 하기 위해 만든

chancoding.tistory.com

 

 

/ 참조 풀이  /

import sys
input = sys.stdin.readline
from collections import deque

for test_case in range(int(sys.stdin.readline())):
  error = False
  reverse = False

  command_list = sys.stdin.readline().strip()
  num_cnt = int(sys.stdin.readline())
  num_list = deque(sys.stdin.readline().strip()[1:-1].split(","))
  
  if num_cnt == 0:
    num_list = deque()

  for command in command_list:
    if command == "R":
      if reverse:
        reverse = False
      else:
        reverse = True

    else: 
      if num_list:
        if reverse:
          num_list.pop()
        else:
          num_list.popleft()
      else:
        error = True
        break
      
  if error:
    print("error")
  elif reverse:
    num_list.reverse()
    print("["+",".join(num_list)+"]")
  else:
    print("["+",".join(num_list)+"]")