单词的txt是从网上下的,简单用awk ‘{print $1}’ > SimpleEngDict.txt处理了下。

然后就是找出变位词的过程,我的这个单词量比较小,13372个单词。

用了STL里的set和map。

/*
 * =====================================================================================
 *       Filename:  findAllAnagram.cpp
 *    Description:  
 *        Created:  03/24/2012 04:29:18 PM
 *       Compiler:  gcc
 *
 *         Author:  zhy (), [email protected]
 *        Company:  
 * =====================================================================================
 */
#include <iostream>
#include <fstream>
#include <string>
#include <map>
#include <set>
using namespace std;

void swap(char &a, char &b)
{
    char c = a;
    a = b;
    b = c;
}

string getSortedString(string str)
{
    int len = str.size()-1;
    for ( int i=0; i<len; ++i)
        for ( int j=len; j>i; --j) {
            if (str[j] < str[j-1])
                swap(str[j],str[j-1]);
        }

    return str;
}


int main()
{
    ifstream input;
    input.open("../DataRelation/SimpleEngDict.txt",ifstream::in);
    string word("ufoplayer");
    map<string,set<string> > AnagramMap;

    while (getline(input,word))
        AnagramMap[getSortedString(word)].insert(word);
    cout << "get map ok!" << endl;

    map<string,set<string> >::iterator AnagramMapIter = AnagramMap.begin();

    while (AnagramMapIter!=AnagramMap.end()) {
        if (AnagramMapIter->second.size()>1) {
            for (set<string>::iterator iter = AnagramMapIter->second.begin(); iter!=AnagramMapIter->second.end(); ++iter)
                cout << *iter << " ";
            cout << endl;
        }
        AnagramMapIter++;
    }

    return 0;
}

看下部分输出:

backward drawback
aboard abroad
catalogue coagulate
ascertain sectarian
avarice caviare
casual causal
magenta magnate
assuage sausage
parental paternal
marital martial
bad dab
broadside sideboard
abed bead
abridge brigade
beard bread debar
braid rabid
board broad