首页 > 其他 > 详细

编程珠玑--变位词集合

时间:2014-06-10 20:02:03      阅读:300      评论:0      收藏:0      [点我收藏+]

有一步是必须处理的,就是对字符串进行排序;

第二步的方法就比较多:(1)采用字符串哈希(求值)(2)排序后转化为24位的位向量(3)直接排序后的字符串作为标识(书上的方法)

 

bubuko.com,布布扣
 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 }
bubuko.com,布布扣
bubuko.com,布布扣
haired
hardier harried
trihedra
hardwire
airshed dashier hardies shadier
airsheds radishes
hardiest
ravished
dishware rawhides
hayrides
airthed
rawhide
hayride hydriae
bubuko.com,布布扣

以上是in.txt的内容

编程珠玑--变位词集合,布布扣,bubuko.com

编程珠玑--变位词集合

原文:http://www.cnblogs.com/cane/p/3779521.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!