UVa-156 Ananagrams(map映射)

 1 #include <iostream>
 2 #include <algorithm>
 3 #include <cmath>
 4 #include <cstdio>
 5 #include<cstdlib>
 6 #include<cstring>
 7 #include <vector>
 8 #include <queue>
 9 #include <map>
10 #include <sstream>
11 
12 using namespace std;
13 #define maxn 100100
14 map<string, int>ma;
15 std::vector<string> words;
16 string func(string str) {
17     string ans = str;
18     for (int i = 0; i < ans.length(); i++) {
19         ans[i] = tolower(ans[i]);
20     }
21 
22     sort(ans.begin(), ans.end());
23     return ans;
24 }
25 int main() {
26     std::ios::sync_with_stdio(false);
27     string str;
28     while (cin >> str) {
29         if (str[0] == '#') {
30             break;
31         }
32         words.push_back(str);
33         string r = func(str);
34         if (!ma.count(r)) {
35             ma[r] = 0;
36         }
37         ma[r]++;
38     }
39     std::vector<string> ans;
40     for (int i = 0; i < words.size(); i++) {
41         if (ma[func(words[i])] == 1) {
42             ans.push_back(words[i]);
43         }
44         sort(ans.begin(), ans.end());
45 
46     }
47     for (int i = 0; i < ans.size(); i++) {
48         cout << ans[i] << endl;
49     }
50     return 0;
51 }
原文地址:https://www.cnblogs.com/lino/p/9953871.html