Ananagrams

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 aseries 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.

Sample Input

ladder came tape soon leader acme RIDE lone Dreis peat

ScAlE orb eye Rides dealer NotE derail LaCeS drIed

noel dire Disk mace Rob dries

Sample Output

Disk

NotE

derail

drIed

eye

ladder

soon


解题心得

  1. 考察map和set应用的一道题,很简单,因为不区分大小写,所以可以先将string中的所有大写转换成小写,然后sort将string排个序,排好序的string映射一下。最后再重复找一遍就可以了。

#include<bits/stdc++.h>
using namespace std;
map<string,int> Map;
set <string> se2;
int main()
{
string s;
while(cin >> s)
{
if(s == "#")
break;
string s1 = s;
for(string::iterator iter = s1.begin();iter!=s1.end();iter++)
*iter = tolower(*iter);
sort(s1.begin(),s1.end());
Map[s1]++;//先将所有的string小写转换,排序,映射一下;
se2.insert(s);//记录本身的单词
} for(set<string> :: iterator iter=se2.begin();iter!=se2.end();iter++)
{
string k = *iter;
for(string::iterator iter1=k.begin();iter1!=k.end();iter1++)
*iter1 = tolower(*iter1);
sort(k.begin(),k.end());
if(Map[k] == 1)//单词只出现过一次即符合条件
cout<<*iter<<endl;
}
return 0;
}

map函数的应用:UVa156-Ananagrams的更多相关文章

  1. map 函数----filter函数

    # map 函数 l = (1,2,4,5,6,7,8,9,) print(list(map(lambda x:x**2,l)))#使用list类型((map函数(lambda 匿名函数定义x值:x* ...

  2. 实现python中的map函数

    假设Python没有提供map()函数,自行编写my_map()函数实现与map()相同的功能.以下代码在Python 2.7.8中实现. 实现代码: def my_map(fun,num): i = ...

  3. js parseInt和map函数

    今天看了一个js的题目["1","2","3"].map(parseInt),看到后脑海中浮现的答案是[1,2,3],但是看到正确答案后蒙了 ...

  4. JavaScript中map函数和filter的简单举例(转)

    js的数组迭代器函数map和filter,可以遍历数组时产生新的数组,和python的map函数很类似1)filter是满足条件的留下,是对原数组的过滤:2)map则是对原数组的加工,映射成一一映射的 ...

  5. Python-lambda函数,map函数,filter函数

    lambda函数主要理解: lambda 参数:操作(参数). lambda语句中,冒号前是参数,可以有多个,用逗号隔开,冒号右边的返回值.lambda语句构建的其实是一个函数对象 map函数: ma ...

  6. map() 函数

    map()函数 map()是 Python 内置的高阶函数,它接收一个函数 f 和一个 list,并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 list 并返回. 例如,对于li ...

  7. STL : map函数的运用 --- hdu 4941 : Magical Forest

    Magical Forest Time Limit: 24000/12000 MS (Java/Others)    Memory Limit: 131072/131072 K (Java/Other ...

  8. Swift之map函数的强大之处

    CollectionType Map 在CollectionType的extension中map方法的定义: extension CollectionType { /// Return an `Arr ...

  9. python map函数

    map()函数 map()是 Python 内置的高阶函数,它接收一个函数 f 和一个 list,并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 list 并返回. 例如,对于li ...

  10. perl的map函数

    perl的map函数的使用: 语法 map EXPR, LIST map BLOCK LIST 定义和使用 对list中的每个元素执行EXPR或BLOCK,返回新的list.对每一此迭代,$_中保存了 ...

随机推荐

  1. 利用arguments对象在javaScript中实现重载(overload)

    一些概念: 重载(overload): 什么是: 相同函数名,不同参数列表的多个函数,在调用时,可根据传入参数的不同,自动选择对应的函数调用! 为什么: 减轻调用者的负担,一个函数名,可执行多种操作 ...

  2. Java Lambda表达式 Stream

    Stream Stream不是集合元素,它不是数据结构并不保存数据,而是有关算法和计算的,更像是一个高级版本的Iterator,原始版本的Iterator,用户只能显式地一个一个遍历元素并对其进行操作 ...

  3. 【5岁小孩都会】vs2013如何进行单元测试

    1,如何进行单元测试呢,打开vs  新建一个项目 然后在解决方案右键点击,如下图所示: 2,左侧点击 测试  ->单元测试项目 3)点击确定,如下图 4)在当前代码上右键点击,调试 或者运行测试 ...

  4. var声明提前 undefined

    1.同一代码块内,所有var声明都提前: 2.var 变量的初始化不提前,按顺序执行: 3."undefined"和undefined都存在于window中: 4.if(" ...

  5. “chm 已取消到该网页的导航”解决方案

    1. 右键单击该 CHM 文件,然后单击“属性”. 2. 单击“取消阻止”或者“解除锁定”. 3. 双击此 .chm 文件以打开此文件.

  6. iOS Block的本质(一)

    iOS Block的本质(一) 1.对block有一个基本的认识 block本质上也是一个oc对象,他内部也有一个isa指针.block是封装了函数调用以及函数调用环境的OC对象. 2.探寻block ...

  7. LibreOJ #103. 子串查找

    题目描述 这是一道模板题. 给定一个字符串 A AA 和一个字符串 B BB,求 B BB 在 A AA 中的出现次数. A AA 中不同位置出现的 B BB 可重叠. 输入格式 输入共两行,分别是字 ...

  8. codevs 1131 统计单词数 2011年NOIP全国联赛普及组

     时间限制: 1 s  空间限制: 128000 KB  题目等级 : 白银 Silver 题目描述 Description 一般的文本编辑器都有查找单词的功能,该功能可以快速定位特定单词在文章中的位 ...

  9. Android的Activity之间传对象的方法

    传值代码块 //Serializeable传递对象的方法 public void SerializeMethod(){ Person mPerson = new Person(); mPerson.s ...

  10. URAL 2048 Histroy(打表+模拟)

    因为年历是400年一个循环节的,所以递推出一年的情况,然后递推处理出一个循环节的情况.对于询问,求一个类似前缀和的东西就好了. 跑出来和比样例小一,把A和B加一以后交后AC... 写得时候注意变量的定 ...