본문 바로가기
코딩테스트

백준 단어 뒤집기2

by GOMJ 2026. 2. 1.

입력받은 단어를 뒤집어서 보여주면 된다.

 

하지만 태그일때는 뒤집지 않고 그대로 보여주라고 한다.

 

즉 태그일때와 아닐때만 구별해서 입력해주면 된다.

 

태그면 입력받은대로 입력 아니면 역순

 

#include <iostream>
#include <queue>
#include <algorithm>
#include <cstring>
#include <unordered_map>
#include <math.h>
#include <string>
#include <stack>

#define INF 999999999

using namespace std;



int dx[8] = { 1, 0, -1, 0,-1,-1,1,1 };
int dy[8] = { 0, 1, 0, -1,-1,1,-1,1 };
int cnt;
int n, m, t, w;
int dp[300001];
int days[16];
int maxs[16];
//vector<pair<int, int>> node[300001];
vector<int> node[300001];
priority_queue<pair<int, int>, vector<pair<int, int>>, greater<>> pq;


int main() {
	string s;
	getline(cin,s);
	stack<char> st;
	string temp;
	bool tag = false;

	for (int i = 0, len = s.length(); i < len; i++) {
		if (s[i] == '<') {
			tag = true;
			while (!st.empty()) {
				temp += st.top();
				st.pop();
			}
			temp.push_back(s[i]);
		}
		else if (s[i] == '>') {
			temp.push_back(s[i]);
			tag = false;
		}
		else if (tag) {
			temp.push_back(s[i]);
		}
		else {
			if (s[i] == ' ') {
				while (!st.empty()) {
					temp += st.top();
					st.pop();
				}
				temp.push_back(' ');
			}
			else {
				st.push(s[i]);
			}
		}

	}

	while (!st.empty()) {
		temp += st.top();
		st.pop();
	}
	
	cout << temp << endl;

	return 0;
}

'코딩테스트' 카테고리의 다른 글

백준 회의실배정  (0) 2026.01.25
백준 경쟁적 전염  (0) 2026.01.07
백준 촌수계산  (0) 2026.01.01
백준 노드사이의 거리  (0) 2025.12.27
백준 보물섬  (0) 2025.12.21