ABOUT ME

-

Today
-
Yesterday
-
Total
-
  • [백준] 5430. AC (다시 풀어보기)
    코테 준비/구현 2023. 1. 27. 03:31

    첫번째 시도 (시간초과) - reverse를 매번 해줘서 타임아웃

    from collections import deque
    import sys
    t=int(sys.stdin.readline())
    for i in range(t):
      p=sys.stdin.readline()
      n=int(sys.stdin.readline())
      x=deque(sys.stdin.readline().rstrip()[1:-1].split(','))
      for i in range(n):
        x[i]=int(x[i])
      if n==0:
        print('error')
      else:
        for j in p:
          if j=='R':
            x.reverse()
          elif j=='D':
            if x:
              x.popleft()
            else:
              print("error")
        print(list(x))

    두번째 시도 (틀림) - 1, R ,0, [] 일때 틀림

    from collections import deque
    import sys
    t=int(sys.stdin.readline())
    for i in range(t):
      p=sys.stdin.readline()
      n=int(sys.stdin.readline())
      x=deque(sys.stdin.readline().rstrip()[1:-1].split(','))
      if n==0:
        print('error')
      else:
        count=0
        for j in p:
          if j=='R':
            count+=1
          elif j=='D':
            if x:
              if count%2==0:
                x.popleft()
              else:
                x.pop()
            else:
              print("error")
        if count%2==0:
          print("["+",".join(x)+"]")
        else:
          x.reverse()
          print("["+",".join(x)+"]")

    세번째 시도 (정답) - flag를 사용해서 error를 출력하면 리스트는 출력 안하도록 해야함

    from collections import deque
    import sys
    t=int(sys.stdin.readline())
    for i in range(t):
      p=sys.stdin.readline().rstrip()
      n=int(sys.stdin.readline())
      x=deque(sys.stdin.readline().rstrip()[1:-1].split(','))
      count=0
      flag=0
      if n==0:
        x=[]
      for j in p:
        if j=='R':
          count+=1
        elif j=='D':
          if x:
            if count%2==0:
              x.popleft()
            else:
              x.pop()
          else:
            print("error")
            flag=-1
            break
      if flag==0:
        if count%2==0:
          print("["+",".join(x)+"]")
        else:
          x.reverse()
          print("["+",".join(x)+"]")
Designed by Tistory.