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 | 29 | 30 | 31 |
Tags
- 자바
- 관계형 데이터베이스
- 출처 모던 자바스크립트 Deep Dive
- java
- msa
- JPA
- springboot
- spring security
- UUID
- ORM
- 자바스크립트
- 유효성검사
- 자바 ORM 표준 JPA 프로그래밍
- 공백검사
- 중복되지 않는 값 만들기
- auto-offset-reset
- Kafka
- 스프링 시큐리티
- Thymeleaf
- 게시판 작성자를 아이디로
- 모던 자바스크립트 Deep Dive
Archives
- Today
- Total
인지용
[백준] 4889 안정적인 문자열 [python, 실버1, 스택] 본문
https://www.acmicpc.net/problem/4889
import sys
def input():
return sys.stdin.readline().strip()
idx = 1
while True:
text = input()
if text.startswith("-"):
break
arr = list(text)
stack = []
for i in arr:
if i == '{':
stack.append(i)
continue
else:
if stack and stack[-1] == '{':
stack.pop()
else:
stack.append(i)
num = 0
for i in range(0, len(stack), 2):
if stack[i] == stack[i+1]:
num += 1
else:
num += 2
print(f"{idx}. {num}")
idx += 1
먼저 원본 배열에서 안정적인 문자열을 다 제거하고 남은 값들을 변환시켜주면 된다.
입력은 무조건 짝수라고 했으니 반복문을 2씩 증가시키고 값이
{ { 혹은 } } 와 같이 같은 값이라면 하나만 바꾸면 되니까 + 1을
} { 라면 두개를 바꿔야 하니까 +2를 해주면 된다.
'알고리즘' 카테고리의 다른 글
| [백준] 27497 알파벳 블록 [python, 실버1, 스택] (0) | 2026.01.21 |
|---|---|
| [백준] 11899 괄호 끼워넣기 [python, 실버3, 스택] (0) | 2026.01.19 |
| [백준] 2841 외계인의 기타 연주 [python, 실버1, 스택] (0) | 2026.01.16 |
| [백준] 1935 후위 표기식2 [python, 실버3, 스택] (0) | 2026.01.15 |
| [백준] 12789 도키도키 간식드리미 [python, 실버2, 스택] (0) | 2026.01.14 |
