[백준] 에디터 1406번 파이썬 Python 자료구조

Jeony·2021년 9월 30일
0

백준

목록 보기
5/25
post-thumbnail

📌문제 접근

밑의 코드는 이 문제를 보고 처음 문제를 푼 코드다.
cursor라는 변수를 만들어서 커서의 위치를 나타내도록 했다.
그걸 사용하기 위해서 pop(), insert()를 사용했다.
시간초과가 계속 나와서 구글링 해보니 시간복잡도의 문제였다.

시간 복잡도: O(1)보다 O(N)이 더 복잡함.
append(): O(1)
pop(): O(1)
insert(): O(N)

이러한 문제로 append()와 pop()을 사용하기 위해서 stack을 왼쪽 stack, 오른쪽 stack 두개 만들었다.

import sys

text = list(input())
n = int(input())
cursor = len(text)

for i in range(n):
    command = sys.stdin.readline().split()

    if command[0] == "L" and cursor > 0:
        cursor -= 1
    elif command[0] == "D" and cursor < len(text):
        cursor += 1
    elif command[0] == "B" and cursor > 0:
        text.pop(cursor-1)
        cursor -= 1
    elif command[0] == "P":
        text.insert(cursor, command[1])
        cursor += 1

for i in range(len(text)):
    print(text[i], end="")

📌내가 작성한 코드 (시간복잡도 반영)

import sys

stack_l = list(input())
stack_r = []
n = int(input())

for i in range(n):
    command = sys.stdin.readline().split()

    if command[0] == "L" and stack_l:
        stack_r.append(stack_l.pop())
    elif command[0] == "D" and stack_r:
        stack_l.append(stack_r.pop())
    elif command[0] == "B" and stack_l:
        stack_l.pop()
    elif command[0] == "P":
        stack_l.append(command[1])

print("".join(stack_l + list(reversed(stack_r))))

📌풀이

  1. sys.stdin.readline()을 쓰기 위해서 import sys.
  2. 스택 왼쪽을 표현한 stack_l 변수에 문자 입력 받음
    ex) abc
  3. 스택 오른쪽을 표현한 stack_r 변수에 스택을 쌓기 위해 빈 리스트 초기화.
  4. n변수에 edit 입력 횟수 입력 받음.
import sys
stack_l = list(input())
stack_r = []
n = int(input())
  1. for문을 edit 입력 횟수에 따라 반복시키고 그 안에서 edit 입력 받음.
for i in range(n):
    command = sys.stdin.readline().split()
  1. for문 안에서 입력받은 command[0]가 L과 같고 stack_l이 True(값이 있으면)면,
    stack_l의 마지막 값을 pop해서 stack_r 마지막자리에 append해서 넣는다.
if command[0] == "L" and stack_l:
        stack_r.append(stack_l.pop())
  1. for문 안에서 입력받은 command[0]가 D와 같고 stack_r이 True(값이 있으면)면,
    stack_r의 마지막 값을 pop해서 stack_l 마지막자리에 append해서 넣는다.
elif command[0] == "D" and stack_r:
        stack_l.append(stack_r.pop())
  1. for문 안에서 입력받은 command[0]가 B와 같고 stack_l이 True(값이 있으면)면,
    stack_l의 마지막 값을 pop한다.
elif command[0] == "B" and stack_l:
        stack_l.pop()
  1. for문 안에서 입력받은 command[0]가 P와 같으면,
    stack_l의 마지막 값에 입력받은 command[1]을 append 한다.
elif command[0] == "P":
        stack_l.append(command[1])
  1. stack_l와 stack_r을 합쳐야 한다.
    stack_r를 reversed()로 반대로 돌린다. 이렇게만 하면 revered의 객체가 표현된다. 그래서 list로 형변환을 해준다.
    "".join()은 fomat형식을 맞춰준다.
    출력 예제처럼 나타낼 수 있게된다.
    ex) yxabc
print("".join(stack_l + list(reversed(stack_r))))

입력값:
abc
9
L
L
L
L
L
P x
L
B
P y

시작 값
stack_l = [a, b, c]
stack_r = []

L 입력
stack_l = [a, b]
stack_r = [c]

L 입력
stack_l = [a]
stack_r = [c, b]

L 입력
stack_l = []
stack_r = [c, b, a]

L 입력
stack_l의 값이 없어서 조건 불충족

L 입력
stack_l의 값이 없어서 조건 불충족

P x 입력
stack_l = [x]
stack_r = [c, b, a]

L 입력
stack_l = []
stack_r = [c, b, a, x]

B 입력
stack_l의 값이 없어서 조건 불충족

P y 입력
stack_l = [y]
stack_r = [c, b, a, x]

출력 (stack_l + list(reversed(stack_r)))
[y, x, a, b, c]

profile
알고리즘으로 문제를 해결하자 (ʘ言ʘ╬)

0개의 댓글