码迷,mamicode.com
首页 > 其他好文 > 详细

UVa156 Ananagrams (STL)

时间:2016-08-31 20:34:34      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:

链接:http://acm.hust.edu.cn/vjudge/problem/19294
分析:map容器的应用。map容器按key值从小到大排序,所以自定义类型作为key时要定义“小于”运算符,还有map重载“[]”运算符,可以像数组一样使用。将每个单词“标准化”(将字母转换成小写并按字典序从小到大),cnt记录每个“标准化”后单词的个数,words保存出现过的所有单词,最后遍历words数组,将“标准化”后只出现一次的单词保存最后按字典序排序后输出。

 1 #include <iostream>
 2 #include <string>
 3 #include <vector>
 4 #include <map>
 5 #include <algorithm>
 6 using namespace std;
 7 
 8 vector<string> words;
 9 map<string, int> cnt;
10 
11 string repr(const string& s) {
12     string ans = s;
13     for (int i = 0; i < s.length(); i++)
14         ans[i] = tolower(ans[i]);
15     sort(ans.begin(), ans.end());
16     return ans;
17 }
18 
19 int main() {
20     string s;
21     while (cin >> s) {
22         if (s[0] == #) break;
23         words.push_back(s);
24         string r = repr(s);
25         if (!cnt.count(r)) cnt[r] = 0;
26         cnt[r]++;
27     }
28     vector<string> ans;
29     for (int i = 0; i < words.size(); i++)
30         if (cnt[repr(words[i])] == 1) ans.push_back(words[i]);
31     sort(ans.begin(), ans.end());
32     for (int i = 0; i < ans.size(); i++)
33         cout << ans[i] << endl;
34     return 0;
35 }

 

 

UVa156 Ananagrams (STL)

标签:

原文地址:http://www.cnblogs.com/XieWeida/p/5827314.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!