상세 컨텐츠

본문 제목

백준 5430번 AC 파이썬

프로그래밍/백준풀이

by 아싸호랑나비 2022. 9. 20. 10:40

본문

요즘 포스팅이 안올라오는 이유는 현재 기업프로젝트를 진행하기 때문

아무래도 공개적으로 프로젝트진행사항이나 코드, 데이터를 소개하는것은 안될것같기때문에

포스팅이 올라오지않고있다

순조롭게 진행되고있으며 느낀점, 배운점? 등을 기업프로젝트가 끝난후 포스팅으로 정리할 계획이다 아직 1달정도 진행시간이 남았다

 

어제는 발표시간이라 중간에 백준을 하나 풀어봤는데 이문제의 정답률이 19%정도로 매우 낮은편이고

실제로도 어느정도 난이도가 있는것같아서 소개해볼만 한것같다

 

첫번째 제출

from collections import deque
import sys
input=sys.stdin.readline
def R(a):
    b=deque([])
    while len(a)>0:
        b.append(a.pop())
    return b

for _ in range(int(input().strip())):
    order=input().strip()
    num=int(input().strip())
    la=input().strip()
    a=deque(la[1:-1].split(','))
    if la=='[]':
        a=deque([])
    
    try:
        for i in order:
            if i=='R':
                a=R(a)
            else:
                a.popleft()
        print(list(map(int,a)))
    except:
        print('error')

덱을 사용했고 R을 실행시키는 함수를 직접 구현하였다

시간초과가 났다

 

from collections import deque
import sys
input=sys.stdin.readline
# def R(a):
#     b=deque([])
#     while len(a)>0:
#         b.append(a.pop())
#     return b

for _ in range(int(input().strip())):
    order=input().strip()
    num=int(input().strip())
    la=input().strip()
    a=deque(la[1:-1].split(','))
    if la=='[]':
        a=deque([])
    
    try:
        for i in order:
            if i=='R':
                a.reverse()
            else:
                a.popleft()
        print(list(map(int,a)))
    except:
        print('error')

deque의 reverse 함수를 이용해서 구했다 

시간초과가 떳다

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

for _ in range(int(input().strip())):
    order=input().strip()
    num=int(input().strip())
    la=input().strip()
    a=deque(la[1:-1].split(','))
    if la=='[]':
        a=deque([])
    # 만약 D가 최초 문자열의 길이보다 많으면 자동으로 error 출력
    # 만약 R이 연속적으로 나올때가 짝수면 제거
    D_count=0
    new_order=deque([])
    for i in range(len(order)):
        new_order.append(order[i])
        try:
            if new_order[-1]=='R' and new_order[-2]=='R':
                new_order.pop()
                new_order.pop()
            elif new_order[-1]=='D':
                D_count+=1
        except:
            pass
    
    if len(a)<D_count:
        print('error')
    else:
        for i in new_order:
            if i=='R':
                a.reverse()
            else:
                a.popleft()
        print(list(map(int,a)))

new_order라는 새로운 명령

명령어에 포함되어있는 E가 문자열의 길이보다 길면 error출력

R이 2연속으로 들어가있을경우 제거했다 뒤집은걸 또 뒤집으면 똑같기때문에

시간초과가 떳다

 

새로운 방법론이 있거나

아니면 내가 모르는 계산이 빠른 새로운 모듈이 있을것같다

 

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

for _ in range(int(input().strip())):
    order=input().strip()
    num=int(input().strip())
    la=input().strip()
    a=deque(la[1:-1].split(','))
    if la=='[]':
        a=deque([])
  
    D_count=0
    # 에러 거르기
    for i in order:
        if i=='D':
            D_count+=1
    if D_count>len(a):
        print('error')
        continue
    
    reversed=0
    for i in order:
        if i=='R':
            if reversed==1:
                reversed=0
            else:
                reversed=1
        
        else: # D일경우
            if reversed==1:
                a.pop() 
            else:
                a.popleft()
    

    
    if reversed==1:
        a.reverse()
        print('['+','.join(a)+']')
            
    else:
        # print(list(map(int,list(a))))
        print('['+','.join(a)+']')

생각을 조금해보다가 R함수를 계속 실행시킬 필요가 있나에 대해서 의문점이 생겼다

reverse된상태에서 D함수를 실행시키는것은 pop()과

다를게 없다는 생각이 들었다

위코드는 이 생각을 그대로 구현한 코드이고 마지막만

reverse인경우 한번 reverse해주면된다

맞았습니다가 떳다

'프로그래밍 > 백준풀이' 카테고리의 다른 글

백준 쿼드트리  (0) 2022.10.09
색종이 만들기 파이썬  (0) 2022.10.06
프린터 큐 백준 파이썬  (0) 2022.09.03
코테 만점을 맞다  (0) 2022.09.02
백준 큐 2 파이썬  (0) 2022.08.30

관련글 더보기