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.对每一此迭代,$_中保存了 ...
随机推荐
- 问题: 查看某个文件的修改记录| git log 高级用法
参考文章: git查看某个文件的修改历史 5.3 Git log 高级用法 基本步骤 git log --pretty=oneline [文件名] git show [节点] git log 两周高级 ...
- HTML的基础结构
<html>内容</html> 解释:HTML文档的文档标记,也称为HTML开始标记 功能:这对标记分别位于网页的最前端和最后端 <head>内容</head ...
- C51存储的优化
我们知道51单片机只有128BYTE的RAM, 有的增强型有XRAM,此时编程时就要注意,否则就有可能超出空间 1 对于各模块的全局变量.静态变量.全局枚举型常量,静态枚举型常量.个别操作存储(如外部 ...
- Unity Shader入门精要学习笔记 - 第8章 透明效果
转载自 冯乐乐的 <Unity Shader入门精要> 透明是游戏中经常要使用的一种效果.在实时渲染中要实现透明效果,通常会在渲染模型时控制它的透明通道.当开启透明混合后,当一个物体被渲染 ...
- 重新安装Magento CE 2.1.0
删除 var/cache 文件夹 删除 var/generation文件夹 删除 app/etc/config.php 删除 app/etc/env.php 如果您觉得阅读本文对您有帮助,欢迎转载本文 ...
- leetcode395 Longest Substring with At Least K Repeating Characters
思路: 尺取法. 循环i:1~26,分别计算恰好包含i种字母并且每种字母出现的次数大于等于k个的最长子串长度. 没法直接使用尺取法,因为不满足区间单调性,但是使用如上的方法却是可以的,因为子串中包含的 ...
- codevs 2277 爱吃皮蛋的小明(水题日常)
时间限制: 1 s 空间限制: 32000 KB 题目等级 : 白银 Silver 题目描述 Description 小明特别爱吃蛋,特别是皮蛋.他一次可以吃一个蛋或者两个蛋(整个吞下去),而且他 ...
- 使用python模拟cookie登陆wooyun
import urllib2 class SimpleCookieHandler(urllib2.BaseHandler): def http_request(self, req): simple_c ...
- 结合浅层高层特征的paper总结
1.ION:在conv3.conv4.conv5和context features上分别进行roi_pooling,在channel那一维进行concat 2.Hypernet:在较浅层max_poo ...
- kmp 模板
#include<stdio.h> #include<stdlib.h> #include<string.h> #include<algorithm> ...