重名剔除(Deduplicate)

Epicure先生正编撰美食百科全书,需处理一份包含重复提名的清单。本篇介绍一种通过散列表技术实现高效去重的方法,确保每位被提名的美食只被记录一次。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

重名剔除(Deduplicate)


描述

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

输入

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

输出

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

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


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

散列表,瞎搞搞


#include<stdio.h>
#include<string.h>
typedef struct Hash
{
	Hash *next;
	char str[45];
}Hash;
Hash *s[600005];
char str[55];
int Sech(int x)
{
	int sum = 0;
	Hash *p = s[x];
	while(p->next!=NULL)
	{
		p = p->next;
		if(strcmp(p->str+1, str+1)==0)
			sum++;
		if(sum==2)
			return sum;
	}
	Hash *temp = new Hash;
	temp->next = NULL;
	strcpy(temp->str+1, str+1);
	p->next = temp;
	return sum;
}
int main(void)
{
	int n, i, j, x, y;
	scanf("%d", &n);
	for(i=0;i<=600000;i++)
	{
		s[i] = new Hash;
		s[i]->next = NULL;
	}
	for(i=1;i<=n;i++)
	{
		scanf("%s", str+1);
		x = y = 0;
		for(j=1;str[j]!='\0';j++)
		{
			x += (str[j]-'a'+1)*j;
			x %= 1003;
		}
		for(j--;j>=1;j--)
		{
			y += (str[j]-'a'+j)*j;
			y %= 597;
		}
		y = y*1000+x;
		x = Sech(y);
		if(x==1)
			puts(str+1);
	}
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值