#pragma warning (disable:4786) #pragma warning (disable:4503) #include #include #include #include #include #include #include #include using namespace std; class CaseInsensitiveString { public: bool operator() ( const string & lhs, const string & rhs ) const { return stricmp( lhs.c_str( ), rhs.c_str( ) ) < 0; } }; class CaseInsensitiveChar { public: bool operator() ( char lhs, char rhs ) const { return tolower( lhs ) < tolower( rhs ); } }; typedef map,CaseInsensitiveString> MAP_TYPE; string representative( const string & str ) { string result( str ); sort( result.begin( ), result.end( ), CaseInsensitiveChar( ) ); return result; } void printAnagramGroups( const MAP_TYPE & repMap, int minSize = 5 ) { MAP_TYPE::const_iterator itr = repMap.begin( ); while( itr != repMap.end( ) ) { const vector & group = itr++->second; if( group.size( ) >= minSize ) { cout << "[" << group.size( ) << "]: "; for( int i = 0; i < group.size( ); i++ ) cout << group[ i ] << " "; cout << endl; } } } int main( ) { ifstream dictFile( "dict.txt" ); if( !dictFile ) { cerr << "Dictionary file is not found" << endl; return 1; } string oneWord; MAP_TYPE repMap; while( dictFile >> oneWord ) repMap[ representative( oneWord ) ].push_back( oneWord ); printAnagramGroups( repMap ); return 0; }