UVa-156 Ananagrams
2015-08-18 15:21
429 查看
#include <iostream> #include <cstdio> #include <map> #include <vector> #include <set> #include <algorithm> using namespace std; map<string, int> cnt; //储存标准化后的单词出现次数 vector<string> word; //用来保存原始单词(原顺序 原大小写) string str; inline string norm(string temp) //将单词temp标准化 { for(int i = 0; i < (int)temp.length(); i ++) temp[i] = tolower(temp[i]); sort(temp.begin(), temp.end()); return temp; } int main() { while(cin >> str) { if(str == "#") break; word.push_back(str); //保存原始单词到word string substitute = norm(str); //substitute接收函数返回值 if(cnt.count(substitute) == 0) //如果map中之前没有substitute 则初始化0 cnt[substitute] = 0; cnt[substitute] ++; //记录标准化后出现的次数(出现多次即是不符合题意的) } set<string> ans; //存入满足题意的原单词 并自动从小到大排序 for(int i = 0; i < (int)word.size();i ++) if(cnt[norm(word[i])] == 1) //从里往外看 word[i]是原单词 norm函数将此单词标准化 cnt判断标准化后的出现几次(1次即是满足题意的) ans.insert(word[i]); //存入set for(set<string>::iterator it = ans.begin(); it != ans.end(); it ++) cout << *it << endl; //输出 return 0; }
题意:输入一些单词,找出所有满足如下条件的单词:该单词不能通过字母重排,得到输入文本中的另外一个单词。在判断是否满足条件时,字母不分大小写,但在输出时应保留输入得大小写,按字典序进行排序(所有大写字母在所有小写字母的前面)。
题解:如上。主要是掌握map用法
相关文章推荐
- 实现图片放大缩小,点击热点响应事件
- MySql分析算法作品索引(马上,只是说说而已B-tree)
- IOS 动画播放案例
- Diesel-powered is famous globally among the the majority of exclusive view businesses
- win7安装mysql
- Lua语言介绍
- Leetcode#67||Add Binary
- git使用总结
- AndroidView绘制流程分析及自定义View、ViewGroup进阶
- 浅谈Java动态代理
- Linux按时间排序文件
- Android更改 PreferenceFragment 的背景颜色
- Jmeter 在 linux 命令行下报“获取连接时间过长”的异常
- WPF入门教程系列十七——WPF中的数据绑定(三)
- oracle数据库的导入导出命令
- Android更改 PreferenceFragment 的背景颜色
- hdu 4288 线段树 暴力 **
- 【Mockplus视频教程】《10分钟玩转Mockplus》
- poj1002
- 一致性哈希算法(consistent hashing)