编程珠玑--变位词集合

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

第二步的方法就比较多:(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的内容

转载于:https://www.cnblogs.com/cane/p/3779521.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值