【数据结构与算法实习】题目9 重名剔除(Deduplicate)

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

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

要求:
1 < n < 6 * 10^5
提名均由小写字母组成,不含其它字符,且每项长度不超过40字符。

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

输入样例:

10
brioche
camembert
cappelletti
savarin
cheddar
cappelletti
tortellni
croissant
brioche
mapotoufu

输出样例:

cappelletti
brioche

题解:

//本题的逻辑结构:散列表
//本题的存储结构:链式
//解题思路和算法:通过散列函数做位移映射,将数据存入散列表,关键词与地址一一对应,同一个
//              关键词只在散列表中出现一次,如果出现多次则插入失败,若插入失败则插入另一个散列表,
//              由此可输出重复的关键词,且保证输出重复的关键词只输出一次
//效率:时间复杂度O(n)、空间复杂度O(n):
//测试数据: (1)输入:10
            // brioche
            // camembert
            // cappelletti
            // savarin
            // cheddar
            // cappelletti
            // tortellni
            // croissant
            // brioche
            // mapotoufu
            // 输出:cappelletti
            // brioche

#include<stdio.h>
#include<malloc.h>
#include<string.h>
#include<math.h>
#define true 1
#define false 0
#define MAXTABLESIZE 100000

typedef int bool;
typedef char * ElementType;
typedef int Index;
typedef Index Position;
typedef enum {Legitimate, Empty, Deleted} EntryType;

typedef struct HashEntry Cell;
struct HashEntry{
    ElementType Data;
    EntryType Info;
};

typedef struct TblNode * HashTable;
struct TblNode{
    int TableSize;
    Cell * Cells;
};

int NextPrime(int N){
    int i, p=(N%2)?N+2:N+1;

    while(p<=MAXTABLESIZE){
        for(i = (int)sqrt(p); i>2; i--)
            if(!(p%i))break;
        if(i==2)break;
        else p+=2;
    }
    return p;
}

HashTable CreateTable(int TableSize){
    HashTable H;
    int i;
    H = (HashTable)malloc(sizeof(struct TblNode));
    H->TableSize = NextPrime(TableSize);
    H->Cells = (Cell *)malloc(H->TableSize*sizeof(Cell));
    for(int i=0;i<H->TableSize;i++){
        H->Cells[i].Info = Empty;
    }

    return H;
}

int Hash(const char *Key, int TableSize){
    unsigned int H = 0;
    while(*Key !='\0')
        H = (H<<5)+*Key++;
    return H%TableSize;
}

Position Find(HashTable H, ElementType Key){
    Position CurrentPos, NewPos;
    int CNum = 0;

    NewPos = CurrentPos = Hash(Key, H->TableSize);
    while(H->Cells[NewPos].Info!=Empty && H->Cells[NewPos].Data!=Key){
        if(++CNum%2){
            NewPos = CurrentPos+(CNum+1)*(CNum+1)/4;
            if(NewPos>=H->TableSize)
                NewPos = NewPos %H->TableSize;
        }
        else{
            NewPos = CurrentPos - CNum *CNum/4;
            while(NewPos<0){
                NewPos += H->TableSize;
            }
        }
    }
    return NewPos;
}

bool Insert(HashTable H, ElementType Key){
    Position Pos = Find(H, Key);

    if(H->Cells[Pos].Info!=Legitimate){
        H->Cells[Pos].Info = Legitimate;
        H->Cells[Pos].Data = Key;
        return true;
    }
    else{
        return false;
    }
}


int main(){
    char arr[1000][1000];
    HashTable table1 = CreateTable(MAXTABLESIZE);
    HashTable table2 = CreateTable(MAXTABLESIZE);

    int n;
    int count =0;
    scanf("%d", &n);
    for(int i = 0;i<n;i++){
        char string1[MAXTABLESIZE];
        scanf("%s", string1);
        if(Insert(table1, string1))
            continue;
        else{
            if(Insert(table2, string1)){
                for(int j = 0;j<strlen(string1);j++){
                arr[count][j] = string1[j];
                }
                count++;
            }
            else{
                continue;
            }
            
        }
    }
    for(int i = 0;i<count;i++){
        for(int j = 0;j<strlen(arr[i]);j++){
            printf("%c", arr[i][j]);
        }
        printf("\n");
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

a9c93f2300

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值