map函数的应用:UVa156-Ananagrams
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
解题心得
- 考察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的更多相关文章
- 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* ...
- 实现python中的map函数
假设Python没有提供map()函数,自行编写my_map()函数实现与map()相同的功能.以下代码在Python 2.7.8中实现. 实现代码: def my_map(fun,num): i = ...
- js parseInt和map函数
今天看了一个js的题目["1","2","3"].map(parseInt),看到后脑海中浮现的答案是[1,2,3],但是看到正确答案后蒙了 ...
- JavaScript中map函数和filter的简单举例(转)
js的数组迭代器函数map和filter,可以遍历数组时产生新的数组,和python的map函数很类似1)filter是满足条件的留下,是对原数组的过滤:2)map则是对原数组的加工,映射成一一映射的 ...
- Python-lambda函数,map函数,filter函数
lambda函数主要理解: lambda 参数:操作(参数). lambda语句中,冒号前是参数,可以有多个,用逗号隔开,冒号右边的返回值.lambda语句构建的其实是一个函数对象 map函数: ma ...
- map() 函数
map()函数 map()是 Python 内置的高阶函数,它接收一个函数 f 和一个 list,并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 list 并返回. 例如,对于li ...
- STL : map函数的运用 --- hdu 4941 : Magical Forest
Magical Forest Time Limit: 24000/12000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Other ...
- Swift之map函数的强大之处
CollectionType Map 在CollectionType的extension中map方法的定义: extension CollectionType { /// Return an `Arr ...
- python map函数
map()函数 map()是 Python 内置的高阶函数,它接收一个函数 f 和一个 list,并通过把函数 f 依次作用在 list 的每个元素上,得到一个新的 list 并返回. 例如,对于li ...
- perl的map函数
perl的map函数的使用: 语法 map EXPR, LIST map BLOCK LIST 定义和使用 对list中的每个元素执行EXPR或BLOCK,返回新的list.对每一此迭代,$_中保存了 ...
随机推荐
- HDU4035(概率期望、树形、数学)
和ZOJ3329有些像,都是用期望列出来式子以后,为了解式子,设A[i],B[i],此题又多了C[i],然后用递推(此题是树形dp)去求得ABC,最后结果只跟ABC有关,跟列写的期望数组根本无关. 虽 ...
- NFX UNISTACK 介绍
学习.NET Core和ASP.NET Core,偶然搜索到NFX UNISTACK,现翻译一下Readme,工程/原文:https://github.com/aumcode/nfx NFX Serv ...
- Appium禁止appium setting和unlock在设备上重复安装
1.文件:/Applications/Appium.app/Contents/Resources/node_modules/appium/node_modules/appium-android-dri ...
- Unity Shader入门精要学习笔记 - 第6章 开始 Unity 中的基础光照
转自冯乐乐的<Unity Shader入门精要> 通常来讲,我们要模拟真实的光照环境来生成一张图像,需要考虑3种物理现象. 首先,光线从光源中被发射出来. 然后,光线和场景中的一些物体相交 ...
- sql server技巧
--查出数据最新的存储过程select name,modify_date from sys.procedures where modify_date>'2017-05-26 17:21:09.3 ...
- SQL注入原理与解决方法代码示例
一.什么是sql注入? 1.什么是sql注入呢? 所谓SQL注入,就是通过把SQL命令插入到Web表单递交或输入域名或页面请求的查询字符串,最终达到欺骗服务器执行恶意的SQL命令,比如先前的很多影视网 ...
- 洛谷 P1873 砍树
砍树 二分答案,难度较低. #include <iostream> #include <cstdio> #include <algorithm> using nam ...
- IIS 服务器支持下载apk 文件
前不久,在本地IIS文件下部署一个网站,可以下载apk文件,就是测试apk应用升级,发现访问不能下载,原因是IIS没有配置对这种apk文件的处理程序. 解决方案如下所示: 1.打开IIS, 找到MIM ...
- Objective-C Numbers
In Objective-C programming language, in order to save the basic data types like int, float, bool in ...
- linux下phpstudy的搭建以及网站的搭建
step1: 下载SSH连接到IP地址(阿里云上购买的) step2: 安装下载phpstudywget -c http://lamp.phpstudy.net/phpstudy.bin chmod ...