重名剔除(Deduplicate)

清华OJ——数据结构与算法实验(中国石油大学)

重名剔除(Deduplicate)


Description

Mr. Epicure is compiling an encyclopedia of food. He had collected a long list of candidates nominated by several belly-gods. As candidates in list are nominated by several people, duplication of name is inevitable. Mr. Epicure pay you a visit for help. He request you to remove all duplication, which is thought an easy task for you. So please hold this opportunity to be famous to all belly-gods.

Input

1 integer in fist line to denote the length of nomination list. In following n lines, each nomination is given in each line.

Output

All the duplicated nomination (only output once if duplication appears more multiple times), which is sorted in the order that duplication appears firstly.

Example

Input

10
brioche
camembert
cappelletti
savarin
cheddar
cappelletti
tortellni
croissant
brioche
mapotoufu

Output

cappelletti
brioche

Restrictions

1 < n < 6 * 10^5

All nominations are only in lowercase. No other character is included. Length of each item is not greater than 40.

Time: 2 sec

Memory: 256 MB

Hints

Hash

描述

Epicure先生正在编撰一本美食百科全书。为此,他已从众多的同好者那里搜集到了一份冗长的美食提名清单。既然源自多人之手,其中自然不乏重复的提名,故必须予以筛除。Epicure先生因此登门求助,并认定此事对你而言不过是“一碟小菜”,相信你不会错过在美食界扬名立万的这一良机

输入

第1行为1个整数n,表示提名清单的长度。以下n行各为一项提名

输出

所有出现重复的提名(多次重复的仅输出一次),且以其在原清单中首次出现重复(即第二次出现)的位置为序

样例

见英文题面

限制

1 < n < 6 * 10^5

提名均由小写字母组成,不含其它字符,且每项长度不超过40

时间:2 sec

空间:256 MB

提示

散列

 1 #include<cstdio>
 2 #include<cstring>
 3 #define N 605000
 4 using namespace std;
 5 const unsigned long long base=27;
 6 
 7 char s[N][45];
 8 
 9 struct ss
10 {
11     int index;
12     unsigned long long h;
13 
14     bool operator < (const ss &s)const
15     {
16         if(h!=s.h)return h<s.h;
17         return index<s.index;
18     }
19 };
20 ss arr[N];
21 int vis[N]={0};
22 ss ls[N];
23 
24 void sort(int l,int r)
25 {
26     if(l==r)return;
27     int mid=(l+r)/2;
28     sort(l,mid);
29     sort(mid+1,r);
30 
31     int c1=l,c2=mid+1;
32     int c=l;
33 
34     while(c1<=mid&&c2<=r)
35     {
36         if(arr[c1]<arr[c2])ls[c++]=arr[c1++];
37         else
38             ls[c++]=arr[c2++];
39     }
40 
41     while(c1<=mid)ls[c++]=arr[c1++];
42     while(c2<=r)ls[c++]=arr[c2++];
43     for(int i=l;i<=r;i++)arr[i]=ls[i];
44 }
45 
46 int main()
47 {
48     int n;
49     scanf("%d",&n);
50     for(int i=0;i<n;i++)
51     {
52         scanf("%s",s[i]);
53         int len=strlen(s[i]);
54         unsigned long long now=0;
55 
56         for(int j=0;j<len;j++)
57         {
58             now=now*base+s[i][j];
59         }
60         arr[i].index=i;
61         arr[i].h=now;
62     }
63 
64     sort(0,n-1);
65     int c1=1;
66 
67    // for(int i=0;i<n;i++)printf("%llu %d\n",arr[i].h,arr[i].index);
68 
69     while(c1<n)
70     {
71         if(arr[c1].h==arr[c1-1].h)
72         {
73             vis[arr[c1].index]=1;
74             while(arr[c1].h==arr[c1-1].h)c1++;
75         }
76         else
77         {
78             c1++;
79         }
80     }
81 
82     for(int i=0;i<n;i++)
83         if(vis[i])printf("%s\n",s[i]);
84 
85     return 0;
86 }

 

posted @ 2021-11-27 20:46  sylvia11  阅读(242)  评论(0编辑  收藏  举报