210330 개발일지(113일차) - 알고리즘 문제풀이(feat. stack) 백준 9935번 문자열 폭발

고재개발·2021년 4월 3일
0

Algorithm

목록 보기
26/26

문제는 아래와 같다.

처음에는 파이썬에 있는 "string" in [list]를 활용해서 풀었으나 시간초과로 인해 스택을 사용했다.
문자열 관련 문제 중 스택을 활용해서 시간을 잘 줄일 수 있는 경우가 많으니.. 처음부터 잘 생각해서 풀자.

첫 번째 풀이(시간 초과)

import sys
strings = sys.stdin.readline().rstrip()
bomb=list(sys.stdin.readline().rstrip())

while bomb in strings:
    strings=strings.replace(bomb,"")
if strings =="":
    print("FRULA")
else :
    print(strings)

스택 활용한 두 번째 풀이

import sys
strings = sys.stdin.readline().rstrip()
bomb=list(sys.stdin.readline().rstrip())

answer=[]

for string in strings:
    answer.append(string)
    if string == bomb[-1]:
        if answer[-1:-len(bomb)-1:-1] == bomb[-1:-len(bomb)-1:-1]:
            for i in range(len(bomb)):
                answer.pop()

if len(answer):
    print(''.join(answer))
else :
    print("FRULA")
profile
고재개발

0개의 댓글