map就是从键(key)到值(value)的映射。

因为重载了[]运算符,map像是数组的”高级版“。

例如,map<string,int>month_name 表示:”月份名字到月份编号“的映射。

赋值方式: month_name["July"]=7;(类似于 month_name["string"]=int )

例题5-4

Problem

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.

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

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

Analyse

把每个单词“标准化”,即全部转化为小写字母后再进行排序,然后再放到map中进行统计。

#include<cstdio>
#include<string>
#include<cctype>
#include<iostream>
#include<vector>
#include<map>
#include<algorithm>
using namespace std; map<string,int> cnt;
vector<string> words; //将单词s进行“标准化”
string repr(const string& s)
{
string ans=s;
for(int i=;i<ans.length();i++)
ans[i]=tolower(ans[i]); //tolower()函数,把字符转换成小写字母,非字母字符不做出处理。头文件为ctype.h。
sort(ans.begin(),ans.end());
return ans;
} int main()
{
int n=;
string s;
while(cin>>s)
{
if(s[]=='#')
break;
words.push_back(s); //把单词一个个存储在vector定义的不定长数组words中
string r=repr(s); //调用repr()函数,通过引用,返回一个"标准化"的单词
if(!cnt.count(r)) //count()函数只能返回0(不存在)或者1(存在)。返回指定元素的次数。
cnt[r]=;
cnt[r]++;
}
vector<string> ans;
for(int i=;i<words.size();i++)
if(cnt[repr(words[i])]==)
ans.push_back (words[i]);
sort(ans.begin(),ans.end());
for(int i=;i<ans.size();i++)
cout<<ans[i]<<'\n';
return ;
}

(STL初步)映射:map的更多相关文章

  1. C++ STL之映射map的使⽤

    写在最前面:本文摘录于柳神笔记: map 是键值对,⽐如⼀个⼈名对应⼀个学号,就可以定义⼀个字符串 string 类型的⼈名为“键”,学 号 int 类型为“值”,如 map<string, i ...

  2. C++标准模板库(STL)之Map

    1.Map的常用用法 map:映射.可以将任何基本类型,结构体,STL容器映射到任何基本类型包括容器. 使用map,需要加map的头文件,#include<map>和using names ...

  3. STL中的map、unordered_map、hash_map

    转自https://blog.csdn.net/liumou111/article/details/49252645 在之前使用STL时,经常混淆的几个数据结构,特别是做Leetcode的题目时,对于 ...

  4. [知识点]C++中STL容器之map

    UPDATE(20190416):写完vector和set之后,发现不少内容全部引导到map上了……于是进行了一定的描述补充与更正. 零.STL目录 1.容器之map 2.容器之vector 3.容器 ...

  5. STL 中的map 与 hash_map的理解

    可以参考侯捷编著的<STL源码剖析> STL 中的map 与 hash_map的理解 1.STL的map底层是用红黑树存储的,查找时间复杂度是log(n)级别: 2.STL的hash_ma ...

  6. VIM键盘映射 (Map)~转载

    VIM键盘映射 (Map) 设置键盘映射 使用:map命令,可以将键盘上的某个按键与Vim的命令绑定起来.例如使用以下命令,可以通过F5键将单词用花括号括起来: :map <F5> i{e ...

  7. 图像映射map

    <map>标签:带有可点击区域的图像映射 定义一个客户端图像映射.图像映射(image-map)指带有可点击区域的一幅图像. 效果图: 点击相应蓝色标签可进入详情页面浏览. 代码: < ...

  8. UVa 11991:Easy Problem from Rujia Liu?(STL练习,map+vector)

    Easy Problem from Rujia Liu? Though Rujia Liu usually sets hard problems for contests (for example, ...

  9. <顶>vim快捷键映射Map使用

    问题描述: 使用vim中的快捷键映射map,可以自定义快捷键 问题解决: (1)vim模式 (2)map前缀 (3)删除映射Map (4)使用示例 (5)查看快捷键映射 命令行---:verbose ...

  10. STL 整理(map、set、vector、list、stack、queue、deque、priority_queue)(转)

    向量(vector) <vector> 连续存储的元素<vector> Vector<int>c; c.back()    传回最后一个数据,不检查这个数据是否存在 ...

随机推荐

  1. 《C Primer Plus(第6版)中文版》一1.12 复习题

    本节书摘来自异步社区<C Primer Plus(第6版)中文版>一书中的第1章,第1.12节,作者 傅道坤,更多章节内容可以访问云栖社区"异步社区"公众号查看. 1. ...

  2. windows下安装Pycham2020软件

    1.在pycham官网下载安装软件https://www.jetbrains.com/pycharm/download/#section=windows 2.我下载的是64位的安装包,现在开始安装 3 ...

  3. Xapian实战(一):环境搭建 + 简介

    1. 参考资料 http://xapian.org/docs/install.html Xapian的存储系统.性能以及检索模型等 2. 安装 1) xapian # ./configure --pr ...

  4. ACM-ICPC 2019 山东省省赛 C Wandering Robot

    这个题额,我觉的是一道水题,思维题,需要考虑的情况比较多,题意一个机器人给一条指令,循环n遍,问此过程中离原点最远距离. 考虑最远距离可能出现的的情况. 每次循环之后距离至少为0: 1.假设他每一次循 ...

  5. MySQL命令1

    开始学习MySQL. // 创建数据库 CREATE DATABASE db_name; // 删除数据库 DROP DATABASE db_name; // 显示数据库 SHOW DATABASES ...

  6. Programming Languages_05 FWAE

    FWAE : Concrete syntax <FWAE> ::= <num> | {+ <FWAE> <FWAE>} | {- <FWAE> ...

  7. Flutter 1.17版本重磅发布

    Flutter 1.17 是2020年的第一个稳定版本,此版本包括iOS平台Metal支持(性能更快),新的Material组件,新的Network跟踪工具等等! 对所有人来说,今年是充满挑战的一年. ...

  8. Linova and Kingdom(树型-贪心)

    题目大意:给定一棵树,1为首都(首都可以是工业城市也可以是旅游城市),一共有n个点. 其中要选出k个工业城市,每个工业城市出一个代表去首都,其快乐值是其途径旅游城市(非工业)的个数 求所有快乐值相加的 ...

  9. R - Weak Pair HDU - 5877 离散化+权值线段树+dfs序 区间种类数

    R - Weak Pair HDU - 5877 离散化+权值线段树 这个题目的初步想法,首先用dfs序建一颗树,然后判断对于每一个节点进行遍历,判断他的子节点和他相乘是不是小于等于k, 这么暴力的算 ...

  10. SQL SERVER 函数举例

    需求说明 将字符串按照指定的分隔符进行分割,并将结果按照从后往前的顺序倒序排列,拼接后的结果用‘/’符连接.(也可以按照指定符号分割为多个列,修改最后一部分即可) 创建测试表及数据 /* 创建一张测试 ...