UVA 156 Ananagrams

把每个单词“标准化“,即全部转化为小写再排序,然后放到map中统计。

 1 #include "iostream"
 2 #include "string"
 3 #include "cctype"
 4 #include "vector"
 5 #include "map"
 6 #include "algorithm"
 7 using namespace std;
 8 map<string, int> cnt;
 9 vector<string> words;
10 string repr(const string& s)//化为小写
11 {
12     string ans = s;
13     for (int i = 0; i < ans.length(); i++)
14     {
15         ans[i] = tolower(ans[i]);
16     }
17     sort(ans.begin(), ans.end());
18     return ans;
19 }
20 int main()
21 {
22     int n = 0;
23     string s;
24     while (cin>>s)
25     {
26         if (s[0] == '#')
27             break;
28         words.push_back(s);//压进去
29         string r = repr(s);//化为小写
30         if (!cnt[r])
31             cnt[r] = 0;
32         cnt[r]++;
33     }
34     vector<string> ans;
35     for (int i = 0; i < words.size(); i++)
36     {
37         if (cnt[repr(words[i])] == 1)//重复度为1则为答案
38             ans.push_back(words[i]);
39         sort(ans.begin(), ans.end());//排序    
40     }
41     for (int i = 0; i < ans.size(); i++)
42     {
43         cout << ans[i] << endl;
44     }
45     return 0;
46 }

猜你喜欢

转载自www.cnblogs.com/fudanxi/p/10352925.html
今日推荐