본문 바로가기

PS

[ 백준 / C++ ] 17413 : 단어 뒤집기 2

[ 문제 ]

 

17413번: 단어 뒤집기 2 (acmicpc.net)

 

[ 접근방법 ]

 

태그는 그대로 출력하고, 아닌 경우에는 스택을 활용하여 뒤집는다.

 

[ 소스코드 ]

 

#include <iostream>
#include <string>
#include <stack>

using namespace std;

string str;
bool isSpecial;

stack<char> st;

void swapWord()
{
    while (!st.empty())
    {
        cout << st.top();
        st.pop();
    }
}

int main()
{
    ios::sync_with_stdio(0);
    cin.tie(0);

    getline(cin, str);

    for (char c : str)
    {
        if (c == '<')
        {
            swapWord();
            isSpecial = true;
            cout << c;
        }
        else if (c == '>')
        {
            isSpecial = false;
            cout << c;
        }
        else if (isSpecial)
        {
            cout << c;
        }
        else if (c == ' ')
        {
            swapWord();
            cout << c;
        }
        else
        {
            st.push(c);
        }
    }

    swapWord();

    return 0;
}