728x90
오랜만에 string 문제를 들고와봤다.
문제는 쉬운편인데, 나는 진짜 단순하게 풀었다.
조금 더 생각하면 깔끔한 코드가 나올 것 같기도 한데,
일단 queue에 무지성으로 때려박고 tag와 단어, 공백 3가지 유형으로 넣어주었다.
출력할때는 단어인 경우에만 reverse를 이용해서 출력해주었다.
< 코드 >
#include <iostream>
#include <algorithm>
#include <queue>
using namespace std;
/*
a-z, 0-9, ,<,>(두개 개수 같음)
태그 말고 단어만 뒤집어서 출력
태그는 <>안으로 길이는 3이상, 공백 포함가능
태그와 단어사이에는 공백 없음
---
<psudo code>
// inputs
string tag;
string words;
queue<string> q;
if (input starts '<') {
if(words != empty) q.push(words);
words = null;
while (input == '>') {
tag += input;
}
q.push(tag);
tag = null;
}
else {
words += input;
}
*/
queue<string> q;
void makeQueue(string inputs) {
string tag;
string words;
string gap = " ";
for(int i=0; i<inputs.size(); i++) {
if(inputs[i] == '<') {
if(!words.empty()) {
q.push(words);
words.clear();
}
tag += inputs[i];
while(inputs[i] != '>') {
i++;
tag += inputs[i];
}
q.push(tag);
tag.clear();
}
else {
if((inputs[i] == ' ') & (!words.empty())) {
i++;
q.push(words);
words.clear();
q.push(gap);
}
words += inputs[i];
}
}
if (!words.empty()) q.push(words);
}
void printQueue() {
while(!q.empty()) {
string x = q.front();
q.pop();
if(x.front()=='<') {
cout << x;
}
else {
reverse(x.begin(), x.end());
cout << x;
}
}
}
int main(void)
{
ios::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
string inputs;
getline(cin, inputs);
makeQueue(inputs);
printQueue();
return 0;
}
728x90
'Algorithm > 백준과 프로그래머스' 카테고리의 다른 글
[C++] 16926. 배열 돌리기1 (0) | 2024.03.11 |
---|---|
[C++] 16234. 인구이동 (0) | 2024.03.07 |
[프로그래머스/C++] LV2. 기능개발 (0) | 2024.02.02 |
[프로그래머스] LV1. 키패드 누르기 (0) | 2024.01.19 |
[C++] 프로그래머스 LV1. 가장 많이 받은 선물 (Kakao 기출문제) (0) | 2024.01.17 |