-
[17413] 단어 뒤집기 2BOJ 2022. 5. 14. 13:47
https://www.acmicpc.net/problem/17413
17413번: 단어 뒤집기 2
문자열 S가 주어졌을 때, 이 문자열에서 단어만 뒤집으려고 한다. 먼저, 문자열 S는 아래와과 같은 규칙을 지킨다. 알파벳 소문자('a'-'z'), 숫자('0'-'9'), 공백(' '), 특수 문자('<', '>')로만 이루어져
www.acmicpc.net
아마 스택을 사용하면 조금 더 깔끔하게 짤 수 있을 것 같다.
'<'이 나오면, 단어가 있다면 출력하고, '>'이 나올때까지 그대로 출력하고 continue
' '이 나오면, 단어를 출력한다.
루프가 끝난 다음에도 아직 출력하지 않은 단어가 존재할 수 있으므로 한번 더 체크해준다.
#include <bits/stdc++.h>using namespace std;#ifdef ONLINE_JUDGEconstexpr bool local = false;#elseconstexpr bool local = true;#endifusing ll = long long;using pi = pair<ll, ll>;string s, cur;void print(void) {reverse(cur.begin(), cur.end());cout << cur;cur.clear();}int main(void) {if (!local) ios_base::sync_with_stdio(0), cin.tie(0);getline(cin, s);int i, S = s.length();for (i = 0; i < S; i++) {if (s[i] == '<') {print();while (s[i] != '>') cout << s[i++];cout << s[i];continue;}if (s[i] == ' ') {print();cout << ' ';continue;}cur.push_back(s[i]);}print();return 0;}
'BOJ' 카테고리의 다른 글
[4659] 비밀번호 발음하기 (0) 2022.05.14 [4307] 개미 (0) 2022.05.14 [17485] 진우의 달 여행 (Large) (0) 2022.05.14 [14607] 피자 (Large) (0) 2022.05.13 [2312] 수 복원하기 (0) 2022.05.02