본문 바로가기
Algorithm/C++

[백준 17413번] 단어 뒤집기 2

by imagineer_jinny 2022. 4. 19.

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

 

17413번: 단어 뒤집기 2

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

www.acmicpc.net

 

#include <iostream>
#include <string>
#include <stack>
using namespace std;
void print(stack<char> &s) {
    while (!s.empty()) {
        cout << s.top();
        s.pop();
    }
}
int main() {
    string str;
    getline(cin, str);
    bool tag = false;
    stack<char> s;
    for (char ch : str) {
        if (ch == '<') {
            print(s);
            tag = true;
            cout << ch;
        } else if (ch == '>') {
            tag = false;
            cout << ch;
        } else if (tag) {
            cout << ch;
        } else {
            if (ch == ' ') {
                print(s);
                cout << ch;
            } else {
                s.push(ch);
            }
        }
    }
    print(s);
    cout << '\n';
    return 0;
}

'Algorithm > C++' 카테고리의 다른 글

[프로그래머스 lv1] 행렬의 덧셈  (0) 2022.05.13
[백준 1110번] 더하기 사이클  (0) 2022.04.21
[백준 1874번] 스택 수열  (0) 2022.04.10
[백준 5635번] 생일  (0) 2022.04.09
[백준 9012] 괄호  (0) 2022.04.08

댓글