※ 글쓴이는 취미로 코딩을 익혀보는 사람이라 정확하지 않은 내용을 담고 있을 수 있다 ※
이번에 볼 문제는 백준 11341번 문제인 Eakspay igpay atinlay?이다.
문제는 아래 링크를 확인하자.
https://www.acmicpc.net/problem/11341
11341번: Eakspay igpay atinlay?
Your job is to translate English phrases into Pig Latin. There are only two rules you need to know. First, for words beginning with a consonant or consonant cluster, you must cut off the consonant or consonant cluster, attach it to the end of the word, and
www.acmicpc.net
주어진 각 단어를 지문에 주어진 규칙에 따라 Pig Latin으로 변환해 출력하는 문제이다.
입력을 줄단위로 입력받고, 공백을 기준으로 입력을 각 단어단위로 나누어 단어별로 올바르게 처리해 문제를 해결하자. 줄단위의 입력은 getline을 이용해 받을 수 있다.
자음만으로 구성된 단어를 처리할 때 인덱스가 문자열의 길이를 벗어나지 않게 유의해 구현하자.
아래 구현의 vowel배열과 같은 배열을 만들어둔다면 각 문자가 모음인지 여부를 확인하는 것을 간단히 구현할 수 있다.
이 문제에서 'y'는 모음으로 취급함에 유의하자.
아래는 제출한 소스코드이다.
#include <iostream>
#include <string>
using namespace std;
int T;
string s;
int slen;
bool vowel[128];
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
vowel['a'] = vowel['e'] = vowel['i'] = vowel['o'] = vowel['u'] = vowel['y'] = 1;
getline(cin, s);
T = stoi(s);
while (T--) {
getline(cin, s); s += " ";
slen = s.length();
int L = 0, idx = 0;
while (idx < slen) {
if (s[idx] != ' ') idx++;
else {
string ss = s.substr(L, idx - L);
if (vowel[ss[0]]) {
cout << ss << "yay ";
}
else {
int id = 0;
while (id < ss.length() && !vowel[ss[id]]) id++;
if (id < ss.length()) ss = ss.substr(id, ss.length() - id) + ss.substr(0, id);
cout << ss << "ay ";
}
idx++;
L = idx;
}
}
cout << '\n';
}
}
'BOJ' 카테고리의 다른 글
[BOJ 9460 // C++] 메탈 (0) | 2023.09.30 |
---|---|
[BOJ 2318 // C++] 상사 찾기 (0) | 2023.09.29 |
[BOJ 6080 // C++] Bad Grass (0) | 2023.09.27 |
[BOJ 25498 // C++] 핸들 뭘로 하지 (0) | 2023.09.26 |
[BOJ 25496 // C++] 장신구 명장 임스 (0) | 2023.09.25 |