The following two paragraphs are taken from programming Pearl River:
Give an English dictionary to find a set of all the modified words. For example, "pots", "stop", and "tops" are different words, because each word can be obtained by changing the order of letters in other words.
What we got, "Aha! The Brainwave "identifies every word in the dictionary, so that words in the same modified word class have the same identifier. Then, all words with the same logo are combined. This simplifies the problem of original modified words into two subproblems: Selecting the words with the same logo and the words with the same logo in the set.For the first question, we can use a sort-based identifier: to sort the letters in a word in alphabetical order. To solve the second problem, we arrange all words in the order of their identifiers.
# pragma warning (Disable: 4786) # include
# include
# include
# include
using namespace STD; int compare_string (const void * a, const void * B) {return * (char *) (a)-* (char *) (B);} void findanagram (char * file_name) {ifstream in_file (file_name ); string word_sort; string word; multimap
word_map; while (in_file> word) {word_sort = word; qsort (Word_sort.begin (), word_sort.length (), sizeof (char), compare_string); word_map.insert (make_pair (word_sort, word);} multimap
:: const_iterator iter = word_map.begin (); multimap
:const_iterator iter_end = word_map.end (); While (iter_end! = ITER) {cout
first <":"
second