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

分析:

利用hash存儲並快速找到字符串,並在存儲時記錄該字符串出現的次數,如果是第二次出現就輸出一下

我的代碼:

#include<cstdio>
#include<cstring>
#define HASHSIZE 600001
//請獨立完成
using namespace std;

struct Sl{
	char* data;
	bool repeat;
	Sl* suc;
}buckets[HASHSIZE];

char name[HASHSIZE][41];
void Insert(int a, char* s) {
	Sl* t = new Sl;
	t->data = s; t->repeat = false;
	t->suc = buckets[a].suc;
	buckets[a].suc = t;
}
int HashCode(char* s) {
	int sum = 0, len = strlen(s);
	for (int i = 0; i<len; i++)
		sum += (i + 1)*(s[i] - 'a'+1);
	return sum;
}
int main() {
	int n;
	scanf("%d", &n);
	for (int i = 0; i < n; i++){
		scanf("%s", name[i]);
		int a = HashCode(name[i]) % HASHSIZE;
		Sl* p = buckets[a].suc;
		while (p)
		if (!strcmp(p->data, name[i])) {
			if (!p->repeat) {
				p->repeat = true;
				printf("%s\n",name[i]);
			}break;
		}
		else p = p->suc;
		if (!p)Insert(a, name[i]);
	}
	return 0;
}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章