Created
July 12, 2021 12:15
-
-
Save PedroRacchetti/1fec8d9f258d75fa3cd1dca2e63a6d5f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include<bits/stdc++.h> | |
using namespace std; | |
vector<int> alfabeto; //manter o alfabeto em um vector | |
vector<pair<int, int> > vogais; //guardar as vogais | |
//como vector de pair, que guarde seu indice | |
string s; | |
vector<char> ans; | |
bool isvogal(int i){ | |
//função que verifica se uma letra (representada por um número) é uma vogal | |
if( i == 'a' - 'a' || i == 'e' - 'a' || i == 'i' - 'a' || i == 'o' - 'a' || i == 'u' - 'a') return true; | |
return false; | |
} | |
int main(){ | |
//inicializamos o alfabeto e as vogais | |
for(int i = 0; i < 26; i++){ | |
if( i == 'w' - 'a' || i == 'y' - 'a' ) continue; | |
alfabeto.push_back(i); | |
if(isvogal(i)){ | |
vogais.push_back({alfabeto.size() - 1, i}); | |
} | |
} | |
cin >> s; | |
for(int i = 0; i < s.size(); i++){ | |
if(isvogal(s[i] - 'a')){ | |
//se uma letra é uma vogal, basta inseri-la na resposta | |
ans.push_back(s[i]); | |
continue; | |
} | |
//sabemos que a letra atual é uma consoante | |
ans.push_back(s[i]); | |
int id; | |
//encontramos a consoante no alfabeto | |
for(int j = 0; j < alfabeto.size(); j++) | |
if(alfabeto[j] == s[i] - 'a') id = j; | |
//encontramos as vogais mais próximas a direita e a esquerda | |
//caso não exista vogais para um dos lados, | |
//colocamos um número que não sera usado | |
int idesquerda = -500, iddireita = 500; | |
for(int j = id; j >= 0; j--){ | |
if(isvogal(alfabeto[j])){ | |
idesquerda = j; | |
break; | |
} | |
} | |
for(int j = id; j < alfabeto.size(); j++){ | |
if(isvogal(alfabeto[j])){ | |
iddireita = j; | |
break; | |
} | |
} | |
//comparamos as distâncias das vogais, e escolhemos a correta | |
if(id - idesquerda <= iddireita - id) ans.push_back('a'+alfabeto[idesquerda]); | |
else ans.push_back('a' + alfabeto[iddireita]); | |
//se a consoante for z, escolhemos ela | |
//se nao, escolhemos a proxima consoante no alfabeto. | |
if(id == alfabeto.size() - 1) ans.push_back(s[i]); | |
else{ | |
for(int j = id + 1; j < alfabeto.size(); j++){ | |
if(!isvogal(alfabeto[j])){ | |
ans.push_back('a' + alfabeto[j]); | |
break; | |
} | |
} | |
} | |
} | |
//imprimimos a resposta. | |
for(int i = 0; i < ans.size(); i++){ | |
cout<<ans[i]; | |
} | |
cout<<endl; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment