https://edder773.tistory.com/86
[백준 17413] 단어 뒤집기 2 (python)
https://www.acmicpc.net/problem/17413 17413번: 단어 뒤집기 2 문자열 S가 주어졌을 때, 이 문자열에서 단어만 뒤집으려고 한다. 먼저, 문자열 S는 아래와과 같은 규칙을 지킨다. 알파벳 소문자('a'-'z'), 숫자('
edder773.tistory.com
# stack 사용\
# <>안에 있는 글자는 바꾸지 않음
import sys
# 입력 문자열 읽기
s = sys.stdin.readline().rstrip() + ' '
stack = []
result = ''
inside_tag = False # 태그(<>) 안에 있는지 여부
for char in s:
if char == '<':
# 태그 시작: 스택에 쌓인 단어를 뒤집어 결과에 추가
while stack:
result += stack.pop()
inside_tag = True
result += char
elif char == '>':
# 태그 끝: 태그를 결과에 추가
inside_tag = False
result += char
elif inside_tag:
# 태그 안: 그대로 결과에 추가
result += char
elif char == ' ':
# 단어 끝: 스택에 쌓인 단어를 뒤집어 결과에 추가
while stack:
result += stack.pop()
result += char
else:
# 단어 구성 중: 스택에 추가, <>와 공백이 아닐 때 stack에 단어 쌓임
stack.append(char)
print(result)
stack 구현 더 공부하기
'Algorithm > Stack&Queue' 카테고리의 다른 글
10799번/ 쇠막대기 / stack (0) | 2025.04.08 |
---|