[ 문제 ]
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;
}
'PS' 카테고리의 다른 글
[ 백준 / C++ ] 10804 : 카드 역배치 (0) | 2024.10.20 |
---|---|
[ 백준 / C++ ] 4470 : 줄번호 (0) | 2024.10.18 |
[ 백준 / C++ ] 2075 : N번째 큰 수 (3) | 2024.10.14 |
[ 백준 / C++ ] 1644 : 소수의 연속합 (0) | 2024.10.10 |
[ 백준 / C++ ] 11048 : 이동하기 (0) | 2024.10.08 |