Problem D Ananagrams(map的使用)
2015-06-30 15:57
218 查看
题目链接:Problem D
题意:输入一些单词,找出所有满足如下条件的单词:该单词不能通过字母重排,得到输入文本中的另一个单词。在判断是否满足条件时,字母不区分大小写。
但是输出时应保留原始大小写,按字典序进行排列。
思路:把单词统一处理一下,然后放入map中,用vector记录下满足要求的单词,最后排序一下即可。
code:
题意:输入一些单词,找出所有满足如下条件的单词:该单词不能通过字母重排,得到输入文本中的另一个单词。在判断是否满足条件时,字母不区分大小写。
但是输出时应保留原始大小写,按字典序进行排列。
思路:把单词统一处理一下,然后放入map中,用vector记录下满足要求的单词,最后排序一下即可。
code:
#include <iostream> #include <string> #include <map> #include <vector> #include <algorithm> #include <cctype> using namespace std; vector<string> words; map<string, int> cnt; string repr(string str) { string ret = str; int len = ret.size(); for (int i = 0; i < len; ++i) ret[i] = tolower(ret[i]); sort(ret.begin(), ret.end()); return ret; } int main() { string str; while (cin >> str) { if ('#' == str[0]) break; words.push_back(str); string t = repr(str); if (cnt.count(t) == 0) cnt[t] = 0; ++cnt[t]; } int len = words.size(); vector<string> ans; for (int i = 0; i < len; ++i) { if (1 == cnt[repr(words[i])]) ans.push_back(words[i]); } len = ans.size(); sort(ans.begin(), ans.end()); for (int i = 0; i < len; ++i) cout << ans[i] << endl; return 0; }
相关文章推荐
- AES128加密解密方法
- Android Studio project有多个module情况下安装到手机出现多个app问题
- 二叉树的深度优先遍历与广度优先遍历
- webService之(二)java原生态客户端
- 基于REST 的 Web 服务:基础
- Java中的ReentrantLock和synchronized两种锁定机制的对比
- java面向对象的三大特性
- Oracle时间运算
- 北大ACM2785——4 Values whose Sum is 0~~双向搜索
- C++—函数
- centos6 安装mysql5.77(开发版)
- AFNetwork上传文件,下载文件,监控网络状态
- mysql BLOB类型
- Lua实现将字典数据分成10份
- 获取某一个view所在视图控制器,进而可以进行跳转等操作
- maven使用liqubase管理数据库
- Android Instrumentation模拟鼠标点击事件
- 刷新页面,页面,F5的方法
- 算法---奇偶
- Lesson11 vSphere VUM