一个字符串集合
{"...", "...", ... }
找到相同的字符串,这样的字符串是:包含的字符相同,字符的个数也相同
解决方案:
先对每个字符串排序
然后对排完序的字符串整体排序
遍历整个字符串集合,即可得到结果
1 #include <iostream>
2 #include <vector>
3 #include <map>
4 #include <string>
5 #include <algorithm>
6 using namespace std;
7
8 int main()
9 {
10 vector<string> data;
11 data.push_back("cafe");
12 data.push_back("baidu");
13 data.push_back("duiba");
14 data.push_back("thisone");
15 data.push_back("iseasy");
16 data.push_back("esayis");
17 data.push_back("siesay");
18 data.push_back("esaysi");
19
20 multimap<string, string> mem;
21 for (vector<string>::size_type i = 0; i != data.size(); ++i)
22 {
23 string tmp(data[i]);
24 sort(tmp.begin(), tmp.end());
25 mem.insert(make_pair(tmp, data[i]));
26 }
27 if (mem.size() <= 1)
28 {
29 return 0;
30 }
31 for (multimap<string, string>::const_iterator cit = mem.begin(); cit != mem.end(); ++cit)
32 {
33 cout << cit->first << '\t' << cit->second << endl;
34 }
35 cout << "===================" << endl;
36 multimap<string, string>::const_iterator cit1, cit2, cit3;
37 cit1 = mem.begin();
38 cit3 = cit1;
39 cit2 = ++cit3;
40 bool f = false;
41 while (cit2 != mem.end())
42 {
43 if (cit1->first == cit2->first)
44 {
45 if (!f)
46 {
47 f = true;
48 cout << cit1->first << '(' << cit1->second << ')' << '\t' << cit2->first << '(' << cit2->second << ')' << '\t';
49 }
50 else
51 {
52 cout << cit2->first << '(' << cit2->second << ')' << '\t';
53 }
54 }
55 else
56 {
57 if (f)
58 {
59 cout << endl;
60 f = false;
61 }
62 }
63 ++cit1;
64 ++cit2;
65 }
66 return 0;
67 }
68
posted on 2011-07-11 13:34
unixfy 阅读(528)
评论(0) 编辑 收藏 引用