这是我使用正则表达式编写的内容:
#include <iostream>
#include <regex>
std::string solution(std::string i){
std::regex re(
"([bcdfghjklmnpqrstvxz])"
"([aeiouy])"
"([bcdfghjklmnpqrstvxz])");
return std::regex_replace(i, re, "$1$2$2$3");
}
int main(){
for (std::string i; std::cin >> i;) {
//std::cout << i << " -> " << solution(i) << "\n";
std::cout << solution(i) << "\n";
}
}
回应@cigien 的评论:
std::string solution(std::string i)
{
std::regex re(
"([bcdfghjklmnpqrstvxz])"
"([aeiouy])"
"(?=[bcdfghjklmnpqrstvxz])");
return std::regex_replace(i, re, "$1$2$2");
}
这也将处理存在多个元音的“中间辅音”的情况。
随机测试:
(echo informatics; sort -R /etc/dictionaries-common/words | head -9) | ./sotest
infoormaatiics
niiceest
afteereeffeect
Lamaar
poortaageed
Asuunción's
viiviiseection
opaaquer
inteerruupts
Anaabaaptiist's
审查您的代码
最突出的问题是 insert 没有使用合适的参数集调用:https://en.cppreference.com/w/cpp/string/basic_string/insert
最接近的是使用重载(3):
basic_string& insert( size_type index, const CharT* s, size_type count );
所以,基本上
i.insert(j + 1, i.data() + j, 1);
附注是,由于 i 被修改,缓存 size() 将导致一些匹配未处理,因为您提前放弃了循环。
for(size_t j=1; j<i.size()-1; j++){
解决这个问题。
std::string vo = "aeuio";
似乎效率很低。为什么不使用 lambda - 更具表现力和效率:
static inline bool is_vowel(char ch) {
switch (ch) {
case 'a': case 'e': case 'u': case 'i': case 'o':
case 'A': case 'E': case 'U': case 'I': case 'O': return true;
default: return false;
}
}
然后也许:
std::string solution(std::string input)
{
auto cons_around = [&input](int index) {
return not(is_vowel(input.at(index - 1)) //
or is_vowel(input.at(index + 1)));
};
for (size_t i = 1; i < input.size() - 1; i++) {
if (cons_around(i) and is_vowel(input.at(i)))
input.insert(i + 1, input.data() + i, 1);
}
return input;
}
严格来说,这并不能满足要求(即使它现在可以识别大写元音),因为它假定所有非元音都是辅音(存在分词和数字等)。
但结果更接近:
http://coliru.stacked-crooked.com/a/f9db3887705a3b4c
#include<string>
#include<iostream>
static inline bool is_vowel(char ch) {
switch (ch) {
case 'a': case 'e': case 'u': case 'i': case 'o':
case 'A': case 'E': case 'U': case 'I': case 'O': return true;
default: return false;
}
}
std::string solution(std::string input)
{
auto cons_around = [&input](int index) {
return not(is_vowel(input.at(index - 1)) //
or is_vowel(input.at(index + 1)));
};
for (size_t i = 1; i < input.size() - 1; i++) {
if (cons_around(i) and is_vowel(input.at(i)))
input.insert(i + 1, input.data() + i, 1);
}
return input;
}
int main(){
int t;
std::cin >> t;
std::string i;
while (t-- && std::cin >> i) {
std::cout << solution(i) << "\n";
}
}
打印
foo
baar
quux