重名剔除问题
描述
Epicure先生正在编撰一本美食百科全书。为此,他已从众多的同好者那里搜集到了一份冗长的美食提名清单。既然源自多人之手,其中自然不乏重复的提名,故必须予以筛除。Epicure先生因此登门求助,并认定此事对你而言不过是“一碟小菜”,相信你不会错过在美食界扬名立万的这一良机
输入
第1行为1个整数n,表示提名清单的长度。以下n行各为一项提名
输出
所有出现重复的提名(多次重复的仅输出一次),且以其在原清单中首次出现重复(即第二次出现)的位置为序
样例
Input
10
brioche
camembert
cappelletti
savarin
cheddar
cappelletti
tortellni
croissant
brioche
mapotoufu
Output
cappelletti
brioche
限制
1 < n < 6 * 10^5
提名均由小写字母组成,不含其它字符,且每项长度不超过40
时间:2 sec
空间:256 MB
提示
散列
#include <iostream>
#include <stdio.h>
#include <string.h>
using namespace std;
const int Maxlength = 600005;
typedef struct Node
{
char data[100];
int repeat;
Node *next;
}Node;
Node Hashlist[Maxlength];
char Name[Maxlength][100];
char repeatName[Maxlength][100];
int number ;
int GetHashCode(char ch[])
{
int hashcode = 0;
for(int i=0;i<strlen(ch);i++)
{
hashcode = hashcode + (i + 1)*(ch[i] - 'a' +1);
}
return hashcode;
}
void Insert(int hashnode,char name[])
{
Node *newNode = new Node;
for(int i =0;i<strlen(name);i++)
{
newNode->data[i] = name[i];
}
newNode->next = Hashlist[hashnode].next;
Hashlist[hashnode].next = newNode;
}
int main()
{
int n;
cin>>n;
for(int i=0;i<n;i++)
{
scanf("%s",&Name[i]);
int hashcode = GetHashCode(Name[i])%Maxlength;
Node *node = Hashlist[hashcode].next;
while(node)
{
if(!strcmp(node->data,Name[i]))
{
if(!node->repeat)
{
node->repeat = 1;
// //cout<<number<<endl;
// for(int j =0;j<strlen(Name[i]);j++)
// {
// //cout<<Name[i][j]<<endl;
// repeatName[number][j] = Name[i][j];
// }
//puts(Name[i]);
strcpy(repeatName[number++],Name[i]);
}
break;
}
else
{
node = node->next;
}
}
if(!node)
{
Insert(hashcode,Name[i]);
}
}
for(int i = 0;i<number;i++)
{
//cout<<"ddd"<<endl;
puts(repeatName[i]);
}
return 0;
}