coding skill

[백준][문제 풀기] 알고리즘 기초 1/2 - 17413 단어 뒤집기 2

pikabite 2022. 3. 11. 18:44

단순히 단어 파싱을 잘 하면 된다. 별다른 함정같은 것은 없어보이는 문제

이번 문제도 화이팅!



import enum
import sys

if __name__ == "__main__" :

    # 데이터를 잘 받자!
    str_ = sys.stdin.readline().strip()

    word = []
    tag = []

    output = ""

    # 나는 성선설을 좋아하지만 어쩔 수 없다
    # 현재 process중인 character가 tag인지 아닌지 체크
    in_tag = False
    word_start = 0

    for i, v in enumerate(str_) :
        if v == "<" : 
            in_tag = True
            if len(word) > 0 :
                word = word[::-1]
                output += "".join(word)
                word = []
        elif v == ">" : 
            in_tag = False
            output += ("<" + "".join(tag) + ">")
            tag = []
        elif in_tag : 
            tag.append(v)
        elif not in_tag and not v == " " : 
            word.append(v)
        elif v == " " and not in_tag : 
            word = word[::-1]
            output += "".join(word) + " "
            word = []

    if len(word) > 0 :
        word = word[::-1]
        output += "".join(word)
        word = []

    print(output)
  • string을 조심히 잘 처리하면 되는 문제였음 굳