CodeForces - 637B . map 水题
2018-02-02 20:01
1351 查看
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.
Example
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这道题主要就是倒着找第一次出现的人名,经提示,使用map 我也是第一次接触map,A-B问题也可以用map
又get一招
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.
Example
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这道题主要就是倒着找第一次出现的人名,经提示,使用map 我也是第一次接触map,A-B问题也可以用map
又get一招
#include <cstdio> #include <iostream> #include <string> #include <stack> #include <map> using namespace std; int main() { int n; stack<string>l; map<string,int>m; string s; scanf("%d",&n); while(n--) { cin>>s; l.push(s); m[s]=1; } while(!l.empty()) { if(m[l.top()]==1)//倒着出现的第一次 { cout<<l.top()<<endl; m[l.top()]--; l.pop(); } else l.pop(); } return 0; }
相关文章推荐
- codeforces 266A Stones on the Table(模拟水题)
- codeforces 557B Pasha and Tea-水题
- codeforces 637D D. Running with Obstacles(dp,水题,贪心)
- CodeForces 300A Array(水题)
- Codeforces--366A--Dima and Guards(水题)
- Codeforces 652A Gabriel and Caterpillar【水题】
- codeforces 659B B. Qualifying Contest(水题+sort)
- 平常水题 - CodeForces - 222B
- codeforces 664A A. Complicated GCD(水题)
- Codeforces 426 B Sereja and Mirroring【暴力枚举+模拟】水题
- 平常水题 - CodeForces - 359C(质数)
- CodeForces 165A Supercentral Point(水题)
- Codeforces 651B Beautiful Paintings【贪心】水题
- CodeForces 602A Two Bases(水题)
- Bits - CodeForces 484 A 水题
- codeforces 675A A. Infinite Sequence(水题)
- CodeForces 682A Alyona and Numbers (水题)
- codeforces 804A Find Amir 思维/水题
- codeforces 677A A. Vanya and Fence(水题)
- 【打CF,学算法——一星级】CodeForces 701A Cards(水题)