map codeforce Chat Order
2016-03-18 21:54
330 查看
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.
Examples
input
output
input
output
Note
In the first test case Polycarpus first writes to friend by name "alex", and the list looks as follows:
alex
Then Polycarpus writes to friend by name "ivan" and the list looks as follows:
ivan
alex
Polycarpus writes the third message to friend by name "roman" and the list looks as follows:
roman
ivan
alex
Polycarpus writes the fourth message to friend by name "ivan", to who he has already sent a message, so the list of chats changes as follows:
ivan
roman
alex
#include<iostream>
#include<map>
#include<cstring>
using namespace std;
string str[222222];
map<string,int>a;
map<string,int>::iterator it;
int main()
{
int n;
string s;
while(cin>>n)
{
for(int i=0,j=n-1;i<n&&j>=0;j--,i++)
{
cin>>s;
str[j]=s;
}
int j = 0;
for(int i=0;i<n;i++)
{
//cout<<str[i]<<endl;
if(a.find(str[i])==a.end())
a[str[i]]=j++;
}
for(it = a.begin(); it != a.end(); ++it){
str[it->second] = it->first;
}
for(int i = 0; i < j; ++i){
cout << str[i] << '\n';
}
}
return 0;
}
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.
Examples
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
Note
In the first test case Polycarpus first writes to friend by name "alex", and the list looks as follows:
alex
Then Polycarpus writes to friend by name "ivan" and the list looks as follows:
ivan
alex
Polycarpus writes the third message to friend by name "roman" and the list looks as follows:
roman
ivan
alex
Polycarpus writes the fourth message to friend by name "ivan", to who he has already sent a message, so the list of chats changes as follows:
ivan
roman
alex
#include<iostream>
#include<map>
#include<cstring>
using namespace std;
string str[222222];
map<string,int>a;
map<string,int>::iterator it;
int main()
{
int n;
string s;
while(cin>>n)
{
for(int i=0,j=n-1;i<n&&j>=0;j--,i++)
{
cin>>s;
str[j]=s;
}
int j = 0;
for(int i=0;i<n;i++)
{
//cout<<str[i]<<endl;
if(a.find(str[i])==a.end())
a[str[i]]=j++;
}
for(it = a.begin(); it != a.end(); ++it){
str[it->second] = it->first;
}
for(int i = 0; i < j; ++i){
cout << str[i] << '\n';
}
}
return 0;
}
相关文章推荐
- ReactNative页面跳转Navigator
- hdu 5615 Jam's math problem
- 使用ClassyShark压缩你的项目
- 作业程序
- POJ 1287 最小生成树
- 打印乘法口诀
- Java面向对象04-继承,访问控制修饰符,static和final
- 自定义cell
- Leetcode 234 Palindrome Linked List 链表
- 如何利用dd和cat做文件拼接
- ADO:_ConnectionPtr,_RecordsetPtr,_CommandPtr
- 高效地配置okhttp
- 出差(二十四)失控
- 笔记本搭建wifi
- 今日BBC
- YARN编程实例—Unmanaged AM工作原理介绍
- 1. Two Sum
- OC-字符串、数组、字典总结
- DFS与BFS之间的区别
- Android下雪动画的实现