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

  1. 原理与要点:利用hash存储并快速找到字符串,并在存储时记录该字符串出现的次数,如果是第二次出现就输出一下
  2. 遇到的问题:本来想用双hash解决,这样就不用记录字符串了,但是搞了一波发现过不了,就又老老实实写了hash,用单链表记录每个hash值下的字符串。
  3. 时间和空间复杂度:时间复杂度随字符串hash值冲突程度而定,空间复杂度(O(n))
#include "iostream"
#include "cstring"

using namespace std;
typedef long long ll;
const int mod1 = 6e5 - 10;
const int maxn = 6e5;
const int p1 = 13331;
char s[50];
const int SZ = 1 << 20;  //快速io
struct fastio {
    char inbuf[SZ];
    char outbuf[SZ];

    fastio() {
        setvbuf(stdin, inbuf, _IOFBF, SZ);
        setvbuf(stdout, outbuf, _IOFBF, SZ);
    }
} io;

typedef struct node {
    char s[50];
    int cnt;

    node() {
        cnt = 0;
    }

    node *next;
} *Node;
Node v[maxn];

int main() {
    // freopen("in.txt", "r", stdin);
    int n, len;
    ll h;
    scanf("%d", &n);
    for (int i = 0; i < n; i++) {
        scanf("%s", s);
        len = strlen(s);
        h = 0;
        for (int j = 0; j < len; j++) {
            h = (h * p1 % mod1 + (s[j] - 'a' + 1)) % mod1;
        }
        node *now = v[h];
        int flag = 0;
        if (v[h] == NULL) v[h] = new node;

        while (NULL != now) {
            if (strcmp(now->s, s) == 0) {
                now->cnt++;
                if (now->cnt == 2) {
                    printf("%s
", s);
                }
                flag = 1;
                break;
            }
            now = now->next;
        }
        if (!flag) {
            node *temp = new node;
            strcpy(temp->s, s);
            temp->next = v[h]->next;
            temp->cnt = 1;
            v[h]->next = temp;
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/albert-biu/p/11542126.html