有一步是必须处理的,就是对字符串进行排序;
第二步的方法就比较多:(1)采用字符串哈希(求值)(2)排序后转化为24位的位向量(3)直接排序后的字符串作为标识(书上的方法)
1 #include <iostream> 2 #include <string> 3 #include <fstream> 4 #include <map> 5 #include <algorithm> 6 #include <vector> 7 8 using namespace std; 9 10 11 int main() 12 { 13 ifstream dic("in.txt"); 14 vector<pair<string, string> > words_pair; 15 string word, word_sign; 16 while(dic >> word) { 17 word_sign = word; 18 sort(word_sign.begin(), word_sign.end()); 19 words_pair.push_back(pair<string, string>(word_sign, word)); 20 } 21 22 sort(words_pair.begin(), words_pair.end()); 23 vector<pair<string, string> >::const_iterator iter; 24 string pre = ""; 25 for (iter = words_pair.begin(); iter != words_pair.end(); iter++){ 26 if (pre != "" && (*iter).first != pre) { 27 cout << endl; 28 } 29 cout << (*iter).second << " "; 30 pre = (*iter).first; 31 } 32 33 cout << endl; 34 return 0; 35 }
haired
hardier harried
trihedra
hardwire
airshed dashier hardies shadier
airsheds radishes
hardiest
ravished
dishware rawhides
hayrides
airthed
rawhide
hayride hydriae
以上是in.txt的内容