Chat Order

Time Limit:3000MS     Memory Limit:262144KB     64bit IO Format:%I64d & %I64u

Submit Status

Description

Polycarp is a big lover of killing time in social networks. A page with a chatlist in his favourite network is made so that when a message is sent to some friend, his friend's chat rises to the very top of the page. The relative order of the other chats doesn't change. If there was no chat with this friend before, then a new chat is simply inserted to the top of the list.

Assuming that the chat list is initially empty, given the sequence of Polycaprus' messages make a list of chats after all of his messages are processed. Assume that no friend wrote any message to Polycarpus.

Input

The first line contains integer n (1 ≤ n ≤ 200 000) — the number of Polycarpus' messages. Next n lines enlist the message recipients in the order in which the messages were sent. The name of each participant is a non-empty sequence of lowercase English letters of length at most 10.

Output

Print all the recipients to who Polycarp talked to in the order of chats with them, from top to bottom.

Sample Input

Input
4
alex
ivan
roman
ivan
Output
ivan
roman
alex
Input
8
alina
maria
ekaterina
darya
darya
ekaterina
maria
alina
Output
alina
maria
ekaterina
darya

第一次用map,感觉功能很强大,映射的功能真的很厉害

Select Code

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<map>
#include<stack>
using namespace std; int main()
{
int n;
scanf("%d",&n);
stack<string>S;
map<string,int>M;
string s;
while(n--)
{
cin >> s;
S.push(s);
M[s] = 1;
}
while(!S.empty())
{
if(M[S.top()] == 1)
{
cout << S.top() << endl;
M[S.top()]--;
S.pop();
}
else
S.pop();
}
return 0;
}

原文链接

  1. map简介
  2. map的功能
  3. 使用map
  4. 在map中插入元素
  5. 查找并获取map中的元素
  6. 从map中删除元素

2。map简介

map是一类关联式容器。它的特点是增加和删除节点对迭代器的影响很小,除了那个操作节点,对其他的节点都没有什么影响。对于迭代器来说,可以修改实值,而不能修改key。

3。map的功能

  1. 自动建立Key - value的对应。key 和 value可以是任意你需要的类型。
  2. 根据key值快速查找记录,查找的复杂度基本是Log(N),如果有1000个记录,最多查找10次,1,000,000个记录,最多查找20次。
  3. 快速插入Key - Value 记录。
  4. 快速删除记录
  5. 根据Key 修改value记录。
  6. 遍历所有记录。

4。使用map

使用map得包含map类所在的头文件
#include <map> //注意,STL头文件没有扩展名.h

map对象是模板类,需要关键字和存储对象两个模板参数:
std:map<int, string> personnel;
这样就定义了一个用int作为索引,并拥有相关联的指向string的指针.

为了使用方便,可以对模板类进行一下类型定义,

typedef map<int, CString> UDT_MAP_INT_CSTRING; 
UDT_MAP_INT_CSTRING enumMap;

5。在map中插入元素

改变map中的条目非常简单,因为map类已经对[]操作符进行了重载

enumMap[1] = "One";
enumMap[2] = "Two";
.....

这样非常直观,但存在一个性能的问题。插入2时,先在enumMap中查找主键为2的项,没发现,然后将一个新的对象插入enumMap,键是2,值是一个空字符串,插入完成后,将字符串赋为"Two"; 该方法会将每个值都赋为缺省值,然后再赋为显示的值,如果元素是类对象,则开销比较大。我们可以用以下方法来避免开销:

enumMap.insert(map<int, CString> :: value_type(2, "Two"))

6。查找并获取map中的元素

下标操作符给出了获得一个值的最简单方法:

CString tmp = enumMap[2];

但是,只有当map中有这个键的实例时才对,否则会自动插入一个实例,值为初始化值。

我们可以使用Find()和Count()方法来发现一个键是否存在。

查找map中是否包含某个关键字条目用find()方法,传入的参数是要查找的key,在这里需要提到的是begin()和end()两个成员,分别代表map对象中第一个条目和最后一个条目,这两个数据的类型是iterator.

int nFindKey = 2;            //要查找的Key
//定义一个条目变量(实际是指针)
UDT_MAP_INT_CSTRING::iterator it= enumMap.find(nFindKey); 
if(it == enumMap.end()) {
    //没找到
}
else {
    //找到
}

通过map对象的方法获取的iterator数据类型是一个std::pair对象,包括两个数据 iterator->first 和 iterator->second 分别代表关键字和存储的数据

7。从map中删除元素

移除某个map中某个条目用erase()

该成员方法的定义如下

  1. iterator erase(iterator it); //通过一个条目对象删除
  2. iterator erase(iterator first, iterator last);        //删除一个范围
  3. size_type erase(const Key& key); //通过关键字删除

clear()就相当于 enumMap.erase(enumMap.begin(), enumMap.end());

Chat Order (map映射)的更多相关文章

  1. codeforces 637B B. Chat Order(map,水题)

    题目链接: B. Chat Order time limit per test 3 seconds memory limit per test 256 megabytes input standard ...

  2. VK Cup 2016 - Qualification Round 1——B. Chat Order(试手stack+map)

    B. Chat Order time limit per test 3 seconds memory limit per test 256 megabytes input standard input ...

  3. VK Cup 2016 - Qualification Round 1 (Russian-Speaking Only, for VK Cup teams) B. Chat Order 水题

    B. Chat Order 题目连接: http://www.codeforces.com/contest/637/problem/B Description Polycarp is a big lo ...

  4. ZOJ 3644 Kitty's Game dfs,记忆化搜索,map映射 难度:2

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=4834 从点1出发,假设现在在i,点数为sta,则下一步的点数必然不能是sta的 ...

  5. POJ2503——Babelfish(map映射+string字符串)

    Babelfish DescriptionYou have just moved from Waterloo to a big city. The people here speak an incom ...

  6. map——映射(message.cpp)

    信息交换 (message.cpp) [题目描述] Byteland战火又起,农夫John派他的奶牛潜入敌国获取情报信息. Cow历尽千辛万苦终于将敌国的编码规则总结如下: 1 编码是由大写字母组成的 ...

  7. filter过滤器与map映射

    filter过滤器 >>> list(filter(None,[0,1,2,True,False])) [1, 2, True] filter的作用就是后面的数据按照前面的表达式运算 ...

  8. map映射

    采集于:https://blog.csdn.net/luanpeng825485697/article/details/78056312 映射map: var map = new Map(); //映 ...

  9. Java精选笔记_集合【Map(映射)接口】

    Map(映射)接口 简介 该集合存储键值对,一对一对的往里存,并且键是唯一的.要保证map集合中键的唯一性. 从Map集合中访问元素时,只要指定了Key,就能找到对应的Value. 关键字是以后用于检 ...

随机推荐

  1. win7+jdk环境变量配置

    进行java开发,首先要安装jdk,安装了jdk后还要进行环境变量配置:1.下载jdk(http://java.sun.com/javase/downloads/index.jsp),我下载的版本是: ...

  2. 3D数学基础 KeyNote 1

    [计算几何复习要点] 1.向量加法的几何含意: a+b的释意为:a的尾连上b的头,新建一条从a的尾指向b的头的向量. 2.向量减法的几何含意: a-b的释意为:尾部相连,新建一个从b的头指向a的头的向 ...

  3. C#使用NPOI导出excel设置单元格背景颜色

    ICellStyle cellStyle = workbook.CreateCellStyle(); cellStyle.FillPattern = FillPattern.SolidForegrou ...

  4. [转]NDK编译库运行时报dlopen failed: cannot locate symbol "__exidx_end" 解决办法

    原文链接:http://blog.csdn.net/acm2008/article/details/41040015 当用NDK编译的库在运行加载时报如下错: dlopen("/data/d ...

  5. 怎样用jQuery拿到select中被选中的option的值

    1.首先要保证select中每一个option标签都有value属性: 2.jquery的写法 1 $('#sele').val()//这里假设select的id是sele,这样可以获取当前选中的op ...

  6. spring+springmvc+mybatis+redis 实现两重数据缓存

    <?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http:// ...

  7. 对JS中函数的理解

    函数本质就是功能的集合 JS中函数是对象,因此,函数名实际上仅仅是一个指向函数对象的指针,不会与某个函数绑定,所以,JS中没有重载(重载就是通过传递不同类型的参数,使两个相同函数名的函数执行不同的功能 ...

  8. 阿里云OSS-web直传---在服务端c#签名,浏览器直传

    OSS web直传---在服务端php签名,浏览器直传 本文:OSS web直传---在服务端c#签名,浏览器直传 其他语言的范例地址:https://help.aliyun.com/document ...

  9. Chrome与Firefox对于时间处理的不同

    new Date() 函数传参数,在火狐浏览器和谷歌浏览器控制台运行,会得到不同的结果,刚开始觉得不可能,后来实际操作才发现此陷阱. 在Firefox中: var dString = "20 ...

  10. [修正] Firemonkey Windows 控件有虚线残影问题

    说明:在 Wndows 显示时,有时控件左方会显示一条虚线 适用:Firemonkey Windows (Berlin 或更高版) 修正方法: 请将源码 FMX.Canvas.D2D.pas 复制到自 ...