※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※
이번에 볼 문제는 백준 14014번 문제인 Dudu of English이다.
문제는 아래 링크를 확인하자.
https://www.acmicpc.net/problem/14014
14014번: Dudu of English
"your uf of library have bugs" -- Dudu, 2015 Dudu realized that English is a very inefficient language. To address this issue he created his own dialect: dudu of english. Here are a couple of examples: From "The Union-Find in your library has bugs!" to
www.acmicpc.net
주어지는 문자열을 주어진 규칙 순서대로 처리하는 문제이다.
문제에서 주어지는 문장의 줄 수와 상관없이 한 줄에 들어가는 로마자의 개수를 기준으로 개행해 출력하므로 단어 하나하나를 읽어가면서 처리하고 출력하는 것으로 문제를 해결할 수 있다.
tolower함수를 이용해 모든 문자를 소문자로 편하게 만들 수 있다.
of-word의 처리는 set을 이용하여 간단히 할 수 있다.
각 문자가 모음인지, 또는 로마자인지를 확인하는 것은 배열을 이용해 쉽게 할 수 있다.
"!@#$%^!#@%" 등의 단어를 받아 읽어 처리하면 ""가 되므로, 구현을 잘못 하면 최종 문자열에 공백이 둘 이상 붙어있는, 규칙에 어긋나는 부분이 생길 수 있음에 유의하자.
아래는 제출한 소스코드이다.
#include <iostream>
#include <set>
using namespace std;
set<string> st;
int arr[128];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
for (int i = 'a'; i <= 'z'; i++) arr[i] = 1;
arr['a'] = arr['e'] = arr['i'] = arr['o'] = arr['u'] = 2;
st.insert("to");
st.insert("into");
st.insert("onto");
st.insert("above");
st.insert("below");
st.insert("from");
st.insert("by");
st.insert("is");
st.insert("at");
int total = 0;
string s; cin >> s;
while (cin >> s) {
for (auto& l : s) {
l = tolower(l);
}
if (st.find(s) != st.end()) s = "of";
int cnt = 0;
for (auto l : s) {
if (arr[l] == 2) cnt++;
}
cnt >>= 1;
string ss = "";
for (auto l : s) {
if (arr[l] == 2 && cnt) cnt--;
else if (arr[l]) ss += l;
}
if (ss != "") {
if (total + ss.length() > 20) {
total = 0;
cout << ss << '\n';
}
else {
total += ss.length();
cout << ss << ' ';
}
}
}
}
'BOJ' 카테고리의 다른 글
[BOJ 12022 // C++] 짝 (0) | 2022.05.19 |
---|---|
[BOJ 2174 // C++] 로봇 시뮬레이션 (0) | 2022.05.18 |
[BOJ 14010 // C++] Where To Go? (0) | 2022.05.16 |
[BOJ 25193 // C++] 곰곰이의 식단 관리 (0) | 2022.05.15 |
[BOJ 25195 // C++] Yes or yes (0) | 2022.05.15 |