Ananagrams (map的使用)

Most crossword puzzle fans are used to anagrams–groups of words with the same letters in different orders–for example OPTS, SPOT, STOP, POTS and POST. Some words however do not have this attribute, no matter how you rearrange their letters, you cannot form another word. Such words are called ananagrams, an example is QUIZ.

Obviously such definitions depend on the domain within which we are working; you might think that ATHENE is an ananagram, whereas any chemist would quickly produce ETHANE. One possible domain would be the entire English language, but this could lead to some problems. One could restrict the domain to, say, Music, in which case SCALE becomes a relative ananagram (LACES is not in the same domain) but NOTE is not since it can produce TONE.

Write a program that will read in the dictionary of a restricted domain and determine the relative ananagrams. Note that single letter words are, ipso facto, relative ananagrams since they cannot be “rearranged” at all. The dictionary will contain no more than 1000 words.

输入一些单词,找出所有满足如下条件的单词:该单词不能通过字母重排,得到输入文
本中的另外一个单词。在判断是否满足条件时,字母不分大小写,但在输出时应保留输入中的大小写,按字典序进行排列(所有大写字母在所有小写字母的前面)。

Input

Input will consist of a series of lines. No line will be more than 80 characters long, but may contain any number of words. Words consist of up to 20 upper and/or lower case letters, and will not be broken across lines. Spaces may appear freely around words, and at least one space separates multiple words on the same line. Note that words that contain the same letters but of differing case are considered to be anagrams of each other, thus tIeD and EdiT are anagrams. The file will be terminated by a line consisting of a single #.

Output

Output will consist of a series of lines. Each line will consist of a single word that is a relative ananagram in the input dictionary. Words must be output in lexicographic (case-sensitive) order. There will always be at least one relative ananagram.

样例输入:

ladder came tape soon leader acme RIDE lone Dreis peat
ScAlE orb eye Rides dealer NotE derail LaCeS drIednoel dire Disk mace Rob dries
#

样例输出:

Disk
NotE
derail
drIed
eye
ladder
soon

分析:

用一个vector存储出现过的单词,把所有单词统一成一个形式,即先把所有字母改为小写,将该单词排序,再放到map里统计可排列成相同单词的数目,把出现次数为1的单词输出。

code

#include <iostream>
#include <cstring>
#include <algorithm>
#include <string>
#include <vector>
#include <map>
using namespace std;/* 将单词标准化 */
string standard( string str ) {for( int i = 0; i < str.size(); i++ ) {str[i] = tolower( str[i] );}sort( str.begin(), str.end() );  //把单词中的字母按字典序排列return str;
}int main() {//freopen("input.txt", "r", stdin);vector<string> words;  //存储所有单词map<string, int> _map;  //记录标准化后的单词出现次数string str;while( cin >> str && str != "#" ) {words.push_back( str );if ( ! _map.count( standard(str) ) ) {  //单词未出现,将其数目标记为0_map[ standard(str) ] = 0;  //可看作初始化,尽管该单词已经出现,但此时标记为0即可,因为后续还会自加}_map[ standard(str) ]++;  //因为该单词出现了,所以次数加1(若首次出现,刚好0加1后变为1;若不是首次出现,次数加1)}vector<string> ans;vector<string>::iterator it;for( it = words.begin(); it != words.end(); it++ ) {if ( _map[ standard(*it) ] == 1 ) {ans.push_back( *it );  //将出现次数为1的单词放到ans中}}sort( ans.begin(), ans.end() );  //按字典序排序for( it = ans.begin(); it != ans.end(); it++ ) {cout << *it << endl;  //输出}
}

Ananagrams Uva 156相关推荐

  1. Ananagrams(sort+map) UVA - 156 统计字谜

    Ananagrams(sort+map) UVA - 156 统计字谜 Most crossword puzzle fans are used to anagrams - groups of word ...

  2. uva 156 Ananagrams

    题目大意: 输入一些单词,找出所有满足条件的单词,该单词不能通过重排,得到输入文本的另一个单词,在判断满足条件时,不区分大小写,但在输出时保留输入的大小写,按字典序排序 解体思路: 把所有单词保存,在 ...

  3. ICPC训练联盟2021寒假冬令营(6)_2021.01.25_笔记

    文章目录 试题链接 学习笔记 - C++ STL 简介 STL容器实验 序列式容器 关联式容器 集合容器 A - The Blocks Problem (POJ 1208, UVA 101) 中文释义 ...

  4. 集训der二周目学习(练习题+感悟)

    永动WA题机der二周目学习摘录 (一)有趣的题目 A - 看病要排队 HDU - 1873 Input Output Sample Input Sample Output 对题目的理解 代码 B - ...

  5. 很有趣的STL初学资料

    1.泛型程序设计简介与迭代器的介绍 2.常见的STL容器及其例题应用(UVA10474,UVA101,UVA10815,UVA156,UVA540,UVA136 HDU1027,CF501B,HDU1 ...

  6. π-Algorithmist分类题目(1)

    原题网站:Algorithmist,http://www.algorithmist.com/index.php/Main_Page π-Algorithmist分类题目(1) Sorting UVAL ...

  7. WaWa的奇妙冒险(第二周集训自闭现场)

    第二周周记 (一)例题记录 A-简单计算器 (水题,栈的运用) HDU - 1237 Input Output Sample Input Sample Output 理解 AC代码 B-计算 (逆波兰 ...

  8. STL之map映射(C++)

    文章目录 1.map 是一个键值对 (1)有关 key / value (3)底层实现--红黑树 2.map 的常用方法 (1)注意 (2)用法示例 3.map 的应用 反片语(Ananagrams, ...

  9. 紫书《算法竞赛入门经典》

    紫书<算法竞赛入门经典>题目一览 第3章 数组和字符串(例题) UVA 272 TEX Quotes UVA 10082 WERTYU UVA 401 Palindromes UVA 34 ...

最新文章

  1. mysql error104528000_腾讯云服务器CentOS安装JDK+Tomcat+MySQL详细步骤(以及遇到的各种坑)...
  2. AC自动机 HDOJ 5384 Danganronpa
  3. Java文件流应用:复制文件
  4. ucla 计算机专业 本科申请,美国计算机专业申请
  5. php ob静态缓存
  6. DNS A记录和CNAME记录
  7. C语言细节之四: 空指针
  8. linux QT 结束当前进程_嵌入式linux编程开发必备知识
  9. Trim or Discard or Unmap
  10. 树状数组 (数据结构)
  11. C#通过XElement写入XML文件
  12. npm:no such file /usr/local/lib/node_modules/vue-cli/node_modules/get-stream
  13. 疯狂的程序员 31-40
  14. 拆解查看unity游戏资源
  15. dlink网卡驱动 linux,D-Link友讯
  16. RGMII接口(KSZ9031)
  17. 公司官网建站笔记(四):从阿里云将域名转出,并将域名转入腾讯云
  18. ansible 001 __ 小斌文档 | ansible介绍和安装
  19. ASP.NET Development Server的Directory Browsing模式HTML垃圾代码
  20. 全球高分辨率(10m和30m)土地覆盖数据分享

热门文章

  1. css实现鼠标覆盖显示大图
  2. calc() ---一个会计算的css属性
  3. 监听微信、支付宝等移动app及浏览器的返回、后退、上一页按钮的事件方法
  4. 如何搭建lamp(CentOS7+Apache+MySQL+PHP)环境 [转]
  5. 小试---EF5.0入门实例1
  6. JavaScript 参考教程(二)——转载
  7. 根据数据库中的值用js设置RadioButtonList选中与否
  8. Linux Vim 光标错位,技术|Vim 复制粘帖格式错乱问题的解决办法
  9. php 数字加逗号,php数字满三位添加一逗号
  10. 不属于python标准库的是_python标准库和扩展库