반응형

   문제

   https://www.acmicpc.net/problem/5430



R에따라 어디서 팝하고 출력해줄건지 결정해 주면 될거같다.

이번역시 collections의 deque를 사용했지만 더빠르게 구현하고 싶다면

직접 배열을 조정하는거보단 top과 tail을 사용하여 위치를 잡아주고 출력할때도 뒤집

을 필요없이 반대방향으로 출력하면 빨라질것이다.



1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import sys
from collections import deque
 
 
T=int(input())
 
 
while T>0:
    T-=1
    check=0#0은 왼쪽부터 1은 오른쪽부터
    command=list(sys.stdin.readline())
    n=int(sys.stdin.readline())
    arr = sys.stdin.readline().strip()
    arr = arr.replace('[''')
    arr = arr.replace(']''')
    if len(arr)!=0:
        num_list = list(map(int,arr.split(',')))
    else:
        num_list=[]
    num_deque=deque(num_list)
    for i in range(len(command)):
        if command[i]=='R':#방향 바꾸기
            if check==0:
                check=1
            elif check==1:
                check=0
 
        elif command[i]=='D':#제거
            if len(num_deque)==0:
                check=2#에러
                break
            if check==0:#정방향
                num_deque.popleft()
            elif check==1:#역방향
                num_deque.pop()
 
    if check==1:
        num_deque.reverse()
    if check==2:
        print('error')
    else:
        print('[',end='')
        for i in range(len(num_deque)):
            print(num_deque[i],end='')
            if i!=len(num_deque)-1:
                print(',',end='')
        print(']')
cs




반응형

'알고리즘(python) > 자료구조' 카테고리의 다른 글

[Python]우선순위큐 백준 1927  (0) 2020.02.02
[Python]우선순위큐 백준 11279  (0) 2020.02.01
[Python]Deque 백준 1021  (0) 2020.01.17
[Python]Deque 백준 10866  (0) 2020.01.17
[Python]Queue 백준 1966  (0) 2020.01.17
  • 네이버 블러그 공유하기
  • 네이버 밴드에 공유하기
  • 페이스북 공유하기
  • 카카오스토리 공유하기