무제
[백준] 17413번 단어뒤집기 2 본문
import sys
input = sys.stdin.readline
from collections import deque
sentence = input()
tmp = deque()
answer = ""
is_tag= False
for word in sentence:
if word == "<":
is_tag = True
answer += "".join(tmp)
tmp.clear()
answer += word
elif word == ">":
is_tag = False
answer += word
elif is_tag or word == " ":
answer += "".join(tmp)
tmp.clear()
answer += word
else:
tmp.appendleft(word)
tmp.popleft()
answer += "".join(tmp)
print(answer)
누더기 코드
다른사람의 풀이.. 이 방법이 더 나은것같다
s = input()
temp = []
answer = []
for i in range(len(s)):
# 닫힌 괄호가 나오면
if s[i] == '>':
temp.append('>')
# 그대로 최종 리스트에 추가
answer.append(''.join(temp))
# 임시 리스트 초기화
temp = []
# 열린 괄호가 나왔는데 이전에 입력된 것들이 있다면
elif s[i] == '<' and temp:
# 임시 리스트를 뒤집어서 추가
temp.reverse()
answer.append(''.join(temp))
# 임시 리스트 초기화
temp = [s[i]]
# 공백 문자인데 괄호 밖이라면
elif s[i] == ' ' and '<' not in temp:
# 임시 리스트 뒤집어서 추가
temp.reverse()
answer.append(''.join(temp))
answer.append(' ')
# 임시 변수 초기화
temp = []
else:
temp.append(s[i])
# 아직 임시 변수에 문자열이 남아있다면 뒤집어서 추가
if temp:
temp.reverse()
answer.append(''.join(temp))
# 문자열로 반환
print(''.join(answer))'Study > Coding Test 오답노트' 카테고리의 다른 글
| [백준] 17299번 오등큰수 (0) | 2024.11.22 |
|---|---|
| [백준] 10799번 쇠막대기 (2) | 2024.11.13 |
| [백준] 1158번 요세푸스 문제 (0) | 2024.11.10 |
| [백준] 1406번 에디터 (0) | 2024.11.09 |
| [백준] 자료구조 - 9012(괄호) / 1874(스택 수열) (0) | 2024.10.19 |
Comments