// Método que verificar quais são as maiores substrings palíndromas dentro de uma string void Trie::verificaPalindroma(string word) { word = converteString(word); // 1º Converte todas as letras para maiúsculas this->criarTrieSufixos(word); // 2º Cria a trie de sufixos daquela palavra string aux2, aux; // strings auxiliares // aux - string invertida que é percorrida sempre diminuindo a letra do inicio // aux2 - string que recebe a maior substring palimdroma encontra a partir de aux aux = inverteString(word); // aux recebe a string invertida aux.erase(aux.begin()); // remover o 1º caracter que quando invertido é o '$' int tamMaiorPalindroma = 0; // tamanho da maior palindroma encontrada int i = 0; while (aux.compare("$") > 0) // Enquanto a string invertida não é '$' { if (aux.size() >= tamMaiorPalindroma) // Se o tamanho da string for menor que a maior palindroma não tem porque seguir procurando { // já que por mais que encontre outra palindroma não tem como ela ser maior ou igual aux2 = this->buscaMaiorString(aux); // aux2 recebe a maior string encontrada a partir de aux if ((aux2.size() == tamMaiorPalindroma) && (aux2.compare(inverteString(aux2)) == 0)) // Se o tamanho for igual e realmente for uma palíndroma { // Então insere na trie de palindromas aux2+='$'; palindromas->insereString(aux2); } if ((aux2.size() > tamMaiorPalindroma) && (aux2.compare(inverteString(aux2)) == 0)) // Se o tamanho por maior e for realmente uma palíndroma { // Então apaga a trie anterior, cria uma nova e insere a string nela free(palindromas); palindromas = new Trie(); aux2+='$'; palindromas->insereString(aux2); tamMaiorPalindroma = aux2.size()-1; // Nese caso o tamanho da maior palíndroma passa a ser o da string atual } aux.erase(aux.begin()); // Remove o 1º caracter da string e recomeça a busca } // Caso o tamanho da string for maior que a string restante pode sair do laço else { break; } } // Se o tamanho da maior palíndroma for igual a 1 então é porque só encontrou letras separadas como palíndromas nesse caso não encontrou nenhuma palíndroma de verdade if (tamMaiorPalindroma == 0) { cout<< "Não possui nenhuma palindroma"<< endl; } // Senão imprime a trie de palíndromas else { palindromas->imprimeTrie(); } }
void inverteString(char* str, int inicio, int fim){ if(inicio >= fim) return; int aux; aux = str[inicio]; str[inicio] = str[fim]; str[fim] = aux; inverteString(str,inicio+1,fim-1); }
void main(){ char str[4] = {"OLAP"}; inverteString(str,0,strlen(str)-1); puts(str); }