※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※
이번에 볼 문제는 백준 26587번 문제인 Reverse이다.
문제는 아래 링크를 확인하자.
https://www.acmicpc.net/problem/26587
26587번: Reverse
At a contest you have been asked to write a program that reads in a line of text and reverses the order of all words that begin with vowels. Words that begin with consonants will keep their position in the line of text. You are not sure why you would ever
www.acmicpc.net
각 차례에 자음으로 시작하는 문자열을 출력해야하는지 모음으로 출력해야하는지를 기록해두는 벡터, 자음으로 시작하는 문자열들을 순서대로 모아둔 벡터, 모음으로 시작하는 문자열들을 순서대로 모아둔 벡터를 만들면 문제를 해결할 수 있다.
각 차례에 출력해야하는 종류의 문자열을 골라 종류별로 저장해둔 벡터에서 차례에 맞게 단어를 골라 출력해 문제를 해결하자.
아래는 제출한 소스코드이다.
#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
string s;
vector<string> v, c;
vector<int> is_v;
bool isvowel[128];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
isvowel['A'] = isvowel['E'] = isvowel['I'] = isvowel['O'] = isvowel['U'] = isvowel['a'] = isvowel['e'] = isvowel['i'] = isvowel['o'] = isvowel['u'] = 1;
while (getline(cin, s)) {
is_v.clear();
s += " ";
int slen = s.length();
int L = 0;
for (int R = 0; R < slen; R++) {
if (s[R] == ' ') {
string tmp = s.substr(L, R - L);
if (isvowel[tmp[0]]) is_v.emplace_back(1), v.emplace_back(tmp);
else is_v.emplace_back(0), c.emplace_back(tmp);
L = R + 1;
}
}
reverse(c.begin(), c.end());
for (auto& b : is_v) {
if (b) {
cout << v.back() << ' ';
v.pop_back();
}
else {
cout << c.back() << ' ';
c.pop_back();
}
}
cout << '\n';
}
}
728x90
'BOJ' 카테고리의 다른 글
[BOJ 6830 // C++] It's Cold Here! (0) | 2022.12.22 |
---|---|
[BOJ 4327 // C++] Combination Lock (0) | 2022.12.22 |
[BOJ 5340 // C++] Secret Location (0) | 2022.12.22 |
[BOJ 5342 // C++] Billing (0) | 2022.12.22 |
[BOJ 5292 // C++] Counting Swann's Coins (0) | 2022.12.22 |