B - Chat Order -- (各种映射乱搞)

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

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

最开始开结构体数组映射乱搞,果断超时,各种生活不能自理
超时代码:
#include <stdio.h>
#include <string.h>
struct p{
	char s[12];
	int flag;
}p[200005];
int main(){
	int n,i,j;
	scanf("%d",&n);
	for(i=1;i<=n;i++){
		p[i].flag=0;
	}
	scanf("%s",p[1].s);
	for(i=2;i<=n;i++){
		scanf("%s",p[i].s);
		for(j=i-1;j>=1;j--){
			if(!p[j].flag)
			if(strcmp(p[i].s,p[j].s)==0){
				p[j].flag=1;
				break;
			}
		}
	}
	for(i=n;i>=1;i--){
		if(p[i].flag==0)
			printf("%s\n",p[i].s);
	}
	return 0;
}

最后试着用STL,,,,第一次用map映射。。。貌似很好用的啊
#include <cstdio>
#include <iostream>
#include <string>
#include <stack>
#include <map>
using namespace std;
int main(){
	int n;
	stack<string>S;
	map<string,int>M;
	string s;
	scanf("%d",&n);
	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;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值