17413번: 단어 뒤집기 2

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

www.acmicpc.net

#include <iostream>
#include <string>
#include <stack>
using namespace std;

void printstk(stack<char>& stk) {
	while (!stk.empty()) {
		cout << stk.top();
		stk.pop();
	}
}

int main() {
	ios_base::sync_with_stdio(false); cin.tie(nullptr); cout.tie(nullptr);
	string str; getline(cin, str); str += '\n';

	stack<char> stk;
	bool reverse = true;

	for (char& ch : str) {
		if (ch == '<') {
			printstk(stk);
			reverse = false;
			cout << ch;
		}
		else if (ch == '>') {
			cout << ch;
			reverse = true;
		}
		else if (reverse) {
			if (ch == ' ' || ch == '\n') {
				printstk(stk);
				cout << ch;
			}
			else {
				stk.push(ch);
			}
		}
		else if (!reverse) {
			cout << ch;
		}
	}

	return 0;
}

'Problem set' 카테고리의 다른 글

[백준] 3085 사탕 게임  (0) 2021.01.19
[백준] 2309 일곱 난쟁이  (0) 2021.01.19
[백준] 17087: 숨바꼭질 6  (0) 2020.12.29
[백준] 16194: 카드 구매하기 2  (0) 2020.12.29
[백준] 11727: 2×n 타일링 2  (0) 2020.12.29
복사했습니다!