MOOC数据结构(下)(自主模式)-重名剔除(Deduplicate)

重名剔除(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

提示

散列

 

C++:

/*
 @Date    : 2018-03-06 20:26:43
 @Author  : 酸饺子 (changzheng300@foxmail.com)
 @Link    : https://github.com/SourDumplings
 @Version : $Id$
*/

/*
https://dsa.cs.tsinghua.edu.cn/oj/problem.shtml?id=1150
 */

#include <iostream>
#include <cstdio>
#include <cstring>

using namespace std;

static const int MAXS = 41;
static const int M = 26 * 26 * 26 * 26 * 2;
static char data[M][MAXS];
static bool outputed[M];

int my_pow(int b, int e)
{
    int res = 1;
    for (int i = 0; i != e; ++i)
        res *= b;
    return res;
}

int my_hash(char name[])
{
    int h = (name[0] - 'a') * my_pow(26, 3);
    if (name[1] != '\0')
    {
        h += (name[1] - 'a') * my_pow(26, 2);
        if (name[2] != '\0')
        {
            h += (name[2] - 'a') * my_pow(26, 1);
            if (name[3] != '\0')
                h += name[3] - 'a';
        }
    }
    return h;
}

int my_find(char name[])
{
    int h = my_hash(name);
    int pos;
    for (pos = h; data[pos][0] != '\0' && strcmp(data[pos], name); pos = (pos + 1) % M);
    return pos;
}

int main(int argc, char const *argv[])
{
    int N;
    scanf("%d", &N);
    for (int i = 0; i != M; ++i)
    {
        outputed[i] = false;
        data[i][0] = '\0';
    }
    for (int i = 0; i != N; ++i)
    {
        char name[MAXS];
        scanf("%s", name);
        int pos = my_find(name);
        if (strcmp(data[pos], name) == 0)
        {
            if (!outputed[pos])
            {
                printf("%s\n", data[pos]);
                outputed[pos] = true;
            }
        }
        else
            strcpy(data[pos], name);
    }
    return 0;
}

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值