본문 바로가기
Algorithm

백준 17413번 : 단어 뒤집기 2

by jun_code 2022. 1. 25.

<문제>

링크 : https://www.acmicpc.net/problem/17413

 

17413번: 단어 뒤집기 2

문자열 S가 주어졌을 때, 이 문자열에서 단어만 뒤집으려고 한다. 먼저, 문자열 S는 아래와과 같은 규칙을 지킨다. 알파벳 소문자('a'-'z'), 숫자('0'-'9'), 공백(' '), 특수 문자('<', '>')로만 이루어져

www.acmicpc.net


<코드>

import sys
input = sys.stdin.readline

S = input().rstrip()
inner = False
word = ''
result = ''

for i in S:
    if inner == False: # < > 외부
        if i == '<':
            inner = True
            result += word
            result += i
            word = ''
        elif i == ' ':
            word += i
            result += word
            word = ''
        # elif i == '>': 는 나올수 없는 경우
        else:
            word = i + word 
    else: # < > 내부
        if i == '>':
            result += i
            inner = False
        else:
            result += i
if len(word) >0:
    result += word
print(result)
import sys
input = sys.stdin.readline

s = input().rstrip()
flag = False
word = ""
answer = ""

for i in s:
    if flag == False:
        if i == "<":
            flag = True
            word += i
        elif i == " ":
            word += i
            answer += word
            word = ""
        else:
            word = i + word

    else:
        word += i
        if i == ">":
            flag = False
            answer += word
            word = ""

print(answer + word)

<풀이>

  • <> 외부인 경우와 내부인 경우를 나누기 위해 T/F를 사용한다.
  • <> 외부인 경우 문자가 올 수 있는 경우는 '<', ' '(공백), 알파벳이므로 세가지 경우로 나누어 생각한다.
  • <> 내부인 경우 '>'인 경우와 그 외로 분류할 수 있다.
  • 마지막이 '>'로 끝나지 않는 경우 마지막 단어도 추가해야 하므로 word에 남은 문자가 있는지 확인한다.

참고 : https://github.com/Junuux/Algorithm/tree/main/Silver/17413

 

GitHub - Junuux/Algorithm

Contribute to Junuux/Algorithm development by creating an account on GitHub.

github.com

'Algorithm' 카테고리의 다른 글

백준 20291번 : 파일 정리  (0) 2022.01.25
백준 19948번 : 음유시인 영재  (0) 2022.01.25
백준 1972번 : 놀라운 문자열  (0) 2022.01.25
백준 1764번 : 듣보잡  (0) 2022.01.25
백준 11725번 : 트리의 부모 찾기  (0) 2022.01.16