#include <iostream>
#include <string>
#include <vector>
#include <map>
#include <cctype>
#include <algorithm>
std::string make_key( std::string str )
{
// remove non-alphabets
static const auto is_not_alpha = [] ( char c ) { return !std::isalpha(c) ; } ;
str.erase( std::remove_if( str.begin(), str.end(), is_not_alpha ), str.end() ) ;
// convert to lower case
for( char& c : str ) c = std::tolower(c) ;
// and sort
std::sort( str.begin(), str.end() ) ;
return str ;
}
using anagram_map = std::map< std::string, std::vector<std::string> > ;
anagram_map make_anagram_map( std::istream& stm )
{
anagram_map ana_map ;
std::string word ;
while( stm >> word ) ana_map[ make_key(word) ].push_back(word) ;
return ana_map ;
}
void look_up( const std::string& word, const anagram_map& ana_map )
{
auto iter = ana_map.find( make_key(word) ) ;
if( iter != ana_map.end() )
{
std::cout << "anagrams of '" << word << "':\n" ;
for( const auto& str : iter->second ) std::cout << " " << str << '\n' ;
}
else std::cout << "no anagrams of '" << word << "' were found\n" ;
}
#include <sstream>
int main()
{
std::istringstream stm( "Petal Maple Teal leapt!! Lepta tale Pleat ample LATE? Plate" ) ;
const anagram_map ana_map = make_anagram_map(stm) ;
look_up( "petal", ana_map ) ;
look_up( "late", ana_map ) ;
look_up( "quick", ana_map ) ;
}
I2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgojaW5jbHVkZSA8dmVjdG9yPgojaW5jbHVkZSA8bWFwPgojaW5jbHVkZSA8Y2N0eXBlPgojaW5jbHVkZSA8YWxnb3JpdGhtPgoKc3RkOjpzdHJpbmcgbWFrZV9rZXkoIHN0ZDo6c3RyaW5nIHN0ciApCnsKICAgIC8vIHJlbW92ZSBub24tYWxwaGFiZXRzCiAgICBzdGF0aWMgY29uc3QgYXV0byBpc19ub3RfYWxwaGEgPSBbXSAoIGNoYXIgYyApIHsgcmV0dXJuICFzdGQ6OmlzYWxwaGEoYykgOyB9IDsKICAgIHN0ci5lcmFzZSggc3RkOjpyZW1vdmVfaWYoIHN0ci5iZWdpbigpLCBzdHIuZW5kKCksI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