Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 99클럽
- jwt
- KPT회고
- 인프콘 2024
- 취업리부트코스
- 프로그래머스 이중우선순위큐
- jwttoken
- 전략패턴 #StrategyPattern #디자인패턴
- 단기개발자코스
- 코딩테스트 준비
- @FeignClient
- infcon 2024
- DesignPattern
- 빈 조회 2개 이상
- 1주일회고
- Python
- 구글 OAuth login
- 항해99
- 디자인패턴
- 빈 충돌
- spring batch 5.0
- 개발자부트캠프추천
- 프로그래머스
- 개발자 취업
- JavaScript
- 디자인 패턴
- TiL
- Spring multimodule
- 파이썬
- 커스텀 헤더
Archives
- Today
- Total
m1ndy5's coding blog
[자료구조] 백준 1406번 에디터 with Python 본문
https://www.acmicpc.net/problem/1406
왜 이문제가 실버 2일까~~ 했더니만 역시나 시간초과!!ㅎㅎ
import sys
s = sys.stdin.readline().rstrip()
c = len(s)
n = int(sys.stdin.readline().rstrip())
for i in range(n):
com = sys.stdin.readline().rstrip()
if ' ' in com:
com, letter = com.split()
if com == 'L':
if c != 0:
c -= 1
elif com == 'D':
if c != len(s):
c += 1
elif com == 'B':
if c != 0:
c -= 1
s = s[:c] + s[c+1:]
elif com == 'P':
s = s[:c] + letter + s[c:]
c += 1
print(s)
문자열 슬라이싱으로 풀어보기도 하고 리스트 insert, del을 사용해서도 풀어봤지만 시간초과가 났다.
insert와 del은 모두 O(N)의 시간복잡도를 가지고 있었고 상대적으로 시간복잡도가 작은 append와 pop을 사용해야했다.(append와 pop은 O(1))
append와 pop를 사용하기 위해선 스택을 두개로 나누어서 진행하면 된다.
import sys
stack1 = list(sys.stdin.readline().rstrip())
stack2 = []
n = int(sys.stdin.readline().rstrip())
for i in range(n):
com = sys.stdin.readline().rstrip()
if ' ' in com:
com, letter = com.split()
if com == 'L':
if len(stack1) != 0:
stack2.append(stack1.pop())
elif com == 'D':
if len(stack2) != 0:
stack1.append(stack2.pop())
elif com == 'B':
if len(stack1) != 0:
stack1.pop()
elif com == 'P':
stack1.append(letter)
print(''.join(stack1)+''.join(reversed(stack2)))
'알고리즘 with python > 자료구조' 카테고리의 다른 글
[자료구조] 백준 10799번 쇠막대기 with Python (0) | 2023.03.22 |
---|---|
[자료구조]백준 1874번 스택 수열 with Python (0) | 2023.03.22 |
[자료구조] 백준 1935번 후위 표기식2 with python (0) | 2023.03.22 |
[자료구조] 백준 10773번 제로 with python (0) | 2023.03.22 |
[자료구조] 백준 9012번 괄호 with Python (0) | 2023.03.20 |